git 命令总结(转)

结构图:


<1>

  • Workspace:工作区
  • Index / Stage:暂存区
  • Repository:仓库区(或本地仓库)
  • Remote:远程仓库

一、新建代码库

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

二、配置

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

  1.  
  2. # 显示当前的Git配置
  3. $ git config --list
  4.  
  5. # 编辑Git配置文件
  6. $ git config -e [--global]
  7.  
  8. # 设置提交代码时的用户信息
  9. $ git config [--global] user.name "[name]"
  10. $ git config [--global] user.email "[email address]"

三、增加/删除文件

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

四、代码提交

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

五、分支

  1.  
  2. # 列出所有本地分支
  3. $ git branch
  4. # 列出所有远程分支
  5. $ git branch -r
  6.  
  7. # 列出所有本地分支和远程分支
  8. $ git branch -a
  9.  
  10. # 新建一个分支,但依然停留在当前分支
  11. $ git branch [branch-name]
  12.  
  13. # 新建一个分支,并切换到该分支
  14. $ git checkout -b [branch]
  15.  
  16. # 新建一个分支,指向指定commit
  17. $ git branch [branch] [commit]
  18.  
  19. # 新建一个分支,与指定的远程分支建立追踪关系
  20. $ git branch --track [branch] [remote-branch]
  21.  
  22. # 切换到指定分支,并更新工作区
  23. $ git checkout [branch-name]
  24.  
  25. # 切换到上一个分支
  26. $ git checkout -
  27.  
  28. # 建立追踪关系,在现有分支与指定的远程分支之间
  29. $ git branch --set-upstream [branch] [remote-branch]
  30.  
  31. # 合并指定分支到当前分支
  32. $ git merge [branch]
  33.  
  34. # 选择一个commit,合并进当前分支
  35. $ git cherry-pick [commit]
  36.  
  37. # 删除分支
  38. $ git branch -d [branch-name]
  39.  
  40. # 删除远程分支
  41. $ git push origin --delete [branch-name]
  42. $ git branch -dr [remote/branch]

六、标签

  1.  
  2. # 列出所有tag
  3. $ git tag
  4.  
  5. # 新建一个tag在当前commit
  6. $ git tag [tag]
  7.  
  8. # 新建一个tag在指定commit
  9. $ git tag [tag] [commit]
  10.  
  11. # 删除本地tag
  12. $ git tag -d [tag]
  13.  
  14. # 删除远程tag
  15. $ git push origin :refs/tags/[tagName]
  16.  
  17. # 查看tag信息
  18. $ git show [tag]
  19.  
  20. # 提交指定tag
  21. $ git push [remote] [tag]
  22.  
  23. # 提交所有tag
  24. $ git push [remote] --tags
  25.  
  26. # 新建一个分支,指向某个tag
  27. $ git checkout -b [branch] [tag]

七、查看信息

  1.  
  2. # 显示有变更的文件
  3. $ git status
  4.  
  5. # 显示当前分支的版本历史
  6. $ git log
  7. # 显示commit历史,以及每次commit发生变更的文件
  8. $ git log --stat
  9.  
  10. # 搜索提交历史,根据关键词
  11. $ git log -S [keyword]
  12.  
  13. # 显示某个commit之后的所有变动,每个commit占据一行
  14. $ git log [tag] HEAD --pretty=format:%s
  15.  
  16. # 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件
  17. $ git log [tag] HEAD --grep feature
  18.  
  19. # 显示某个文件的版本历史,包括文件改名
  20. $ git log --follow [file]
  21. $ git whatchanged [file]
  22.  
  23. # 显示指定文件相关的每一次diff
  24. $ git log -p [file]
  25.  
  26. # 显示过去5次提交
  27. $ git log -5 --pretty --oneline
  28.  
  29. # 显示所有提交过的用户,按提交次数排序
  30. $ git shortlog -sn
  31.  
  32. # 显示指定文件是什么人在什么时间修改过
  33. $ git blame [file]
  34.  
  35. # 显示暂存区和工作区的代码差异
  36. $ git diff
  37.  
  38. # 显示暂存区和上一个commit的差异
  39. $ git diff --cached [file]
  40.  
  41. # 显示工作区与当前分支最新commit之间的差异
  42. $ git diff HEAD
  43.  
  44. # 显示两次提交之间的差异
  45. $ git diff [first-branch]...[second-branch]
  46.  
  47. # 显示今天你写了多少行代码
  48. $ git diff --shortstat "@{0 day ago}"
  49.  
  50. # 显示某次提交的元数据和内容变化
  51. $ git show [commit]
  52.  
  53. # 显示某次提交发生变化的文件
  54. $ git show --name-only [commit]
  55.  
  56. # 显示某次提交时,某个文件的内容
  57. $ git show [commit]:[filename]
  58.  
  59. # 显示当前分支的最近几次提交
  60. $ git reflog
  61.  
  62. # 从本地master拉取代码更新当前分支:branch 一般为master
  63. $ git rebase [branch]

