常用git和repo命令

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/helaisun/article/details/79394687

常用git和repo命令


文章收集了最近使用的一些repo和git命令,下图是个人理解的git文件状态转换图。
这里写图片描述

相关概念

名称 意义
repo 谷歌用Python脚本写的调用git的一个脚本,把git仓库统一管理起来的工具
Gerrit 基于web的代码review及权限控制系统,面向基于 Git 版本控制系统
snapshot 快照,某个时间点的所有文件集合
commitID 40位哈希值,全球唯一
HEAD指针 指向当前工作分支
git分支 指向某一系列提交(commit)的指针或引用

svn与git命令的对比

svn git
checkout clone
update pull
comit push

git常用命令

git log         // 查看当前库的git log信息
git status ./   // 查看当前库的状态
git diff ./     // 比较当前库的修改情况
git add ./      // 将当前库的代码修改提交到暂存区
git commit ./   // 将代码提交到本地分支
git commit --amend ./   // 追加修改
git reset HEAD~1    // 将当前库恢复到HEAD的上一个版本

其他命令

git config -l // 参看配置信息
git show HEAD^ // 查看HEAD的上一个版本信息
git show HEAD~4 // 查看HEAD的上溯4代的信息
git reset --hard HEAD^^ // 回退两个版本
git reset --hard 8308f03 // 回退到指定的commitID前几位的版本

git remote -v //  参看远程仓库
git branch -a // 参看远程分支
git log --oneline --decorate --graph --all // 图像显示git log信息
git log --pretty=format:"%h - %cd %s" --graph // 列出指定格式的log
git log -since="2 weeks ago" // 显示2周前到现在所有的历史记录

远程同步

# 下载远程仓库的所有变动
$ 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

撤销

# 恢复暂存区的指定文件到工作区
$ git checkout [file]

# 恢复某个commit的指定文件到暂存区和工作区
$ git checkout [commit] [file]

# 恢复暂存区的所有文件到工作区
$ git checkout .

# 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
$ git reset [file]

# 重置暂存区与工作区,与上一次commit保持一致
$ git reset --hard

# 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
$ git reset [commit]

# 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
$ git reset --hard [commit]

# 重置当前HEAD为指定commit,但保持暂存区和工作区不变
$ git reset --keep [commit]

# 新建一个commit,用来撤销指定commit
# 后者的所有变化都将被前者抵消,并且应用到当前分支
$ git revert [commit]

# 暂时将未提交的变化移除,稍后再移入
$ git stash
$ git stash pop

repo常用命令

repo init -u URL -b ........ // 创建.repo 

repo upload // 将代码提交到gerrit.

repo abandon master // 放弃master分支

repo forall -c "git reset --hard HEAD" // 所有代码执行git命令,回退到HEAD

// repo sync相当于git clone会把repository中的所有内容拷贝到本地,非首次运行repo sync相当于更新和合并.
// repo sync会更新.repo下面的文件,如果在merge的过程中出现冲突,这需要手动运行git rebase --continue.
repo sync -c -j 4 

repo start master --all // 创建新分支

其他帮助参考

repo 使用的更详细参考:
常用 Git 命令清单——阮一峰
Repo的理解及用法小结(1)
Repo的理解及用法小结(2)
repo
Git和Repo管理使用简要介绍

git使用的更详细参考: 可以百度“Pro Git 中文版”这个很不错的资料。

另外可以利用工具自带的帮助功能查询常用命令:

repo 
repo help
git help
git help [command]

猜你喜欢

转载自blog.csdn.net/helaisun/article/details/79394687