git log,git reflog,git diff一般都在图形工具中使用,所以暂时未列出
 
执行所有的git命令,都可以使用-C <path>指定工作目录。如:git -C /home init demo等同于在/home目录下执行git init demo命令
 
init - 初始化仓库 1 2 3 git init <name>            git init <path>            git -C <path> init <name>  
 
config - 信息配置 1 2 git -C <path> config user.name "<你的姓名>"  # 设置仓库提交人姓名 git -C <path> config user.email "<你的邮箱>" # 设置仓库提交人邮件地址 
 
局部
1 2 git config user.name "<你的姓名>"  git config user.email "<你的邮箱>"  
 
全局
1 2 3 4 5 6 git config --global user.name "<你的姓名>"  git config --global user.email "<你的邮箱>"  git config --global credential.helper store git config --global pull.rebase true  git config --global core.quotepath false  git config --global core.autocrlf false  
 
使用密码帮助程序记录账号和秘密
1 git config --global credential.helper store 
 
设置git pull时默认为变基
1 git config --global pull.rebase true  
 
文件路径Unicode码处理
1 git config --global core.quotepath false  
 
换行符处理
1 git config --global core.autocrlf false  
 
true
表示在推送时转成 \n,在拉取时转成 \r\n。这样的设置让 Windows 的开发者能兼容很多的开发工具(比如早期的记事本,新的已经支持 \r\n 了),不至于遇到很多换行符问题。
 
false
表示在推送时和拉取时都原样保留换行符。git 完全不处理换行符,全部改由开发者自行解决。
 
input
表示在推送时转成 \n,在拉取时原样保留换行符。注意到,这样的设置会让仓库里所有的换行符都变成 \n 不再有什么时候有 \r\n 了。
 
 
 
add - 文件追踪 
git commit只会提交已经git add修改
 
1 2 3 git add <file-name> # 追踪指定文件的最新状态,支持 linux 下文件通配符。如 *.js 等 git add -A          # 追踪当前仓库下所有文件 git add .           # 追踪当前目录及其子目录下所有文件,如果处于仓库根目录,和 git add -A 表现一致 
 
commit - 提交 
提交只是将代码存储在了本地仓库,需要结合git push才能将代码推送到远程仓库与其他小伙伴共享
 
1 2 3 4 5 git commit                           git commit -m "message"               git commit -a                        git commit --amend                   git commit --amend -m "New message"   
 
1 git commit --amend -m "New message"  
 
它的原理是产生一个新的提交对象,替换掉上一次提交产生的提交对象。
这时如果暂存区有发生变化的文件,会一起提交到仓库。所以,--amend 不仅可以修改提交信息,还可以整个把上一次提交替换掉。
 
push - 推送 1 2 3 4 5 6 7 8 9 10 11 git push origin master               git push -u origin master            git push origin A:B                  git push origin :B                   git push origin --delete B           git push --tags                      git push origin v1.0                 git push origin v1.0:refs/tags/v2.0  git push origin :refs/tags/v2.0      git push <remote> <branch> --force   git push <remote> <branch> -f        
 
若有 branch 与 tag 重名,则需要明确区分即可。
对象 
路径 
 
 
branch 
refs/heads/branch_name 
 
tag 
refs/tags/tag_name 
 
pull - 拉取远程代码并自动合并 1 2 git pull                       git pull origin master:master  
 
fetch - 拉取远程代码不合并 1 2 3 git fetch                       git fetch -p                    git fetch origin master:master  
 
merge - 合并 1 2 3 git merge <branch>          git merge --no-ff <branch>                              
 
checkout - 代码检出操作 1 2 3 4 5 6 7 8 9 git checkout --orphan <new-branch>      git checkout <branch>                   git checkout <branch> <rbranch>         git checkout <fileName>                 git checkout -b <new-branch>                                                    git checkout -b <new-branch> <rbranch>  git checkout -b <new-branch> origin/<rbranch>      git checkout -b <new-branch> refs/tags/<tag-name>  
 
clone - 仓库拉取 1 2 3 git clone  <url> <local-path>          git clone  -b dev <url>                git clone  --depth 1 <url>             
 
branch - 分支操作 1 2 3 4 5 6 7 8 9 10 11 12 git branch                     git branch -r                  git branch -a                  git branch <new-branch>        git branch remotes/origin/A    git branch -m <new-branch>     git branch -d <branch>         git branch -D <branch>         git branch -r | awk '{print $1}'  | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}'  | xargs git branch -d git branch -u origin/<branch>  git branch -u origin/b a       
 
stash - 工作目录暂存 1 2 3 4 5 6 git stash                  git stash list             git stash apply stash@{0}  git stash pop stash@{0}    git stash drop stash@{0}   git stash branch <branch>  
 
