优秀的Git命令清单

一. 新建代码库


  1. # 在当前目录新建一个Git代码库  
  2. $ git init  
  3. # 新建一个目录,将其初始化为Git代码库  
  4. $ git init [project-name]  
  5. # 下载一个项目和它的整个代码历史  
  6. $ git clone [url] 

二.配置

Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)


  1. # 显示当前的Git配置  
  2. $ git config –list   
  3. # 编辑Git配置文件  
  4. $ git config -e [–global]  
  5. # 设置提交代码时的用户信息  
  6. $ git config [–global] user.name "[name]"  
  7. $ git config [–global] user.email "[email address]"  
  8. # 颜色设置  
  9. git config –global color.ui true                         # git status等命令自动着色  
  10. git config –global color.status auto  
  11. git config –global color.diff auto  
  12. git config –global color.branch auto  
  13. git config –global color.interactive auto  
  14. git config –global –unset http.proxy                    # remove  proxy configuration on git 

三. 增加/删除文件


  1. # 添加指定文件到暂存区  
  2. $ git add [file1] [file2] …  
  3. # 添加指定目录到暂存区,包括子目录  
  4. $ git add [dir]  
  5. # 添加当前目录的所有文件到暂存区  
  6. $ git add .  
  7. # 添加每个变化前,都会要求确认  
  8. # 对于同一个文件的多处变化,可以实现分次提交 
  9. $ git add -p  
  10. # 删除工作区文件,并且将这次删除放入暂存区  
  11. $ git rm [file1] [file2] …  
  12. # 停止追踪指定文件,但该文件会保留在工作区  
  13. $ git rm –cached [file]  
  14. # 改名文件,并且将这个改名放入暂存区  
  15. $ git mv [file-original] [file-renamed] 

四. 代码提交


  1. # 提交暂存区到仓库区  
  2. $ git commit -m [message]  
  3. # 提交暂存区的指定文件到仓库区  
  4. $ git commit [file1] [file2] … -m [message]  
  5. # 提交工作区自上次commit之后的变化,直接到仓库区  
  6. $ git commit -a  
  7. # 提交时显示所有diff信息  
  8. $ git commit -v  
  9. # 将add和commit合为一步  
  10. $ git commit -am 'message'  
  11. # 使用一次新的commit,替代上一次提交  
  12. # 如果代码没有任何新变化,则用来改写上一次commit的提交信息  
  13. $ git commit –amend -m [message]  
  14. # 重做上一次commit,并包括指定文件的新变化  
  15. $ git commit –amend [file1] [file2] … 

五. 分支


  1. # 列出所有本地分支  
  2. $ git branch  
  3. # 列出所有远程分支  
  4. $ git branch -r  
  5. # 列出所有本地分支和远程分支  
  6. $ git branch -a  
  7. # 新建一个分支,但依然停留在当前分支  
  8. $ git branch [branch-name]  
  9. # 新建一个分支,并切换到该分支  
  10. $ git checkout -b [branch]  
  11. # 新建一个分支,指向指定commit  
  12. $ git branch [branch] [commit]  
  13. # 新建一个分支,与指定的远程分支建立追踪关系  
  14. $ git branch –track [branch] [remote-branch]  
  15. # 切换到指定分支,并更新工作区  
  16. $ git checkout [branch-name]  
  17. # 切换到上一个分支  
  18. $ git checkout – 
  19. # 建立追踪关系,在现有分支与指定的远程分支之间  
  20. $ git branch –set-upstream [branch] [remote-branch]  
  21. # 合并指定分支到当前分支  
  22. $ git merge [branch]  
  23. # 选择一个commit,合并进当前分支  
  24. $ git cherry-pick [commit]  
  25. # 删除分支  
  26. $ git branch -d [branch-name]  
  27. # 删除远程分支  
  28. $ git push origin –delete [branch-name]  
  29. $ git branch -dr [remote/branch]  
  30. # 检出版本v2.0  
  31. $ git checkout v2.0  
  32. # 从远程分支develop创建新本地分支devel并检出  
  33. $ git checkout -b devel origin/develop  
  34. # 检出head版本的README文件(可用于修改错误回退)  
  35. git checkout — README                                 

六. 标签


  1. # 列出所有tag  
  2. $ git tag  
  3. # 新建一个tag在当前commit  
  4. $ git tag [tag]  
  5. # 新建一个tag在指定commit  
  6. $ git tag [tag] [commit]  
  7. # 删除本地tag  
  8. $ git tag -d [tag] 
  9. # 删除远程tag  
  10. $ git push origin :refs/tags/[tagName]  
  11. # 查看tag信息  
  12. $ git show [tag]  
  13. # 提交指定tag  
  14. $ git push [remote] [tag]  
  15. # 提交所有tag  
  16. $ git push [remote] –tags  
  17. # 新建一个分支,指向某个tag  
  18. $ git checkout -b [branch] [tag] 
【声明】:芜湖站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

相关文章