Git常用命令总结

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Git仓库的创建

  • 在当前目录新建一个Git仓库
1
2
3
4
5
# 命令形式:git init

$ cd TestGit
$ git init
Initialized empty Git repository in /Users/***/TeskGit/.git/
  • 在指定目录下新建一个Git仓库
1
2
3
4
# 命令形式:git init [Directory Name]

$ git init TestGit
Initialized empty Git repository in /Users/***/TeskGit/.git/
  • 从Github等地方克隆一个仓库到当前目录(可能需要输入密码,以ssh方式克隆),如果不指定分支,则默认从Refactor仓库的Default branch(一般为master)克隆
1
2
3
4
5
6
7
8
9
10
# 命令形式: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. 关联远程仓库

  • 将本地仓库与远程仓库关联,首先要初始化一个本地仓库,url可以在github的仓库获得,origin为远程仓库在本地的别名,即origin=git@github.com:*/Refacor.git
1
2
3
# 命令形式:git remote add [remote repository aliase] [url]

$ git remote add origin git@github.com:***/Refacor.git
  • 查看本地仓库关联的远程仓库
1
2
3
4
5
6
7
# 命令形式:git remote [-v]

$ git remote
origin
$ git remote -v
origin git@github.com:***/Refacor.git (fetch)
origin git@github.com:***/Refacor.git (push)
  • 删除本地仓库关联的远程仓库
1
2
3
4
# 命令形式:git remote rm [remote repository aliase]

$ git remote rm origin
$ git remote
  • 关联远程仓库后,从远程仓库取内容,并根据远端仓库在本地创建了两个分支,master和develop
1
2
3
4
5
6
7
8
9
# 命令形式: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
  • 显示远程仓库的信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 命令形式: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分支的创建、切换、删除、关联

  • 创建本地分支并切换到新创建的分支,-b代表新创建一个分支,-B用在本地分支已经存在的情况下,强行创建一个新分支并将原来的分支覆盖,checkout主要是分支切换,创建分支使用branch命令
1
2
3
4
5
6
7
8
# 命令形式: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'
  • 切换本地分支
1
2
3
4
# 命令形式:git checkout [local branch name]

$ git checkout develop
Switched to branch 'develop'
  • 查看当前所在的本地分支,创建本地分支,local branch name不存在时是查看当前所在的本地分支,存在时是创建一个新的本地分支
1
2
3
4
5
6
7
8
# 命令形式:git branch [local branch name]

$ git branch
* a
develop
master

$ git branch b
  • 查看远程分支和所有分支,-r是查看远程分支,-a是查看所有分支
1
2
3
4
5
6
7
8
9
10
# 命令形式:git branch [-r] [-a]

$ git branch -r
origin/develop
origin/master
$ git branch -a
develop
* master
remotes/origin/develop
remotes/origin/master
  • 删除本地分支
1
2
3
4
# 命令形式:git branch -d [local branch name]

$ git branch -d test
Deleted branch test (was 0b05e43).
  • 创建本地分支并与远端分支关联
1
2
3
4
5
6
7
8
9
# 命令形式: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'
  • 关联远程分支
1
2
3
4
# 命令形式: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]是删除本地分支与远程分支的关联关系。
1
2
3
4
5
6
7
8
# 命令形式: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).

注:本地可以有多个分支,远程也可以有多个分支,本地多个分支可以关联远程多个分支,但是,本地分支最好与远程分支同名,以免出现问题。

4.从远程仓库取内容,向远程仓库提交内容

向远程仓库提交内容之前,要理解三个概念:本地文件,缓冲区,本地仓库。平常修改的文件内容都是本地文件,在往远程仓库提交之前先要提交到缓冲区,再从缓冲区提交到本地仓库,然后本地仓库才能往远程仓库提交。本地的内容分为三大部分,它们是相互独立的,理解了这四个概念就明白为什么要执行git add,git commit,git push命令了。

  • 从远程仓库取内容,git fetch默认情况下是当前的本地分支从其关联的远程分支上取内容,可以使用git branch先查看当前的本地分支,使用git status(包括本地分支和远程分支都能看到)查看其关联的远程分支。从下面的例子中可以看出本地分支为develop,远程分支也为develop
1
2
3
4
5
6
7
8
9
10
# 命令形式: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
  • 将从远程仓库取下的内容与本地仓库的内容进行合并,合并之前必须将本地的修改提交到本地仓库。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 命令形式: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

# 本地仓库与取下的远程仓库内容有冲突的情况下
$ git merge
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
  • 解决冲突,冲突是使用git时常常会碰到的情况,冲突解决主要是在将本地仓库内容与远程仓库取下的内容merge后,进入merge后的文件进行修改,将冲突解决,然后重新add,commit,push即可。

