Git 常用命令

1. 创建全新仓库

1
2
3
4
# 在当前目录创建一个新的 Git 代码仓库
git init
# 新建一个目录,将其初始化为代码库
git init [project]

2. 克隆远程仓库

1
2
#克隆远程仓库项目和他的整个代码历史记录(版本信息)
git clone [url]

3. 查看文件状态

1
2
3
4
5
#查看指定文件状态
git status [filename]

查看所有文件状态
git status

4. 暂存 提交

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
#添加指定文件到暂存区
git add [file1] [file2] ...

#添加指定文件夹到暂存区,包括子目录
git add [dir]

#添加当前目录的所有文件到暂存区
git add .

#提交暂存区到本地仓库
git commit -m "commit_message"

#提交暂存区的指定文件到本地仓库区
git commit [file1] [file2] ... -m [message]

#提交工作区自上次commit之后的变化,直接到仓库区,跳过了add,对新文件无效
git commit -a

#提交时显示所有diff信息
git commit -v

# 使用一次新的commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次commit的提交信息
git commit --amend -m [message]

# 重做上一次commit,并包括指定文件的新变化
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
#查看本地分支
git branch
#查看远程版本库的分支列表
git branch -r
#查看所有分支列表,包括本地和远程
git branch -a
#查看本地分支对应的远程分支
git branch -vv


#根据当前分支创建指定分支,创建完成依然停留在当前分支上
git branch [name]


#删除本地分支
git branch -d [name]

#强制删除本地分支
git branch -D [name]

#删除远程分支
git push origin --delete [name]

6. 切换分支

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#放弃单个文件的修改
git checkout [fileName]

#放弃当前目录的修改
git checkout .

# 签出到指定分支
git checkout [name]

# 根据当前分支创建并签出到此分支
git checkout -b [name]

#切换到指定分支
git switch [name]

7. 拉取 推送远程分支

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#将与本地当前分支同名的远程分支 拉取到 本地当前分支上(需先关联远程分支)
git pull

#将远程指定分支 拉取到 本地当前分支上
git pull origin [name]

#将本地当前分支 推送到 与本地当前分支同名的远程分支上 (需要先关联远程分支)
git push

#将本地指定分支 推送到 与本地当前分支同名的远程分支上
git push origin [branch_name]

#本地代码强制推送覆盖到远程
git push -f origin <远程分支名>

#将本地分支与远程同名分支相关联
git push -u origin [本地分支名]

8. 标签 tag

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
#查看当前仓库所有标签
git tag

#查看所有标签及说明
git tag -n

#搜索符合条件的标签
git tag -l "1.0.*"

# 查看指定标签具体信息
git show [标签名]

#创建标签
git tag "[标签名]"

#创建带有说明的标签
git tag -a [标签名] -m "[说明文字]"

#给指定的commit打标签
git tag -a "[标签名]" [commitID]

#推送指定标签到远程
git push origin [标签名]

#推送全部本地标签到远程
git push origin --tags

#删除本地标签
git tag -d "[标签名]"

#删除远程标签
git push origin --delete tag [标签名]

9. 版本回退

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#查看已提交日志
git log

#查看所有提交log,显示对应版本号
git reflog

#回退到上一次提交
git reset --hard HEAD^

#回退到某一分支,不保留后续更改
git reset --hard <分支id>

#回退到某一分支,保留后续更改为待暂存状态
git reset --mixed <分支id>

#回退到某一分支,保留后续更改为待提交状态
git reset --soft <分支id>

10. 工作区暂存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#查看stash了哪些存储
git stash list

#执行存储时,添加备注,方便查找,只有git stash 也要可以的,但查找时不方便识别。
git stash save "暂存说明"

#应用某个存储,但不会把存储从存储列表中删除,默认使用第一个存储,即stash@{0}
git stash apply
#如果要使用其他个,git stash apply stash@{$num} , 比如第二个:git stash apply stash@{1}
git stash apply stash@{$num}

#命令恢复之前缓存的工作目录,将缓存堆栈中的对应stash删除,并将对应修改应用到当前的工作目录下,默认为第一个stash,即stash@{0}
git stash pop
#如果要应用并删除其他stash,命令:git stash pop stash@{$num} ,比如应用并删除第二个:git stash pop stash@{1}
git stash pop stash@{$num}

#丢弃stash@{$num}存储,从列表中删除这个存储
git stash drop stash@{$num}

#删除所有缓存的stash
git stash clear

文件的四种状态

- Untracked: 未跟踪, 此文件在文件夹中, 但并没有加入到git库, 不参与版本控制. 通过git add 状态变为Staged.
- Unmodify: 文件已经入库, 未修改, 即版本库中的文件快照内容与文件夹中完全一致. 这种类型的文件有两种去处, 如果它被修改, 而变为Modified. 如果使用git rm移出版本库, 则成为Untracked文件
- Modified: 文件已修改, 仅仅是修改, 并没有进行其他的操作. 这个文件也有两个去处, 通过git add可进入暂存staged状态, 使用git checkout 则丢弃修改过, 返回到unmodify状态, 这个git checkout即从库中取出文件, 覆盖当前修改
- Staged: 暂存状态. 执行git commit则将修改同步到库中, 这时库中的文件和本地文件又变为一致, 文件为Unmodify状态. 执行git reset HEAD filename取消暂存, 文件状态为Modified