执行git stash list后显示的结果
1 2 3 stash@{0}: WIP on master: 049d078 added the index file stash@{1}: WIP on master: c264051 Revert "added file_size" stash@{2}: WIP on master: 21d80a5 added number to log 
 
忽略跟踪 1 2 git update-index --assume-unchanged <file-path>  git update-index --no-assume-unchanged <file-path>  
 
reset - 回退提交 1 git reset [<mode>] [<commit>] 
 
mode: –soft:只回退commit信息,不更改文件索引,不删本地代码,若要提交可直接执行git commit –mixed:回退commit信息和文件索引,不删本地代码,如果还要提交,需要git add然后commit –hard:回退commit信息、文件索引并删除本地代码
commit: 通过git log和git reflog查看
 
示例
1 2 3 4 5 6 7 8 9 10 # 回退到本地存储的最新版本并删除本地所有未暂存的文件 git reset --hard HEAD # 回退到上一个版本 git reset --soft HEAD^ # 或者 git reset --soft HEAD~ # 回退到100个版本前,不写--hard默认是--mixed) git reset –hard HEAD~100 
 
cherry-pick - 拣选 
无论是对单个 commit 进行 cherry-pick ,还是批量处理,注意一定要根据时间线,依照 commit 的先后顺序来处理,否则会有意想不到的问题。
 
1 2 3 4 5 6 7 8 git cherry-pick <commit-id>                git cherry-pick <commit-id1> <commit-id2>  git cherry-pick –n <commit-id>             git cherry-pick --no-commit <commit-id>    git cherry-pick -x <commit_id>             git cherry-pick --continue                  git cherry-pick --quit                     git cherry-pick --abort                    
 
tag - 标签管理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 git tag                              git tag v1.0                         git tag -ln5                         git tag -ln5 v1.0                    git tag -a v1.0                      git tag -a v1.0 9fceb02              git tag -a v1.0 -m "xx"               git tag -a v1.0 -m "xx"  master       git tag -d v1.0                      git show v1.0                        git push --tags                      git push origin v1.0                 git push origin v1.0:refs/tags/v2.0  git push origin :refs/tags/v2.0      
 
remote - 远程仓库操作 1 2 3 4 5 git remote -v                    git remote add origin <url>      git remote set-url origin <url>  git remote remove origin         git remote rm  origin             
 
archive - 导出干净的压缩包 1 2 3 4 5 git archive --format=tar v1.0.0 |gzip > mysite-1.0.0.tar.gz git archive --format=zip v1.0.0 > mysite-1.0.0.zip git archive --format=zip HEAD > mysite.zip git archive --format=zip –prefix=mysite/ v1.0.0 > mysite-1.0.0.zip 
 
--format 指明要产生tar格式的输出。--prefix 指明包中所有东西都放到mysite/目录下。
 
rm - 删除git上已经提交的文件 1 2 3 4 5 6 git rm  <file-name>              git rm  -r <file-name>           git rm  -r --cached <file-name>  git rm  -f <file-name>           git rm  -rf <file-name>          git rm  -rn <file-name>          
 
<file-name>支持通配符。 加上-n参数,执行命令时,是不会删除任何文件,而是展示此命令要删除的文件列表预览,所以一般用这个参数先看看要删除哪些文件,防止误删,确认之后,就去掉此参数,真正的删除文件。
 
统计 1 2 git shortlog --numbered --summary  git log  --oneline | wc  -l          
 
svn - SVN 仓库同步操作 
默认的 SVN 仓库会有三个子目录:trunk:仓库主目录,等效 git 的master分支branches:分支存放目录,等效 git 里除master以外的分支tags:标签存放目录,等效 git 里标签
 
1 2 3 4 5 6 7 8 9 10 git svn clone  <svn-url> -T <trunk> -b <branches> -t <tags> git svn clone  --stdlayout <svn-url>  git svn clone  -s <svn-url>           git svn rebase                       git svn dcommit                      
 
SVN迁移操作可参考《SVN迁移到Git(整理)》 
 
多人协作工作模式 
多人协作工作模式一般是这样的: 首先,可以试图用git push origin branch-name推送自己的修改。 如果推送失败,则因为远程分支比你的本地更新早,需要先用git pull试图合并。 如果合并有冲突,则需要解决冲突,并在本地提交。再用git push origin branch-name推送。
 
hooks 查看项目 hooks 目录
1 git config core.hooksPath 
 
全局操作
1 git config --global core.hooksPath 
 
设置项目 hooks 目录
1 git config core.hooksPath .husky 
 
恢复默认 hooks 目录
1 git config --unset core.hooksPath