冲突内容:

1
2
3
4
5
6
# Refacor
<<<<<<< HEAD
branch develop
=======
branch
>>>>>>> refs/remotes/origin/develop

修改后的内容:

1
2
# Refacor
branch develop

处理过程:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ 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
  • 直接从远程仓库取内容并合并
1
2
3
4
5
6
7
8
9
10
11
# 命令形式: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,因为许多细节都看不到。

  • 将本地文件修改提交到缓冲区,git add filename是将单个文件提交到缓冲区,可以提交多个文件,中间用空格分开,也可提交目录,git add . 是将所有修改内容提交到缓冲区
1
2
3
4
# 命令形式:git add [filename1] [filename2] or [directoryname] or [.]
$ git add README.md
$ git add README.md a.txt
$ git add .
  • 将缓冲区内容提交到本地仓库
1
2
3
4
5
6
# 命令形式: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"
  • 将本地仓库内容提交到远程仓库,默认情况下,如果你的本地分支与远程分支同名且关联,git push就可以,但如果不是你需要加上更多的东西。我本地仓库有master和develop分支,远程仓库有master和develop分支,我本地仓库的develop分支与远程仓库的master分支关联。所以直接git push是不可以的。最好不要这么干,我只是测试一下。下面的另一个例子是我将本地仓库的develop分支与远程仓库的develop分支关联并提交修改到远程仓库。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 命令形式: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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ 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的所有配置信息
1
2
# 命令形式:git config --list
$ git config --list
  • 查看特定的配置信息
1
2
3
4
5
6
7
# 命令形式:git config --get [variable]

$ git config --get user.name
test

$ git config --global --get user.email
global@test.com
  • 配置用户名和邮件,加上–global是配置全局的,否则是配置当前仓库的,当前仓库的配置会覆盖全局配置
1
2
3
4
5
6
7
8
9
# 命令形式: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. 查看提交记录

  • 查看提交记录,当前分支的版本历史
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#命令形式: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发生变更的文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 命令形式: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 +-
  • 查看最近的操作信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 命令形式: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
  • 显示缓存区和工作区的差异
1
2
3
4
5
6
7
8
9
10
# 命令形式: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. 切换分支修改内容

有时候你正在某个分支上进行开发,突然来了一个任务要修改另一个分支上的内容,而此时你还不想将当前分支的内容提交,git stash(保存当前的工作状态)命令就有了用处。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 查看当前工作状态
$ 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是保存当前的工作进度。会分别对暂存区和工作区的状态进行保存,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

# 切换分支,查看状态,修改当前分支内容
$ 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中的内容,然后继续开发develop
# 命令形式:git stash pop stash@{0},git stash pop是恢复stash中的第一个的内容,有多个时也可以添加索引,形式为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)

8. 查看本地代码与远程仓库代码之间的差异

1
2
3
4
5
6
7
* git diff 查看尚未加入缓冲区的文件与远程仓库代码的差异

* git diff filename 查看尚未加入缓冲区的指定文件与远程仓库代码的差异

* git diff –-cached 查看已经暂存起来的文件和上次提交的版本之间的差异

* git diff -–cached filename 查看已经暂存起来的某个文件和上次提交的版本之间的差异

9. 代码回滚

  • 缓冲区的代码回滚

所谓缓冲区代码回滚就是将已经add的代码,即缓冲区代码重新移到工作空间中,主要用来有的代码不想提交的情况下。

1
2
3
4
5
6
# 命令形式:git reset HEAD filename

$ git reset HEAD .project
Unstaged changes after reset:
M .project
M src/main/resources/application.yml
  • commit代码回滚
1
2
3
4
# 命令形式:git reset --head commit_id

$ git reset --hard 5d9cdf3e53bfbd0
HEAD is now at 5d9cdf3 Merge branch 'develop' of

10. 放弃修改

下面的修改都是未提交到缓冲区的修改

  • 放弃修改与远程仓库一致
1
2
3
4
# 命令形式:git reset --hard origin/branch_name

$ git reset --hard origin/develop
HEAD is now at b680879 feat: sms send module
  • 放弃修改与本地仓库一致
1
2
3
# 命令形式:git reset --hard HEAD

$ git reset --hard HEAD
  • 放弃某个文件的修改
1
2
3
# 命令形式:git checkout -- filename

$ git checkout -- .gitignore

备注:还有很多没讲到,例如tag,但我感觉现有的东西已经足够用了,以后需要再去查资料吧。

11. 添加多个远程仓库,并解决merge的冲突

1
2
3
4
$ git remote add origin git@github.com:***/A.git
$ git remote add upsteam git@github.com:***/B.git

$ git pull origin master --allow-unrelated-histories
如果有收获,可以请我喝杯咖啡!