超详细git命令速记

  git add .      -------------------------------------------------------------- 把工作区所有变化(包含修改、新增的文件,不包含删除的文件)提交到暂存区
  git add -u , git add --update      ------------------------------------------ 将工作区所有变化(包含修改、删除的文件,不包含新文件)提交到暂存区
  git add -A , git add --all      --------------------------------------------- 上面两个命令的合集,提交所有变化
  git add fileName      ------------------------------------------------------- 提交单个文件到暂存区
  git add fileName1 fileName2 fileName3      ---------------------------------- 提交多个文件到暂存区
  git add folderName/*      ----------------------------------------------------- 提交整个文件夹到暂存区
  git blame 文件路径  ----------------------------------------------------------- 查看该文件所有的改动提交
git blame -L start,end 文件名 ------------------------------------------------- 查看该文件start-end行数的提交
git branch ------------------------------------------------------------- 查看工作区分支 git branch
-r ---------------------------------------------------------- 查看远程分支 git branch -a ---------------------------------------------------------- 查看所有分支 git branch -v , git branch --v , git branch --verbose ------------------ 查看工作区所有分支、分支对应的hash、领先落后步数、commitMessage git branch -vv --------------------------------------------------------- 查看工作区所有分支、分支对应的hash、远端分支、领先落后步数、commitMessage git branch -av --------------------------------------------------------- -a与-v的组合 git branch -avv --------------------------------------------------------- -a与-vv的组合 git branch --contains 50089 -------------------------------------------- 显示包含提交50089的分支 git branch --merged ---------------------------------------------------- 显示所有已合并到当前分支的分支 git branch --no-merged ------------------------------------------------- 显示所有未合并到当前分支的分支 git branch -m oldBranchName newBranchName ----------------------------- 修改工作区分支名称 git branch -D branchName ---------------------------------------------- 删除工作区分支(不能在branchName分支操作) git branch branchName ------------------------------------------------- 在当前分支创建新分支(不自动切到新分支) git branch --set-upstream-to=origin/remoteBranchName yourBranchName --- 设置工作区分支与远端分支关联
      HEAD是checkout的灵魂所在,原理是.git目录下的HEAD引用
      如下命令查看HEAD指针指向与当前commitId 
       $cat .git/HEAD 打印出 ref:refs/heads/master 
       $cat .get/refs/heads/master 打印出 b0a75d5115a59792784bd23ef4d8d0c0202d2bfa
  git checkout commitId      -------------------------------------------------- 切换到指定的commitId
  git checkout branchName ,git switch branchName     ------------------------------------------------ 切换工作区分支(默认是切换后分支最新的commitId)
  git checkout branchName commitId      --------------------------------------- 换到分支指定的commitId
  git checkout -b branchName , git switch -c  branchName
--------------------------------------------- 创建并切到新分支(默认以当前分支当前最新的commitId创建新分支) git checkout commitId
-b branchName ------------------------------------ 以某个commitId为基础创建并切到新分支 git checkout fileName , git checkout --fileName ------------------------ 撤销一个文件在工作区的修改 git checkout . -------------------------------------------------------- 撤销所有文件在工作区的修改 git checkout --track origin/branchName --------------------------------- 远程新建了分支,工作区没有,使用此命令自动创建本地同名分支并创建远端关联
  git clone git+ssh:  git@xxxx.git    clone远程分支
  git config --list      ------------------------------------------------------ 查看git配置列表
  git config --global user.name "xxx"      ------------------------------------ 配置git用户名
  git config --global user.email "xxx@xxx.com"       -------------------------- 配置git邮箱
  git config --global color.ui true       ------------------------------------- git status等命令自动着色
  git config --global color.status auto      
  git config --global color.diff auto
  git config --global color.branch auto
  git config --global color.interactive auto
  git commit -m "xxx"      ---------------------------------------------------- 创建一次提交,生成一个commitId
  git commit -m "xxx" -n  ----------------------------------------------------- 忽略错误提示进行commit
 git commit
--amend ---------------------------------------------------- 修改提交信息 git commit --amend -m 'xxx' -------------------------------------------- 合并上一次提交(用于反复修改) git commit -am 'xxx' -------------------------------------------------- 将add和commit合并成一步
  git  cherry-pick ff44785404a8       ----------------------------------------- 将commitId为ff44785404a8的提交挑拣出来,常用于从老分支中挑拣出某次提交放到新分支
  git diff    ----------------------------------------------------------------- 显示所有未添加至index的变更
  git diff --cached        ---------------------------------------------------- 显示所有已添加index但还未commit的变更
  git diff HEAD^      --------------------------------------------------------- 比较与上一个版本的差异
  git diff HEAD -- ./lib       ------------------------------------------------ 较与HEAD版本lib目录的差异
  git diff origin/master..master      ----------------------------------------- 比较远程分支master上有本地分支master上没有的
  git diff origin/master..master --stat      ---------------------------------- 只显示差异的文件,不显示具体内容
  git fetch     --------------------------------------------------------------- 获取所有远程分支(不更新本地分支,另需merge)
  git fetch --prune     ------------------------------------------------------- 获取所有原创分支并清除服务器上已删掉的分支
  git grep xxx      ------------------------------------------------------------ 在文件中搜索文本xxx
  git hook      --------------------------------------------------------------- 设置自定义触发脚本,如commit-msg提交规范
  git init      --------------------------------------------------------------- 初始化本地git仓库
  gitk      ------------------------------------------------------------------- 打开git自带的可视化工具
  gitk --all      ------------------------------------------------------------- 打开git自带可视化工具所有内容
  git log      ---------------------------------------------------------------- 查看提交日志
  git log -1       ------------------------------------------------------------ 查看1行日志 -n为n行
  git log -- 文件路径       ---------------------------------------------------- 查看指定文件的更改日志
  git log v3.0        --------------------------------------------------------- 查看v3.0版本日志
  git log --pretty=oneline       ---------------------------------------------- 只显示comidId和msg
  git log --pretty=format:'%h %s' --graph       ------------------------------- 以图示的方式查看提交日志
  git merge branchName      --------------------------------------------------- 合并目标分支到当前工作区分支
  git merge --abort         --------------------------------------------------- 合并出现冲突,跳过冲突,恢复到合并前的状态
  git mv oldFileName newFileName      ----------------------------------------- 更改文件名
  git push origin master      ------------------------------------------------- 将当前分支推到远程master分支
  git push      --------------------------------------------------------------- 将当前分支推到同名远程分支(需先建立关联)
  git push -f       ----------------------------------------------------------- 强推远程分支
  git push origin --delete  branchName       ---------------------------------- 删除指定远程分支
  git push --tags     --------------------------------------------------------- 把所有tag推送到远程仓库 
  git pull origin master       ------------------------------------------------ 拉取合并远程master到当前分支
  git pull      --------------------------------------------------------------- 拉取合并同名远程分支到当前分支(需先建立关联)
  git pull --rebase     ------------------------------------------------------- 拉取合并的同时对commit提交也进行合并
  git pull origin master:dev       -------------------------------------------- 拉取合并远程dev分支到当前分支(拉远程master到本地dev)
  git rm fileName     --------------------------------------------------------- 从暂存区和工作区中删除文件 
  git rm -f fileName        --------------------------------------------------- 强制从暂存区和工作区中删除文件 
  git rm --cached fileName       ---------------------------------------------- 从暂存区删除,保留工作区
  git remote add origin git+ssh:  git@xxxx.git       -------------------------- 增加远程关联定义
  git remote show https:  xxx.git        -------------------------------------- 显示某个远程仓库的信息
  git remote rm  storeName      ----------------------------------------------- 删除远端仓库
  git remote rename oldStoreName newStoreName       --------------------------- 修改仓库名
git remote update origin -p --------------------------- 同步所有远端分支列表到本地
  git rebase -i HEAD~2       -------------------------------------------------- 合并最近2次提交
  git reset       ------------------------------------------------------------- 将回退到目标版本,目标版本后面的版本log全部删掉
  git reset HEAD lookme.txt      --------------------------------------- 撤销最近的一次commit,并把更改放到工作区
  git reset --hard HEAD , get reet --hard HEAD~n       ------------------------ 是将工作区、暂存取和HEAD保持一致
  git reset commitId      ----------------------------------------------------- 将咱暂存区和commitID的提交保持一致
  git reset --soft f7dfsad89fds ----------------------------------------------- 版本回退到f7dfsad89ds,并把更改放在工作区
 git reset
--hard commitId ---------------------------------------------- 是将工作区、暂存取和commitId保持一致
  git revert     -------------------------------------------------------------- 将目标版本撤销,不影响目标版本前后版本,同时生成新版本(默认对每个撤销的版本进行一次提交,--no-commit 后可以最后一起手动提交。)
  git revert -n commitId , git revert --no-commit      ------------------------ 撤销掉指定版本,不影响其他版本,同时生成新版本
  git rev-parse HEAD       ---------------------------------------------------- 获取当前分支完整commitId
  git rev-parse --short HEAD      --------------------------------------------- 获取当前分支部分commitId
  git reflog       --------------------------------------------------------查看操作记录)
  git status      ------------------------------------------------------------- 查看当前版本状态(是否修改)
 
  git submodule init --------------------------------------------------------- 初始化git子模块,需要到配置的目录下去执行
  git submodule update ------------------------------------------------------- 拉取子模块代码
 配合使用项目根目录.gitmodules配置文件:

[submodule "abc"] //路径

path = abc //路径

url = http://github.xxx.xxx/xxxx // 子模块仓库代码

branch = dev // 默认子模块分支

  git show commitId , git show HEAD^n      ------------------------------------ 显示某次提交的详细内容
  git show v3.0        -------------------------------------------------------- 显示v3.0的日志及详细内容
  git show-branch , git show-branch --all       ------------------------------- 图示(当前分支/所有分支)历史
  git stash      --------------------------------------------------------把工作区的改动储藏起来
  git stash list      -------------------------------------------------- 查看所有储藏
  git stash pop       ---------------------------------------------------恢复改动,并删除储藏记录
git stash drop 删除储藏记录
git stash apply 恢复改动,但不删除储藏记录
git stash apply stash@{0} 多个stash时,恢复某个指定的
  git --version      ---------------------------------------------------------- 查看git版本

 

 

 

使用git常见错误:
1、切换分支git pull时,提示分支锁定,需要删某个文件。

\"D:/GitCredentialWinStore/git-credential-winstore.exe\" store: "D:/GitCredentialWinStore/git-credential-winstore.exe": No such file or directory
error: Unable to create 'F:/eFlowerProject/epd/.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.

解决方案:删除.git里的index.lock文件,但是发现没有这个文件,于是找到这个应用程序,"D:/GitCredentialWinStore/git-credential-winstore.exe,关掉就能正常pull了

 

 

 

 

附录:

windows常用命令:

dir  -----列出下一级所有目录文件夹

cd .. | cd ../.. 切换到上一/两层目录

cd [filePath] 切换到指定目录

tree 查看目录结构图示

posted @ 2020-11-27 17:40  早起开拓者  阅读(126)  评论(0编辑  收藏  举报