=git========》命令大全

代码恢复修改(并且sync):
例drv目录:

cd drv/

git status

git clean

git clean -dxf

git status

git checkout --.

git status

 

上库操作命令:

git add file

git status .

git commit .

repo upload .

 

修改之后的提交操作命令:

git status .

git add file

git commit --amend

repo upload .

 

合并多个commit:

git rebase - i commit_id

id为要合并的commit之前的id

 

重置到某一次的提交:

git reset --hard commit_id


 

名词

master:默认开发分支

origin:默认远程版本库

Index/stage:暂缓区

Workspace:工作区

Repository:仓库区(本地仓库)

Remote:远程仓库

一. 新建代码库

  1. # 在当前目录新建一个代码仓库

  2.  

  3. git init

  4.  

  5. # 新建一个目录,将其初始化为Git仓库

  6.  

  7. git init [project-name]

  8.  

  9. # 下载一个项目和它的整个代码历史

  10.  

  11. git clone[url]

二.配置

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

  1. # 显示当前的Git配置

  2.  

  3. git config --list

  4.  

  5. # 编辑Git配置文件

  6.  

  7. git config -e [--global]

  8.  

  9. # 设置提交代码时的用户信息

  10.  

  11. git config [--global]

  12.  

  13. # 设置提交代码时的用户信息

  14.  

  15. git config [--global] user.name "[name]"

  16.  

  17. git config [--global] user.email "email address"

三.增加/删除/修改文件

  1. # 查看状态

  2.  

  3. git status

  4.  

  5. # 查看变更内容

  6.  

  7. git diff

  8.  

  9. # 添加指定文件到暂存区

  10.  

  11. git add [file1] [file2]

  12.  

  13. # 添加指定目录到暂缓区,包括子目录

  14.  

  15. git add .

  16.  

  17. # 添加每个变化前,都会要求确认

  18.  

  19. #对于同一个文件的多处变化,可以实现分次提交

  20.  

  21. git add -p

  22.  

  23. # 删除工作区文件,并且将这次删除放入=暂缓区

  24.  

  25. git rm[file1] [file2]...

  26.  

  27. # 停止追踪指定文件,但该文件会保留在工作区

  28.  

  29. git rm --cached [file]

  30.  

  31. # 改名文件,并且将这个改名放入暂存区

  32.  

  33. git mv [file-oigin] [file-renamed]

代码提交

  1. # 提交暂缓区至仓库区

  2.  

  3. git commit -m [message]

  4.  

  5. # 提交暂缓区的指定文件到仓库区

  6.  

  7. git commit [file1] [file2]...m [message]

  8.  

  9. # 提交工作区自上次commit之后的变化,直接到仓库区

  10.  

  11. git commit -a

  12.  

  13. # 使用一次新的commit,替代上一次提交

  14.  

  15. # 如果代码没有任何新变化,则用来改写上一次commit的提交信息

  16.  

  17. git commit --amend -m [message]

  18.  

  19. # 重做上一次commit,并包括指定文件的新变化

  20.  

  21. git commit --amend [file1] [file2]...

五.分支

  1. # 显示所有的本地分支

  2.  

  3. git branch

  4.  

  5. # 列出所有的远程分支

  6.  

  7. git branch -r

  8.  

  9. # 列出所有的本地分支和远程分支

  10.  

  11. git branch -a

  12.  

  13. # 新建一个分支,但是依然停留在当前分支

  14.  

  15. git branch [branch-name]

  16.  

  17. # 新建一个分支,并与指定的远程分支建立追踪关系

  18.  

  19. git branch --track [branck] [remote-name]

  20.  

  21. # 删除远程分支

  22.  

  23. git push origin --delete [branch-name]

  24.  

  25. git branch -dr [remote/branch]

  26.  

  27. # 新建一个分支,并切换到该分支

  28.  

  29. git checkout -b [branch]

  30.  

  31. # 切换到指定分支,并更新工作区

  32.  

  33. git checkout [branch-name]

  34.  

  35. # 切换到上一个分支

  36.  

  37. git checkout -

  38.  

  39. # 建立追踪关系,在现有分支与指定的远程分支之间

  40.  

  41. git branch --set-upstream [branch] [remote-branch]

  42.  

  43. # 合并指定分支到当前分支

  44.  

  45. git merge [branch]

  46.  

  47. # 衍合指定分支到当前分支

  48.  

  49. git rebase <branch>

  50.  

  51. # 选择一个commit,合并到当前分支

  52.  

  53. git cherry-pick [commit]