八、远程同步

  1. $ git remote update --更新远程仓储
  2. # 下载远程仓库的所有变动
  3. $ git fetch [remote]
  4.  
  5. # 显示所有远程仓库
  6. $ git remote -v
  7.  
  8. # 显示某个远程仓库的信息
  9. $ git remote show [remote]
  10.  
  11. # 增加一个新的远程仓库,并命名
  12. $ git remote add [shortname] [url]
  13.  
  14. # 取回远程仓库的变化,并与本地分支合并
  15. $ git pull [remote] [branch]
  16.  
  17. # 上传本地指定分支到远程仓库
  18. $ git push [remote] [branch]
  19.  
  20. # 强行推送当前分支到远程仓库,即使有冲突
  21. $ git push [remote] --force
  22.  
  23. # 推送所有分支到远程仓库
  24. $ git push [remote] --all

九、撤销

  1.  
  2. # 恢复暂存区的指定文件到工作区
  3. $ git checkout [file]
  4.  
  5. # 恢复某个commit的指定文件到暂存区和工作区
  6. $ git checkout [commit] [file]
  7.  
  8. # 恢复暂存区的所有文件到工作区
  9. $ git checkout .
  10.  
  11. # 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
  12. $ git reset [file]
  13.  
  14. # 重置暂存区与工作区,与上一次commit保持一致
  15. $ git reset --hard
  16.  
  17. # 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
  18. $ git reset [commit]
  19.  
  20. # 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
  21. $ git reset --hard [commit]
  22.  
  23. # 重置当前HEAD为指定commit,但保持暂存区和工作区不变
  24. $ git reset --keep [commit]
  25.  
  26. # 新建一个commit,用来撤销指定commit
  27. # 后者的所有变化都将被前者抵消,并且应用到当前分支
  28. $ git revert [commit]
  29.  
  30. # 暂时将未提交的变化移除,稍后再移入
  31. $ git stash
  32. $ git stash pop

十、其他

  1.  
  2. # 生成一个可供发布的压缩包
  3. $ git archive

<2>

<3>

git fetch和git pull都可以将远端仓库更新至本地那么他们之间有何区别?想要弄清楚这个问题有有几个概念不得不提。

FETCH_HEAD: 是一个版本链接,记录在本地的一个文件中,指向着目前已经从远程仓库取下来的分支的末端版本。 
commit-id:在每次本地工作完成后,都会做一个git commit 操作来保存当前工作到本地的repo, 此时会产生一个commit-id,这是一个能唯一标识一个版本的序列号。 在使用git push后,这个序列号还会同步到远程仓库。

有了以上的概念再来说说git fetch 
git fetch:这将更新git remote 中所有的远程仓库所包含分支的最新commit-id, 将其记录到.git/FETCH_HEAD文件中 
git fetch更新远程仓库的方式如下:

git fetch origin master:tmp 
//在本地新建一个temp分支,并将远程origin仓库的master分支代码下载到本地temp分支
git diff tmp 
//来比较本地代码与刚刚从远程下载下来的代码的区别
git merge tmp
//合并temp分支到本地的master分支
git branch -d temp
//如果不想保留temp分支 可以用这步删除
(1)如果直接使用git fetch,则步骤如下:

创建并更新本 地远程分支。即创建并更新origin/xxx 分支,拉取代码到origin/xxx分支上。
在FETCH_HEAD中设定当前分支-origin/当前分支对应,如直接到时候git merge就可以将origin/abc合并到abc分支上。
(2)git fetch origin 
只是手动指定了要fetch的remote。在不指定分支时通常默认为master 
(3)git fetch origin dev 
指定远程remote和FETCH_HEAD,并且只拉取该分支的提交。

git pull : 首先,基于本地的FETCH_HEAD记录,比对本地的FETCH_HEAD记录与远程仓库的版本号,然后git fetch 获得当前指向的远程分支的后续版本的数据,然后再利用git merge将其与本地的当前分支合并。所以可以认为git pull是git fetch和git merge两个步骤的结合。 
git pull的用法如下:

git pull <远程主机名> <远程分支名>:<本地分支名>
//取回远程主机某个分支的更新,再与本地的指定分支合并。

因此,与git pull相比git fetch相当于是从远程获取最新版本到本地,但不会自动merge。如果需要有选择的合并git fetch是更好的选择。效果相同时git pull将更为快捷
--------------------- 
git reset (–mixed) HEAD~1 
回退一个版本,且会将暂存区的内容和本地已提交的内容全部恢复到未暂存的状态,不影响原来本地文件(未提交的也 
不受影响) 
git reset –soft HEAD~1 
回退一个版本,不清空暂存区,将已提交的内容恢复到暂存区,不影响原来本地的文件(未提交的也不受影响) 
git reset –hard HEAD~1 
回退一个版本,清空暂存区,将已提交的内容的版本恢复到本地,本地的文件也将被恢复的版本替换
--------------------- 
使用git log 查看历史提交

git stauts 查看一下当前版本库中文件状态。

 

posted on 2018-11-29 10:56  奔跑吧人生  阅读(128)  评论(0编辑  收藏  举报

导航