- git核心概念
- 整体流程
- 分析
- 命令大全
git核心概念
- 工作目录:项目目录;
- 暂存区:准备下次提交的文件列表;
- 仓库:存储项目历史记录的地方,可以是本地的,也可以是远程服务器上的;
- 本地仓库:本地电脑存储历史记录的区域;
- 远程仓库:托管在服务器上的代码副本;
整体流程
基础操作
1. 创建仓库
使用当前目录作为Git仓库:
该命令执行完后会在当前目录生成一个.git目录。
2. 新建本地文件
本地增加文件url.c、README.md,使用git status查看文件状态,有2个未跟踪的在文件

3. 提交文件到暂存区
添加所有文件到暂存区。
4. 提交改动
1
| $ git commit -m "代码提交信息"
|

新增的两个文件,已经提交到HEAD,但是还没到远端仓库。
5. 推送到远端仓库
1
| $ git push origin master
|
如果你还没有克隆现有仓库,并欲将你的仓库 连接到某个远程服务器 ,你可以使用如下命令添加:
1
| git remote add origin <server>
|
这样就可以将改动推送到所添加的服务器上了。
分支操作
分支是用来将特性开发绝缘开来的,在创建仓库的时候,master时默认的分支。
在其他分支上进行开发,完成后再将它们合并到主分支上。
1. 查看分支
没有参数时,git branch会列出在本地的分支。
2. 创建分支
1
| $ git branch new-branch-name
|

- 多了一个分支test
- 当前分支为主分支master,前面的*表示活动分支
3. 切换分支
刚刚的branch命令只是单纯的创建一个新分支。使用checkout命令来更改分支:
或者使用以下命令,创建一个分支并同时切换过去(相当于上面两步合在一起):
4. 分支合并
在分支test上进行修改并提交,当分支任务完成后,切换到主分支并合并:
1 2
| $ git checkout master $ git merge test
|

- 合并之后可以看到,主分支多了一个新的commit
- 对其他分支的更改不会反映在主分支上。如果想将更改提交到主分支,则需切换回master分支,然后使用合并。
5. 删除分支
1 2
| git branch -d test git branch -D test
|
替换本地改动
如果操作失误,可以使用如下命令替换掉本地改动:
1
| $ git checkout <filename>
|
此命令会使用HEAD中的最新内容替换掉当前工作目录中的文件,已添加到暂存区的改动以及新文件不会受到影响。

当我修改了一个文件,该文件还没有提交到暂存区,我想撤回修改,则可以执行该命令。
假如想丢弃在本地的所有改动与提交,可以到服务器上获取最新的版本历史,并将本地主分支指向它:
1 2
| $ git fetch origin $ git reset --hard origin/master
|
重置
当我们不想要之前提交的修改时,就会用到这个命令,比如一个错误的提交或者引入一个bug的提交,这个时候就可以使用命令:git reset
,它可以让我们不再使用当前台面上的文件,让我们可以控制HEAD应该指向的位置。
1. 软重置
软重置会将HEAD移动至指定的提交,而不会移除该提交之后加入的修改;

通过 git status
可以看到,仍然可以访问到在之前提交上做过的修改,可以修复这些文件,之后再重新提交。
2. 硬重置
有时候我们并不想保留特定提交引入的修改,且再也不需要访问他们了,Git应该直接将整体状态直接重置到特定提交之前的状态:包括在工作目录中和暂存文件上的修改。

