Git Cheat Sheet——Git的常用命令和最佳做法

一、常见命令

  1、创建

      克隆现有的存储库

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

      创建新的本地存储库

$ git init 

  2、本地变化

  更改工作目录中的文件

$ git status 

  对跟踪文件的更改

$ git diff 

  将所有当前更改添加到下一次提交

$ git add .  

  将< file >中的一些更改添加到下一次提交

$ git add -p <file> 

  提交跟踪文件中的所有本地更改

$ git commit -a 

  提交先前阶段的更改

$ git commit 

  更改最后提交
  不要修改发布的提交!

$ git commit --amend  

  3、提交历史

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

$ git log 

  显示特定文件随时间的变化

$ git log -p <file>  

  谁在< file >中更改了内容和时间?

$ git blame <file> 

  4、分支和标签

  列出所有现有分支

$ git branch -av  

  切换分支

$ git checkout <branch>  

  根据当前的头部创建一个新分支

$ git branch <new-branch> 

  基于远程分支创建新的跟踪分支

$ git checkout --track <remote/bran- ch>

  删除本地分支

$ git branch -d <branch> 

  提交标签

$ git tag <tag-name> 

  5、更新和发布

  列出所有当前配置的远程主机

$ git remote -v   

  显示有关远程

$ git remote show <remote>   

  添加名为< Remote >的新远程存储库

$ git remote add <shortname> <url> 

  从< Remote >下载所有更改,但不要集成到Head中

$ git fetch <remote>  

  下载更改并直接合并/集成到头中

$ git pull <remote> <branch>   

  在远程上发布本地更改

$ git push <remote> <branch>

  删除远程上的分支

$ git branch -dr <remote/branch> 

  发布标签

$ git push --tags

  6、合并和重基

  将<分支>合并到当前的头部

$ git merge <branch> 

  将当前的头重新定位到<分支>
  不要重新发布已发布的提交!

$ git rebase <branch>   

  中止重基

$ git rebase --abort 

  解决冲突后继续重基

$ git rebase --continue   

  使用配置的合并工具解决冲突

$ git mergetool  

  使用编辑器手动解决冲突,并(在解决后)将文件标记为“已解决”。

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

  7、撤销

  放弃工作目录中的所有本地更改。

$ git reset --hard HEAD  

  放弃特定文件中的本地更改。

$ git checkout HEAD <file> 

  还原一个提交(通过产生一个新的具有相反更改的提交)

$ git revert <commit>  

  将头指针重置为上一次提交
       …并放弃自那以后的所有变化 

$ git reset --hard <commit>    

  …并将所有更改保留为未分阶段的更改。

$ git reset <commit>   

  …并保存未提交的本地更改。

$ git reset --keep <commit>   

  

原文地址:https://www.cnblogs.com/windok/p/12843702.html