Git常用命令
参考
https://www.cnblogs.com/chenwolong/p/GIT.html
https://www.cnblogs.com/PeunZhang/p/5957211.html
git常用配置
git配置查看
git config -l
git别名
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
设置commit模板
git config --global commit.template=模板路径
用户信息
git config --global user.name "Your Name"
git config --global user.email "yourname@tp-link.com.cn"
默认文本编辑器
vim/nano/vscode
git config --global core.editor vim
git config --global core.editor 'code --wait'
忽略文件权限
一般情况我们不需要关心文件权限修改
git config --global core.filemode false
中文编码
在默认情况下如果文件名是中文,会显示形如 274\232\350\256\256\346\200\273\347\273\223 的乱码。
可以通过git配置改变默认转义
git config --global core.quotepath false
设置提交时使用 UTF-8 编码,可避免服务器上乱码,同时与 Linux 上的提交保持一致。
git config --global i18n.commitencoding utf-8
命令行着色
命令行输出使用着色
git config --global color.ui true
删除全局配置
git config --global --unset color.ui
基本命令
# 初始化本地仓库
git init
# 克隆一个仓库
git clone [url]
# 添加到暂存区
git add .
# 提交到本地版本库
git commit -m 'first commit'
# 提交到远程仓库
git push -u origin master
# 查看远程仓储地址是否正确
git remote -v
# 列出所有本地分支
git branch
# 列出所有远程分支
git branch -r
# 列出所有本地分支和远程分支
git branch -a
# 指定提交新建一个分支,但依然停留在当前分支,没有commit_id在当前提交新建分支
git branch [branch] [commit]
# 指定提交新建一个分支,并切换到该分支,没有commit_id在当前提交新建分支
git checkout -b [branch_name] [commit_id]
# 切换分支,并更新工作区
git checkout [branch_name]
# 删除本地分支
git branch -d [branch_name]
# 强制删除本地分支(未被合并的分支被删除的时候需要强制)
git branch -D [branch_name]
# 列出所有标签
git tag
# 新建一个tag在指定commit_id,无commit_id在当前提交创建tag
git tag [tag_name] [commit_id]
# 删除本地tag
git tag -d [tag_name]
# 查看tag信息
git show [tag_name]
# 新建一个分支,指向某个tag
git checkout -b [branch_name] [tag_name]
# 显示有变更的文件
git status
# 显示当前分支的提交历史
git log --oneline
# 显示指定文件相关的每一次diff
git log -p [file]
# 显示指定文件的修改人和修改时间
git blame [file]
# 显示当前分支的HEAD最近几次变化历史
git reflog
# 下载远程仓库的所有变动
git fetch
# 显示所有远程仓库
git remote -v
# 下载远程仓库的所有变动与本地合并
git pull = git fetch + git merge
git pull --rebase = git fetch + git rebase
git pull --rebase origin [remote_branch]
# 上传评审
git push origin HEAD:refs/for/<branch_name>
# 撤销工作区修改
git restore [file]
# 撤销暂存区到工作区
git restore --unstage [file]
# 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
git reset --hard [commit_id]
# 重置当前分支的HEAD为指定commit,暂存区和工作区不变
git reset --soft [commit_id]
# 撤销某个提交,新得到的提交将前者的提交修改完全抵消
git revert [commit_id]
# 暂存修改到当前提交
git stash save [stash_name]
# 查看暂存列表
git stash list
# 恢复暂存修改
git stash pop
.gitignore
有些文件我们不想让它们被纳入版本控制,比如 gcc 编译生成的 .o 和可执行程序。可以使用.gitignore文件来过滤。
.gitignore 文件不要求和 .git 目录同级,在 Project 下面的子目录存放都可以。GitHub 有一个十分详细的针对数十种工程及语言的 .gitignore 文件列表,可以参考。
gitignore: A collection of useful .gitignore templates
基本操作
如何撤销上次提交
如果提交已经推送远程库,下方命令撤销,然后推送到远程
git revert HEAD
如果还没有推送到远程话,撤销方法
# 丢弃上次commit内容
git reset --hard HEAD^
# 上次提交保留在暂存区
git reset --soft HEAD^
版本回滚
回滚工作区和暂存区到指定的提交:
$ git reset --hard <commit>
回滚到之前的提交,暂存区也恢复到此提交,但是保留工作区不变:
$ git reset <commit>
回滚到之前的提交,但是保留暂存区和工作区不变:
$ git reset --soft <commit>
git reset --soft
可以实现合并提交,reset之后从新commit
如何修改上次提交
修改后,添加暂存区,修补提交,推送远程
git commit --amend
该命令也可以修改提交的说明,即git commit -m的信息
git pull和git fetch的区别
git fetch 获取远程版本库的 Git 对象放在本地,并更新本地版本库中的远程分支的指向,并不会将远程分支和跟踪远程分支的本地分支合并。
而 git pull 会合并,如果加上 --rebase 选项,则是以 rebase 取代merge。
git pull = git fetch + git merge
git pull --rebase = git fetch + git rebase
所以,当你只想看看远程有什么新的提交时,执行 git fetch,如果想和本地衍合,则执行 git pull --rebase 。
撤销修改
撤销工作区修改
git restore <file>
git add后,修改在暂存区,撤销暂存区修改
git restore --unstage <file>
# 同样效果
git reset <file>
怎么给本地分支重命名?
使用如下命令:
$ git branch -m <oldname> <newname>
如何删除本地未纳入版本控制的文件
-n选项显示要删除的文件
git clean -f -n
真正删除,不带-n选项
git clean -f
如果连文件夹一起删除,加上-d选项
git clean -fd
如果连被忽略的文件也一起删除,加上-x选项
git clean -fdx
如何解决冲突
在Gerrit上整理提交链时,很容易出现冲突。这时候先reset本地到父提交,然后cherry-pick子提交解冲突,然后将修改提交到暂存区,再执行继续拣选提交,最后push到远程即可
git reset 父提交
git cherry-pick 子提交
git cherry-pick --continue
git push
跟踪远程分支
创建本地分支跟踪远程分支
git checkout -b <branch-name> origin/<branch-name>
同步远程分支提交到本地
如果当前分支与远程分支存在跟踪关系,git pull 就可以省略远程分支名。
git pull --rebase
如果没有建立跟踪关系,那么就需要将参数填写完整
git pull --rebase origin <remote_branch>:<local_branch>
上传评审
git push origin HEAD:refs/for/<branch_name>
如何丢弃本地分支所有的的修改和提交
假设在 master 分支上工作,只需执行
git reset --hard origin/master
如何将一个空文件纳入版本控制
Git 不能将空文件夹纳入版本控制,必须在文件夹里放点什么。
典型的做法是放一个 .gitignore 文件。如果要令放在此文件夹中的文件都不会被纳入版本控制,那么 .gitignore 文件的内容为
# Ignore everything in this directory
*
# Except this file
!.gitignore
git reflog—查看Git提交历史
git reflog记录了所有对本地仓库的更贵,包括切换分支、合并、提交、撤销等操作。即使你执行了 git reset 或 git rebase 之类的操作,这些变更也会被记录在内。
最常见的用途是在意外丢失提交或 HEAD 指针时,帮助你找回丢失的内容
查看某个文件的提交历史
使用如下命令查看某个文件的提交历史,即便这个文件被删除了也可以看到
git log -- [filename]
git rebase整理提交
git rebase -i 5c6eb73^ /* 整理从此到最新的提交历史 */
git add .
git commit --amend
git rebase --continue
git rebase --abort /* 衍合过程中 */
stash 指定名称
git stash save "stash_name"
搜索提交历史记录
git log --grep="特定字符串"
搜索文件内容特定字符串
git grep "特定字符串" 文件路径
git grep "特定字符串" /* 搜索全部文件 */
定位修改历史
git blame 文件路径 | grep "特定字符串"
推送Gerrit缺少Change-Id
ERROR: commit b007456: missing Change-Id in message footer
提醒你执行的类似下方命令
gitdir=$(git rev-parse --git-dir);scp -p -P 29418 wangxinzhi@review.tp-link.net:hooks/commit-msg${gitdir}/hooks/
如果提示失败,需要把scp -p -P改成scp -O -P执行
gitdir=$(git rev-parse --git-dir);scp -O -P 29418 wangxinzhi@review.tp-link.net:hooks/commit-msg${gitdir}/hooks/
然后执行如下命令,再push就没问题了
git commit --amend --no-edit
git push origin HEAD:refs/for/<remote_branch>
效率问题
Git 能不能只单独克隆一个分支
可以
git clone -b <branchname> --single-branch <url>
每次提交前需要 add 操作比较繁琐,有什么办法简化
对本地所有变更的文件执行提交操作
git commit -a -m "commit-msg"
但是 -a 选项最好不要使用,这样会丢掉 Git 暂存区带给用户的最大好处:对提交内容进行控制的能力。
批量 cherry-pick
先切换到要cherry-pick到的分支
git checkout <target_branch>
git cherry-pick <start-commit>^ <end-commit>
repo相关
切换到其他的manifest重新下载代码
方法一:重新输入repo init命令
repo init -u ssh://wangxinzhi@autoreview.rd.tp-link.net:29418/tpds/manifest -b master -m tpds-tdmp-intergration.xml
repo sync
方法二:软连接新的manifest
rm .repo/manifest.xml
ln -s .repo/manifests/xxx.xml .repo/manifest.xml
repo sync
repo sync 出现 object is corrupted 错误,怎么解决
出错提示形如:
Fetching projects: 2% (5/231)
fatal: object 26cc119dba8cca384493bde22b67ae99c84ebe14 is corrupted
error: Cannot fetch path/to/project
如果在 path/to/project 没有做过提交,可以删掉工作区和本地版本库,重新 repo sync。相关命令:
$ rm -rf .repo/projects/path/to/project.git/
$ rm -rf path/to/project
$ repo sync
如果已经有提交了,那么把代码备份一下,或者导出为 patch, 再进行上述操作
repo sync 时出现 data stream error 错误,是怎么回事
出错提示形如:
error: inflate: data stream error (incorrect date check)
fatal: serious inflate inconsistency
fatal: index-pack failed
请检查磁盘空间是否满了。

浙公网安备 33010602011771号