文章作者:Tyan
博客:noahsnail.com | CSDN | 簡書
1. Git倉庫的創(chuàng)建
- 在當前目錄新建一個Git倉庫
# 命令形式:git init
$ cd TestGit
$ git init
Initialized empty Git repository in /Users/***/TeskGit/.git/
- 在指定目錄下新建一個Git倉庫
# 命令形式:git init [Directory Name]
$ git init TestGit
Initialized empty Git repository in /Users/***/TeskGit/.git/
- 從Github等地方克隆一個倉庫到當前目錄(可能需要輸入密碼,以ssh方式克?。?,如果不指定分支,則默認從Refactor倉庫的Default branch(一般為master)克隆
# 命令形式:git clone [url] -b [branch name]
$ git clone ssh://git@github.com/**/Refacor -b master
Cloning into 'Refacor'...
Saving password to keychain failed
Identity added: /Users/**/.ssh/id_rsa_github ((null))
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
Checking connectivity... done.
2. 關聯(lián)遠程倉庫
- 將本地倉庫與遠程倉庫關聯(lián),首先要初始化一個本地倉庫,url可以在github的倉庫獲得,origin為遠程倉庫在本地的別名,即origin=git@github.com:***/Refacor.git
# 命令形式:git remote add [remote repository aliase] [url]
$ git remote add origin git@github.com:***/Refacor.git
- 查看本地倉庫關聯(lián)的遠程倉庫
# 命令形式:git remote [-v]
$ git remote
origin
$ git remote -v
origin git@github.com:***/Refacor.git (fetch)
origin git@github.com:***/Refacor.git (push)
- 刪除本地倉庫關聯(lián)的遠程倉庫
# 命令形式:git remote rm [remote repository aliase]
$ git remote rm origin
$ git remote
- 關聯(lián)遠程倉庫后,從遠程倉庫取內(nèi)容,并根據(jù)遠端倉庫在本地創(chuàng)建了兩個分支,master和develop
# 命令形式:git fetch
$ git fetch
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:***/Refacor
* [new branch] develop -> origin/develop
* [new branch] master -> origin/master
- 顯示遠程倉庫的信息
# 命令形式:git remote show [remote repository name]
$ git remote show origin
* remote origin
Fetch URL: git@github.com:***/Refacor.git
Push URL: git@github.com:***/Refacor.git
HEAD branch: master
Remote branches:
develop tracked
master tracked
Local branches configured for 'git pull':
develop merges with remote develop
master merges with remote master
Local refs configured for 'git push':
develop pushes to develop (up to date)
master pushes to master (local out of date)
3. Git分支的創(chuàng)建、切換、刪除、關聯(lián)
- 創(chuàng)建本地分支并切換到新創(chuàng)建的分支,-b代表新創(chuàng)建一個分支,-B用在本地分支已經(jīng)存在的情況下,強行創(chuàng)建一個新分支并將原來的分支覆蓋,checkout主要是分支切換,創(chuàng)建分支使用branch命令
# 命令形式:git checkout [-b or -B] [local branch name]
$ git checkout -b a
Switched to a new branch 'a'
$ git checkout -b a
fatal: A branch named 'a' already exists.
$ git checkout -B a
Switched to and reset branch 'a'
- 切換本地分支
# 命令形式:git checkout [local branch name]
$ git checkout develop
Switched to branch 'develop'
- 查看當前所在的本地分支,創(chuàng)建本地分支,local branch name不存在時是查看當前所在的本地分支,存在時是創(chuàng)建一個新的本地分支
# 命令形式:git branch [local branch name]
$ git branch
* a
develop
master
$ git branch b
- 查看遠程分支和所有分支,-r是查看遠程分支,-a是查看所有分支
# 命令形式:git branch [-r] [-a]
$ git branch -r
origin/develop
origin/master
$ git branch -a
develop
* master
remotes/origin/develop
remotes/origin/master
- 刪除本地分支
# 命令形式:git branch -d [local branch name]
$ git branch -d test
Deleted branch test (was 0b05e43).
- 創(chuàng)建本地分支并與遠端分支關聯(lián)
# 命令形式:git checkout -b [local branch name] origin/[remote branch name]
$ git checkout -b develop origin/develop
Branch develop set up to track remote branch develop from origin.
Switched to a new branch 'develop'
$ git checkout -b master origin/master
Branch master set up to track remote branch master from origin.
Switched to a new branch 'master'
- 關聯(lián)遠程分支
# 命令形式:git branch --set-upstream-to [remote repository name/remote branch]
$ git branch --set-upstream-to origin/develop
Branch develop set up to track remote branch develop from origin.
- 刪除遠程分支,git push origin --delete [remote branch name]是直接刪除掉遠程倉庫的分支,git branch -dr [remote/branch]是刪除本地分支與遠程分支的關聯(lián)關系。
# 命令形式:git push origin --delete [remote branch name] or git branch -dr [remote/branch]
$ git push origin --delete develop
To git@github.com:***/Refacor.git
- [deleted] develop
$ git branch -dr origin/develop
Deleted remote-tracking branch origin/develop (was d6813fd).
注:本地可以有多個分支,遠程也可以有多個分支,本地多個分支可以關聯(lián)遠程多個分支,但是,本地分支最好與遠程分支同名,以免出現(xiàn)問題。
4.從遠程倉庫取內(nèi)容,向遠程倉庫提交內(nèi)容
向遠程倉庫提交內(nèi)容之前,要理解三個概念:本地文件,緩沖區(qū),本地倉庫。平常修改的文件內(nèi)容都是本地文件,在往遠程倉庫提交之前先要提交到緩沖區(qū),再從緩沖區(qū)提交到本地倉庫,然后本地倉庫才能往遠程倉庫提交。本地的內(nèi)容分為三大部分,它們是相互獨立的,理解了這四個概念就明白為什么要執(zhí)行git add,git commit,git push命令了。
- 從遠程倉庫取內(nèi)容,git fetch默認情況下是當前的本地分支從其關聯(lián)的遠程分支上取內(nèi)容,可以使用git branch先查看當前的本地分支,使用git status(包括本地分支和遠程分支都能看到)查看其關聯(lián)的遠程分支。從下面的例子中可以看出本地分支為develop,遠程分支也為develop
# 命令形式:git fetch,git merge,git pull
$ git branch
* develop
master
$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
nothing to commit, working directory clean
$ git fetch
- 將從遠程倉庫取下的內(nèi)容與本地倉庫的內(nèi)容進行合并,合并之前必須將本地的修改提交到本地倉庫。
# 命令形式:git merge
# 正常情況下
$ git merege
Already up-to-date.
# 沒有提交修改到本地倉庫的情況下
$ git merge
Updating d6813fd..2444abc
error: Your local changes to the following files would be overwritten by merge:
README.md
Please, commit your changes or stash them before you can merge.
Aborting
# 本地倉庫與取下的遠程倉庫內(nèi)容有沖突的情況下
$ git merge
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
- 解決沖突,沖突是使用git時常常會碰到的情況,沖突解決主要是在將本地倉庫內(nèi)容與遠程倉庫取下的內(nèi)容merge后,進入merge后的文件進行修改,將沖突解決,然后重新add,commit,push即可。
沖突內(nèi)容:
# Refacor
<<<<<<< HEAD
branch develop
=======
branch
>>>>>>> refs/remotes/origin/develop
修改后的內(nèi)容:
# Refacor
branch develop
處理過程:
$ vim README.md
$ git add .
$ git commit -m "feat: handle conflict"
[develop d4c01a6] feat: handle conflict
$ git push
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 416 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To git@github.com:***/Refacor.git
2444abc..d4c01a6 develop -> develop
- 直接從遠程倉庫取內(nèi)容并合并
# 命令形式:git pull
$ git pull
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:***/Refacor
d4c01a6..afa74f2 develop -> origin/develop
Updating d4c01a6..afa74f2
Fast-forward
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
備注:git pull = git fetch + git merge,git pull功能很強大,能自動合并沖突,合并后需要手動解決沖突,最好不要直接使用git pull,因為許多細節(jié)都看不到。
- 將本地文件修改提交到緩沖區(qū),git add filename是將單個文件提交到緩沖區(qū),可以提交多個文件,中間用空格分開,也可提交目錄,git add . 是將所有修改內(nèi)容提交到緩沖區(qū)
# 命令形式:git add [filename1] [filename2] or [directoryname] or [.]
$ git add README.md
$ git add README.md a.txt
$ git add .
- 將緩沖區(qū)內(nèi)容提交到本地倉庫
# 命令形式:git commit [file1] [file2] -m [comment]
$ git commit -m "feat: edit README.md"
[develop d6813fd] feat: edit README.md
1 file changed, 1 insertion(+), 1 deletion(-)
$ git commit README.md a.txt -m "feat: commit test"
- 將本地倉庫內(nèi)容提交到遠程倉庫,默認情況下,如果你的本地分支與遠程分支同名且關聯(lián),git push就可以,但如果不是你需要加上更多的東西。我本地倉庫有master和develop分支,遠程倉庫有master和develop分支,我本地倉庫的develop分支與遠程倉庫的master分支關聯(lián)。所以直接git push是不可以的。最好不要這么干,我只是測試一下。下面的另一個例子是我將本地倉庫的develop分支與遠程倉庫的develop分支關聯(lián)并提交修改到遠程倉庫。
# 命令形式:git push [remote repository name] [remote branch]
git push
fatal: The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use
git push origin HEAD:master
To push to the branch of the same name on the remote, use
git push origin develop
$ git push origin HEAD:master
Counting objects: 3, done.
Writing objects: 100% (3/3), 296 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:***/Refacor.git
f7ea1a0..d6813fd HEAD -> master
$ git branch --set-upstream-to origin/develop
Branch develop set up to track remote branch develop from origin.
$ git status
On branch develop
Your branch is ahead of 'origin/develop' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
$ git fetch
$ git merge
Already up-to-date.
$ git push
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:***/Refacor.git
f7ea1a0..d6813fd develop -> develop
5. Git配置
- 查看Git的所有配置信息
# 命令形式:git config --list
$ git config --list
- 查看特定的配置信息
# 命令形式:git config --get [variable]
$ git config --get user.name
test
$ git config --global --get user.email
global@test.com
- 配置用戶名和郵件,加上--global是配置全局的,否則是配置當前倉庫的,當前倉庫的配置會覆蓋全局配置
# 命令形式:git config [--global] [user.name or user.email]
$ git config --global user.name "test global"
$ git config user.name "test"
$ git config --global user.email "global@test.com"
$ git config user.email "local@test.com"
6. 查看提交記錄
- 查看提交記錄,當前分支的版本歷史
#命令形式:git log
$ git log
commit d6813fdf94c37d51e952aee42155654fe333ef28
Author: *** <***@gmail.com>
Date: Fri Oct 22 17:05:12 2016 +0800
feat: edit README.md
commit f7ea1a04e423e135d92c4c93e4f188e7257b0cad
Author: *** <***@gmail.com>
Date: Fri Oct 22 16:39:45 2016 +0800
feat:develop
commit 68506c5f153fc303fa33ceb733bf15b6da690b00
Merge: 9b7d7c8 f777a9b
Author: *** <***@gmail.com>
Date: Fri Oct 22 16:25:40 2016 +0800
feat: test
commit 9b7d7c87c32e144ea1364c1a0aa0fe16f074c8d5
Author: *** <***@gmail.com>
Date: Fri Oct 22 16:18:18 2016 +0800
- 顯示commit歷史,以及每次commit發(fā)生變更的文件
# 命令形式:git log --stat
$ git log --stat
commit afa74f2de6caede85b6b4beeefc3ec338ed42986
Author: *** <***@gmail.com>
Date: Fri Oct 22 17:35:32 2016 +0800
Update README.md
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
commit 9b313361b73cd76505c05e6b6ad3b0c6204357ce
Author: *** <***@gmail.com>
Date: Fri Oct 22 17:25:28 2016 +0800
feat: conflict
README.md | 2 +-
- 查看最近的操作信息
# 命令形式:git reflog
$ git reflog
afa74f2 HEAD@{0}: checkout: moving from master to develop
d6813fd HEAD@{1}: merge refs/remotes/origin/master: Fast-forward
f777a9b HEAD@{2}: checkout: moving from develop to master
afa74f2 HEAD@{3}: pull: Fast-forward
d4c01a6 HEAD@{4}: commit (merge): feat: handle conflict
9b31336 HEAD@{5}: commit: feat: conflict
d6813fd HEAD@{6}: commit: feat: edit README.md
f7ea1a0 HEAD@{7}: commit: feat:develop
68506c5 HEAD@{8}: commit (merge): feat: test
9b7d7c8 HEAD@{9}: commit: feat:branch test
0b05e43 HEAD@{10}: checkout: moving from master to develop
f777a9b HEAD@{11}: pull: Fast-forward
0b05e43 HEAD@{12}: checkout: moving from develop to master
0b05e43 HEAD@{13}: checkout: moving from a to develop
0b05e43 HEAD@{14}: checkout: moving from develop to a
0b05e43 HEAD@{15}: checkout: moving from a to develop
0b05e43 HEAD@{16}: checkout: moving from origin/develop to a
0b05e43 HEAD@{17}: checkout: moving from 0b05e4388c3cb16d23a59f7238d59894c7f1fb8
- 顯示緩存區(qū)和工作區(qū)的差異
# 命令形式:git diff
$ git diff
diff --git a/a.txt b/a.txt
index d00491f..cd6c7f8 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1 @@
-1
+1dfa
7. 切換分支修改內(nèi)容
有時候你正在某個分支上進行開發(fā),突然來了一個任務要修改另一個分支上的內(nèi)容,而此時你還不想將當前分支的內(nèi)容提交,git stash(保存當前的工作狀態(tài))命令就有了用處。
# 查看當前工作狀態(tài)
$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
# 命令形式:git stash/git stash save "message",git stash是保存當前的工作進度。會分別對暫存區(qū)和工作區(qū)的狀態(tài)進行保存,save "message"是添加的備注信息,可以沒有。
$ git stash save "test git stash"
Saved working directory and index state On develop: test git stash
HEAD is now at d6813fd feat: edit README.md
# 命令形式:git stash list,查看當前的
$ git stash list
stash@{0}: On develop: test git stash
# 切換分支,查看狀態(tài),修改當前分支內(nèi)容
$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
# 完成master分支的修改后,恢復stash中的內(nèi)容,然后繼續(xù)開發(fā)develop
# 命令形式:git stash pop stash@{0},git stash pop是恢復stash中的第一個的內(nèi)容,有多個時也可以添加索引,形式為stash@{index}
$ git stash pop stash@{0}
On branch develop
Your branch is up-to-date with 'origin/develop'.
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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
Dropped stash@{0} (b30b83ef5fe3127052a992a6679379f78813208e)
備注:還有很多沒講到,例如tag,但我感覺現(xiàn)有的東西已經(jīng)足夠用了,以后需要再去查資料吧。