Git

Peter.Chen

2018/01/24

Agenda

  • VS SVN
  • Local
  • Remote

Git VS SVN

https://www.git-tower.com/learn/git/ebook/en/desktop-gui/appendix/from-subversion-to-git 

Local Operations

  • working directory: 專案資料夾
  • staging area: 下次提交中所設定的資訊
  • git directory: 紀錄每次提交的檔案資訊

https://git-scm.com/book/en/v2/Getting-Started-Git-Basics

寫些文件

建立Git Repo

下git init後,目標資料夾下會多一個.git的隱藏資料夾,有此資料夾代表此目錄就是git directory

查看檔案狀態

PS D:\Code\Personal\GitDemo> git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        hellogit.txt

nothing added to commit but untracked files present (use "git add" to track)
PS D:\Code\Personal\GitDemo>

將檔案加入staging area

PS D:\Code\Personal\GitDemo> git add .\hellogit.txt
PS D:\Code\Personal\GitDemo> git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   hellogit.txt

git add將此檔案變更設定為此次提交

提交至檔案庫

PS D:\Code\Personal\GitDemo> git commit -m 'Initial Project'
[master (root-commit) 4d14395] Initial Project
 1 file changed, 1 insertion(+)
 create mode 100644 hellogit.txt
PS D:\Code\Personal\GitDemo>

在staging area中的檔案資訊被提交到repo上

查看commit

PS D:\Code\Personal\GitDemo> git log
commit 4d1439564da9eb61660e9061586fe4702f179472
Author: peter_hp_chen <peter_hp_chen@gmail.com>
Date:   Wed Jan 24 00:10:07 2018 +0800

    Initial Project

git log查找提交紀錄

回憶一下

1. 建立資料夾

2. git init

3. git add

4. git status

5. git commit

建立分支做開發

PS D:\Code\Personal\GitDemo> git branch develop
PS D:\Code\Personal\GitDemo> git branch -v
  develop 4d14395 Initial Project
* master  4d14395 Initial Project
  • git branch {branch name}: 創建分支
  • git branch -v: 查看目前分支狀態

切換分支

git checkout {branch name}

將分支檔案拉回工作目錄上

PS D:\Code\Personal\GitDemo> git checkout develop
Switched to branch 'develop'
PS D:\Code\Personal\GitDemo> git branch -v
* develop 4d14395 Initial Project
  master  4d14395 Initial Project

Remote

設定遠端路徑

git remote add origin https://github.com/peterhpchen/GItDemo.git

本地檔案庫推至遠端

PS D:\Code\Personal\GitDemo> git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 236 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/peterhpchen/GItDemo.git
 * [new branch]      master -> master

git push {remote repo name} {branch name}

遠端檔案庫拉至本地

  1. git clone: 還未初始化git檔案庫
  2. git pull: 已初始git檔案庫且已設定遠端檔案庫
PS D:\Code\Personal\GitDemo> git clone https://github.com/peterhpchen/GItDemo.git
Cloning into 'GItDemo'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
PS D:\Code\Personal\GitDemo> git pull origin master
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/peterhpchen/GItDemo
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master

有人修改檔案並推至遠端

辛苦開發了一整天...

將Hello Git!改成Hello Local!

PS D:\Code\Personal\GitDemo> git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   hellogit.txt

no changes added to commit (use "git add" and/or "git commit -a")
PS D:\Code\Personal\GitDemo> git add .\hellogit.txt
PS D:\Code\Personal\GitDemo> git commit -m 'Local modified txt'
[master a4a0340] Local modified txt
 1 file changed, 1 insertion(+), 1 deletion(-)
PS D:\Code\Personal\GitDemo> git pull origin master
From https://github.com/peterhpchen/GItDemo
 * branch            master     -> FETCH_HEAD
Auto-merging hellogit.txt
CONFLICT (content): Merge conflict in hellogit.txt
Automatic merge failed; fix conflicts and then commit the result.
PS D:\Code\Personal\GitDemo> git merge reset
error: Merging is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.

Open Conflict File

修改為正確的內容後再做次commit

PS D:\Code\Personal\GitDemo> git add .\hellogit.txt
PS D:\Code\Personal\GitDemo> git commit -m 'Modified is correct'
[master 0d17348] Modified is correct
PS D:\Code\Personal\GitDemo> git push origin master
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 459 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To https://github.com/peterhpchen/GItDemo.git
   c60c781..0d17348  master -> master

Thank You

Git

By Peter Chen

Git

Git Command Introduce

  • 147