Git 常用操作命令

老牛浏览 600评论 0发表于

此处只列出常用的操作命令,具体详细命令介绍及参数说明请参看 Git官方手册

1. 配置

1.1 设置用户名

bash
git config --global user.name "[your name]"

1.2 设置邮箱地址

bash
git config --global user.email "[email address]"

1.3 设置命令行显示为更易阅读的彩色

bash
git config --global color.ui auto

1.4 记住账号密码

如果使用 SSH 方式连接远端,可以用密钥来实现免密登录。可以参考:CentOS 7 搭建 Git 服务器

HTTPS 方式得用凭证系统来处理。

bash
git config --global credential.helper cache [--timeout <seconds>]
# cache 模式下凭证只会存放在内存中一段时间。密码永远不会被存储在磁盘中。
# 可以用 --timeout 指定过期时间,默认 15 分钟后从内存中清除。
bash
git config --global credential.helper store [--file <path>]
# store 模式下凭证会以明文的形式存放在磁盘中,并且永远不会过期。
# 可以用 --file 指定密码文件的存放路径,默认为 ~/.git-credentials。

.git-credentials 中存储格式为:

  • 一行存储一个账号密码

  • 每行的格式为 协议+用户名+:+密码+主机名/IP,如 https://user:password@domain.com,用户名和密码需要使用 urlencode 编码,比如含有 @ 的需要编码为 %40

2. 创建

2.1 克隆一个已存在的分支

2.1.1 通过 SSH

bash
git clone ssh://user@domain.com/repo.git

2.1.2 通过 HTTP

bash
git clone http://domain.com/user/repo.git

2.2 创建一个新的本地分支

bash
git init

3. 本地修改

3.1 显示工作路径下文件状态

bash
git status

3.2 显示已追踪文件的修改

bash
git diff

3.3 删除已追踪文件

取消追踪文件,并保留在本地:

bash
git rm --cached <file>

取消追踪目录,并保留在本地:

bash
git rm -r --cached <dir>  

取消追踪文件,并且删除本地文件:

bash
git rm --f <file>

3.4 把当前所有修改添加到下次提交中

bash
git add .

3.5 把对某个文件的修改添加到下次提交中

bash
git add <file>

3.6 提交本地所有已追踪文件的修改

bash
git commit -a

3.7 提交并带上说明

bash
git commit -m 'message here'

3.8 修改上次提交

不要修改已发布的提交记录!

bash
git commit -a --amend

3.9 把当前分支中未提交的修改暂存,并带到其他分支

bash
git stash
git checkout branch2
git stash pop

3.10 将最近一次暂存的工作进度恢复到当前分支

bash
git stash apply

3.11 将暂存的某个工作进度恢复到当前分支

{stash_number} 可以用命令 git stash list 获取

bash
git stash apply stash@{stash_number}

3.12 删除最近一次暂存的工作进度

bash
git stash drop

4. 搜索

4.1 从当前目录的所有文件中查找文本内容

bash
git grep "Hello"

4.2 在某个版本中搜索文本内容

bash
git grep "Hello" v2.5

5. 提交历史

5.1 从最新提交开始,显示所有提交记录

bash
git log

5.2 显示所有提交记录摘要信息

bash
git log --oneline

5.3 显示某个用户的所有提交

bash
git log --author="username"

5.4 显示某个文件的所有修改记录

bash
git log -p <file>

5.5 谁,在什么时候,修改了文件的什么内容

bash
git blame <file>

5.6 显示所有历史操作记录

bash
git reflog

5.7 删除所有历史操作记录

bash
git reflog delete

6. 分支与标签

6.1 列出所有本地分支

bash
git branch

6.2 列出所有本地/远程分支

bash
git branch -a

6.3 列出所有远程分支

bash
git branch -r

6.4 从不同分支检出某个文件

bash
git checkout <branch> -- <filename>

6.5 创建并切换到新分支

bash
git checkout -b <branch>

6.6 基于一个已存在的提交创建并检出一个新分支

bash
git checkout <commit-hash> -b <new-branch-name>

6.7 基于当前分支创建一个新分支

bash
git branch <new-branch>

6.8 基于远程分支创建新的可追溯的分支

bash
git branch --track <new-branch> <remote-branch>

6.9 删除本地分支

bash
git branch -d <branch>

6.10 重命名当前分支

bash
git branch -m <new-branch-name>

6.11 强制删除本地分支

将会丢失未合并的修改!

bash
git branch -D <branch>

6.12 给当前版本打标签

bash
git tag <tag-name>

6.13 给当前版本打标签并附上说明

bash
git tag -a <tag-name> -m 'message here'

7. 更新与发布

7.1 列出当前配置的远程仓库

bash
git remote -v

7.2 显示远程仓库信息

bash
git remote show <remote>

7.3 添加新的远程仓库

bash
git remote add <remote> <url>

7.4 拉取远程端内容,但不合并到当前分支

bash
git fetch <remote>

7.5 拉取远程端内容,并自动合并到当前分支

bash
git remote pull <remote> <url>

7.6 拉取远程端内容,并自动合并到当前分支

bash
git pull origin master

7.7 拉取远程端内容,并变基到当前分支

bash
git pull --rebase <remote> <branch>

7.8 将本地版本发布到远程端

bash
git push remote <remote> <branch>

7.9 删除远程端分支

bash
git push <remote> --delete <branch>

7.10 发布标签

bash
git push --tags

8. 合并

8.1 将指定分支合并到当前分支

bash
git merge <branch>

8.2 手动解决冲突后,将文件标记为「已解决冲突」

bash
git add <resolved-file>
bash
git rm <resolved-file>

9. 撤销

9.1 放弃工作目录下的所有修改

bash
git reset --hard HEAD

9.2 移除暂存区的所有文件(即撤销之前的 git add 操作)

bash
git reset HEAD

9.3 放弃某个文件的本地修改

bash
git checkout HEAD <file>

9.4 回到上次提交之前(通过新建一个与之相反的提交)

bash
git revert <commit>

9.5 将 HEAD 指针重置到之前的某个版本,并丢弃之后的所有修改

bash
git reset --hard <commit>

9.6 将 HEAD 指针重置到远程分支

bash
git reset --hard <remote/branch> e.g., upstream/master, origin/my-feature

9.7 将 HEAD 指针重置到之前的某个版本,并保留所有未暂存的修改

bash
git reset <commit>

9.8 将 HEAD 指针重置到之前的某个版本,并保留未提交的本地修改

bash
git reset --keep <commit>

9.9 删除添加 .gitignore 之前错误提交的文件

bash
git rm -r --cached .
git add .
git commit -m 'remove xyz file'
点赞
收藏
暂无评论,快来发表评论吧~
私信
老牛@ilaoniu
老牛,俗称哞哞。单纯的九零后理工小青年。喜欢折腾,爱玩,爱音乐,爱游戏,爱电影,爱旅游...
最后活跃于