可以看到所有提交的信息都已经被移除了,工作区文件恢复到恢复到特定状态,打开文件可以看到所有添加的信息都不存在了。
分析
1 2 3 4 5 6 7 8
| $ git branch * station-computer
$ git branch -a * station-computer remotes/origin/master remotes/origin/station-computer remotes/origin/zhm-old-computer
|
当前本地仓库只有一个名为 station-computer
的分支,并且处于激活状态;
远程仓库 origin
有三个分支:master
,station-computer
,zhm-old-computer
;
1. 为什么 git branch
只显示一个本地分支?
git branch
命令默认只显示本地分支,在当前仓库中,只有一个本地分支 station-computer
;
2. 如何操作其他远程分支?
操作远程 master
分支
创建并切换到本地 master
分支
1
| $ git checkout -b master origin/master
|
这条命令会在本地创建一个新的 master
分支,并将其与远程 master
分支建立跟踪关系,然后切换到该分支。
拉取远程 master
分支的最新变更
1
| $ git push origin master
|
这条命令会拉取远程 master
分支的最新变更并合并到本地 master
分支。
操作远程 zhm-old-computer
分支
创建并切换到本地 zhm-old-computer
分支
1
| $ git checkout -b zhm-old-computer origin/zhm-old-computer
|
这条命令会在本地创建一个新的 zhm-old-computer
分支,并将其与远程的 zhm-old-computer
分支建立跟踪关系,然后切换到该分支。
拉取远程 zhm-old-computer
分支的最新变更
1
| $ git pull origin zhm-old-computer
|
这条命令会拉取远程 zhm-old-computer
分支的最新变更并合并到本地 zhm-old-computer
分支。
3. 其他操作
- 删除远程分支
1 2
| $ git push origin --delete master $ git push origin --delete zhm-old-computer
|
命令大全
1. 仓库
1 2 3 4 5 6 7 8
| $ git init
$ git init [project-name]
$ git clone [url]
|
2. 配置
1 2 3 4 5 6 7 8 9
| $ git config --list
$ git config -e [--global]
$ git config [--global] user.name "[name]" $ git config [--global] user.email "[email address]"
|
3. 增加/删除文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| $ git add [file1] [file2] ...
$ git add [dir]
$ git add .
$ git add -p
$ git rm [file1] [file2] ...
$ git rm --cached [file]
$ git mv [file-original] [file-renamed]
|
4. 代码提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| $ git commit -m [message]
$ git commit [file1] [file2] ... -m [message]
$ git commit -a
$ git commit -v
$ git commit --amend -m [message]
$ git commit --amend [file1] [file2] ...
|
5. 分支
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
| $ git branch
$ git branch -r
$ git branch -a
$ git branch [branch-name]
$ git checkout -b [branch]
$ git branch [branch] [commit]
$ git branch --track [branch] [remote-branch]
$ git checkout [branch-name]
$ git checkout -
$ git branch --set-upstream [branch] [remote-branch]
$ git merge [branch]
$ git cherry-pick [commit]
$ git branch -d [branch-name]
$ git push origin --delete [branch-name] $ git branch -dr [remote/branch]
|
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 26
| $ git tag
$ git tag [tag]
$ git tag [tag] [commit]
$ git tag -d [tag]
$ git push origin :refs/tags/[tagName]
$ git show [tag]
$ git push [remote] [tag]
$ git push [remote] --tags
$ git checkout -b [branch] [tag]
|
7. 查看信息
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| $ git status
$ git log
$ git log --stat
$ git log -S [keyword]
$ git log [tag] HEAD --pretty=format:%s
$ git log [tag] HEAD --grep feature
$ git log --follow [file] $ git whatchanged [file]
$ git log -p [file]
$ git log -5 --pretty --oneline
$ git shortlog -sn
$ git blame [file]
$ git diff
$ git diff --cached [file]
$ git diff HEAD
$ git diff [first-branch]...[second-branch]
$ git diff --shortstat "@{0 day ago}"
$ git show [commit]
$ git show --name-only [commit]
$ git show [commit]:[filename]
$ git reflog
|
8. 远程同步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| $ git fetch [remote]
$ git remote -v
$ git remote show [remote]
$ git remote add [shortname] [url]
$ git pull [remote] [branch]
$ git push [remote] [branch]
$ git push [remote] --force
$ git push [remote] --all
|
9.撤销
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
| $ git checkout [file]
$ git checkout [commit] [file]
$ git checkout .
$ git reset [file]
$ git reset --hard
$ git reset [commit]
$ git reset --hard [commit]
$ git reset --keep [commit]
$ git revert [commit]
$ git stash $ git stash pop
|
10. 其他