六. 标签

  1. # 列出所有本地标签

  2.  

  3. git tag

  4.  

  5. # 基于最新提交创建的标签

  6.  

  7. git tag <tagname>

  8.  

  9. # 删除标签

  10.  

  11. git tag -d <tagname>

  12.  

  13. # 删除远程tag

  14.  

  15. git push origin refs/tages/[tagename]

  16.  

  17. # 查看tag信息

  18.  

  19. git show [tag]

  20.  

  21. # 提交指定tag

  22.  

  23. git push [remote] [tag]

  24.  

  25. # 提交所有tag

  26.  

  27. git push [remote] --[tag]

  28.  

  29. # 新建一个分支,指向某个tag

  30.  

  31. git checkout -b [branch] [tag]

七.查看信息

  1. # 显示有变更的文件

  2.  

  3. git status

  4.  

  5. # 显示当前分支的版本历史

  6.  

  7. git log

  8.  

  9. # 显示commit历史,以及每次commit发生变更的文件

  10.  

  11. git log --stat

  12.  

  13. # 搜索提交历史,根据关键词

  14.  

  15. git log -s [keyword]

  16.  

  17. # 显示某个commit之后的所有变动,每个commit占据一行

  18.  

  19. git log [tag] HEAD --pretty=format:%s

  20.  

  21. # 显示某个commit之后的所有变动,其“提交说明”必须符合搜索条件

  22.  

  23. git log [tag] HEAD --grep feature

  24.  

  25. # 显示某个文件的版本历史,包括文件改名

  26.  

  27. git log --follow [file]

  28. git whatchanged [file]

  29.  

  30. # 显示指定文件相关的每一次diff

  31.  

  32. git log -p [file]

  33.  

  34. # 显示过去的5次提交

  35.  

  36. git log -5 --pretty --oneline

  37.  

  38. # 显示所有提交过的用户,按提交次数排序

  39.  

  40. git shortlog -sn

  41.  

  42. # 显示指定文件是什么人在什么时间修改过

  43.  

  44. git blame [file]

  45.  

  46. # 显示暂缓区和工作区的差异

  47.  

  48. git diff

  49.  

  50. # 显示暂缓区和上一个commit的差异

  51.  

  52. git diff HEAD

  53.  

  54. # 显示两次提交之间的差异

  55.  

  56. git diff [first-branch]...[second-branch]

  57.  

  58. # 显示某次提交发生变化的文件

  59.  

  60. git show --name-only [commit]

  61. 当前版本的commitid

  62. git rev-parse HEAD

远程操作

  1. # 下载远程仓库的所有变动

  2.  

  3. git fetch [remote]

  4.  

  5. # 取回远程仓库的变化,并与本地分支合并

  6.  

  7. git pull [remote] [branch]

  8.  

  9. # 显示所有的远程仓库

  10.  

  11. git remote -v

  12.  

  13. # 显示某个远程仓库的信息

  14.  

  15. git remote show [remote]

  16.  

  17. # 增加一个新的远程仓库,并命名

  18.  

  19. git remote add [shortname] [url]

  20.  

  21. # 上传本地指定分支到远程仓库

  22.  

  23. git push [remote] [branch]

  24.  

  25. # 强行推送当前分支到远程仓库,即使有冲突

  26.  

  27. git push [remote] --force

  28.  

  29. # 推送所有的分支到远程仓库

  30.  

  31. git push [remote] --all

九.撤销

  1. # 撤销工作目录中所有未提交文件的修改内容

  2.  

  3. git reset --hard HEAD

  4.  

  5. # 撤销指定的未提交文件的修改内容

  6. git checkout HEAD [file]

  7. # 撤销指定的未提交文件的修改内容到最近的一次提交
  8.  git checkout --[file]

  9. # 撤销指定的提交

  10.  

  11. git revert <commit>

 

posted @ 2020-07-24 21:29  豌豆果果  阅读(158)  评论(0编辑  收藏  举报