常用 Git 命令清单这一篇就够啦!

jqwo5248 7年前
   <p>下面是我整理的常用 Git 命令清单。几个专用名词的译名如下。</p>    <ul>     <li> <p>Workspace:工作区</p> </li>     <li> <p>Index / Stage:暂存区</p> </li>     <li> <p>Repository:仓库区(或本地仓库)</p> </li>     <li> <p>Remote:远程仓库</p> </li>    </ul>    <h3><strong>一、新建代码库</strong></h3>    <p># 在当前目录新建一个Git代码库</p>    <p>$ git init</p>    <p># 新建一个目录,将其初始化为Git代码库</p>    <p>$ git init [project-name]</p>    <p># 下载一个项目和它的整个代码历史</p>    <p>$ git clone [url]</p>    <h3><strong>二、配置</strong></h3>    <p>Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。</p>    <p># 显示当前的Git配置</p>    <p>$ git config --list</p>    <p># 编辑Git配置文件</p>    <p>$ git config -e [--global]</p>    <p># 设置提交代码时的用户信息</p>    <p>$ git config [--global] user.name "[name]"</p>    <p>$ git config [--global] user.email "[email address]"</p>    <h3><strong>三、增加/删除文件</strong></h3>    <p># 添加指定文件到暂存区</p>    <p>$ git add [file1] [file2] ...</p>    <p># 添加指定目录到暂存区,包括子目录</p>    <p>$ git add [dir]</p>    <p># 添加当前目录的所有文件到暂存区</p>    <p>$ git add .</p>    <p># 删除工作区文件,并且将这次删除放入暂存区</p>    <p>$ git rm [file1] [file2] ...</p>    <p># 停止追踪指定文件,但该文件会保留在工作区</p>    <p>$ git rm --cached [file]</p>    <p># 改名文件,并且将这个改名放入暂存区</p>    <p>$ git mv [file-original] [file-renamed]</p>    <h3><strong>四、代码提交</strong></h3>    <p># 提交暂存区到仓库区</p>    <p>$ git commit -m [message]</p>    <p># 提交暂存区的指定文件到仓库区</p>    <p>$ git commit [file1] [file2] ... -m [message]</p>    <p># 提交工作区自上次commit之后的变化,直接到仓库区</p>    <p>$ git commit -a</p>    <p># 提交时显示所有diff信息</p>    <p>$ git commit -v</p>    <p># 使用一次新的commit,替代上一次提交</p>    <p># 如果代码没有任何新变化,则用来改写上一次commit的提交信息</p>    <p>$ git commit --amend -m [message]</p>    <p># 重做上一次commit,并包括指定文件的新变化</p>    <p>$ git commit --amend [file1] [file2] ...</p>    <h3><strong>五、分支</strong></h3>    <p># 列出所有本地分支</p>    <p>$ git branch</p>    <p># 列出所有远程分支</p>    <p>$ git branch -r</p>    <p># 列出所有本地分支和远程分支</p>    <p>$ git branch -a</p>    <p># 新建一个分支,但依然停留在当前分支</p>    <p>$ git branch [branch-name]</p>    <p># 新建一个分支,并切换到该分支</p>    <p>$ git checkout -b [branch]</p>    <p># 新建一个分支,指向指定commit</p>    <p>$ git branch [branch] [commit]</p>    <p># 新建一个分支,与指定的远程分支建立追踪关系</p>    <p>$ git branch --track [branch] [remote-branch]</p>    <p># 切换到指定分支,并更新工作区</p>    <p>$ git checkout [branch-name]</p>    <p># 建立追踪关系,在现有分支与指定的远程分支之间</p>    <p>$ git branch --set-upstream [branch] [remote-branch]</p>    <p># 合并指定分支到当前分支</p>    <p>$ git merge [branch]</p>    <p># 选择一个commit,合并进当前分支</p>    <p>$ git cherry-pick [commit]</p>    <p># 删除分支</p>    <p>$ git branch -d [branch-name]</p>    <p># 删除远程分支</p>    <p>$ git push origin --delete [branch-name]</p>    <p>$ git branch -dr [remote/branch]</p>    <h3><strong>六、标签</strong></h3>    <p># 列出所有tag</p>    <p>$ git tag</p>    <p># 新建一个tag在当前commit</p>    <p>$ git tag [tag]</p>    <p># 新建一个tag在指定commit</p>    <p>$ git tag [tag] [commit]</p>    <p># 查看tag信息</p>    <p>$ git show [tag]</p>    <p># 提交指定tag</p>    <p>$ git push [remote] [tag]</p>    <p># 提交所有tag</p>    <p>$ git push [remote] --tags</p>    <p># 新建一个分支,指向某个tag</p>    <p>$ git checkout -b [branch] [tag]</p>    <h3><strong>七、查看信息</strong></h3>    <p># 显示有变更的文件</p>    <p>$ git status</p>    <p># 显示当前分支的版本历史</p>    <p>$ git log</p>    <p># 显示commit历史,以及每次commit发生变更的文件</p>    <p>$ git log --stat</p>    <p># 显示某个文件的版本历史,包括文件改名</p>    <p>$ git log --follow [file]</p>    <p>$ git whatchanged [file]</p>    <p># 显示指定文件相关的每一次diff</p>    <p>$ git log -p [file]</p>    <p># 显示指定文件是什么人在什么时间修改过</p>    <p>$ git blame [file]</p>    <p># 显示暂存区和工作区的差异</p>    <p>$ git diff</p>    <p># 显示暂存区和上一个commit的差异</p>    <p>$ git diff --cached [file]</p>    <p># 显示工作区与当前分支最新commit之间的差异</p>    <p>$ git diff HEAD</p>    <p># 显示两次提交之间的差异</p>    <p>$ git diff [first-branch]...[second-branch]</p>    <p># 显示某次提交的元数据和内容变化</p>    <p>$ git show [commit]</p>    <p># 显示某次提交发生变化的文件</p>    <p>$ git show --name-only [commit]</p>    <p># 显示某次提交时,某个文件的内容</p>    <p>$ git show [commit]:[filename]</p>    <p># 显示当前分支的最近几次提交</p>    <p>$ git reflog</p>    <h3><strong>八、远程同步</strong></h3>    <p># 下载远程仓库的所有变动</p>    <p>$ git fetch [remote]</p>    <p># 显示所有远程仓库</p>    <p>$ git remote -v</p>    <p># 显示某个远程仓库的信息</p>    <p>$ git remote show [remote]</p>    <p># 增加一个新的远程仓库,并命名</p>    <p>$ git remote add [shortname] [url]</p>    <p># 取回远程仓库的变化,并与本地分支合并</p>    <p>$ git pull [remote] [branch]</p>    <p># 上传本地指定分支到远程仓库</p>    <p>$ git push [remote] [branch]</p>    <p># 强行推送当前分支到远程仓库,即使有冲突</p>    <p>$ git push [remote] --force</p>    <p># 推送所有分支到远程仓库</p>    <p>$ git push [remote] --all</p>    <h3><strong>九、撤销</strong></h3>    <p># 恢复暂存区的指定文件到工作区</p>    <p>$ git checkout [file]</p>    <p># 恢复某个commit的指定文件到工作区</p>    <p>$ git checkout [commit] [file]</p>    <p># 恢复上一个commit的所有文件到工作区</p>    <p>$ git checkout .</p>    <p># 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变</p>    <p>$ git reset [file]</p>    <p># 重置暂存区与工作区,与上一次commit保持一致</p>    <p>$ git reset --hard</p>    <p># 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变</p>    <p>$ git reset [commit]</p>    <p># 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致</p>    <p>$ git reset --hard [commit]</p>    <p># 重置当前HEAD为指定commit,但保持暂存区和工作区不变</p>    <p>$ git reset --keep [commit]</p>    <p># 新建一个commit,用来撤销指定commit</p>    <p># 后者的所有变化都将被前者抵消,并且应用到当前分支</p>    <p>$ git revert [commit]</p>    <h3><strong>十、其他</strong></h3>    <p># 生成一个可供发布的压缩包</p>    <p>$ git archive</p>    <p>最后,如果对这些命令不感兴趣的话,请看这里,一个强大的可视化Git工具:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/d1216ccd8e87aa67f12b75b992e36bb5.jpg"></p>    <p><img src="https://simg.open-open.com/show/c3d008594f31176b5df5db3fdee920d8.jpg"></p>    <p> </p>    <p> </p>    <p> </p>    <p>来自:https://mp.weixin.qq.com/s?__biz=MzI1NDQ3MjQxNA==&mid=2247483796&idx=1&sn=c0a354a414dddc7211359c6fe38c15ae&chksm=e9c5f825deb271334c5daeaead950ebda4fd9d312639ecebfcc71662d1a25367ab22591201ee&scene=0&key=&ascene=7&uin=&devicetype=android-23&version=26031b31&nettype=WIFI</p>    <p> </p>