现在做项目 Git 代码管理是一定少不了的。
多年以前可能是 SVN,我想如今的公司里面基本都转型到用 Git 了吧。
虽然如今已有很多可视化的 Git 工具,但是很多时候我们还是需要用到命令直接操作。
所以我就把 Git 的相关命令汇集了起来,即方便自己,也方便大家。
相关名词解释
master: 默认开发分支 origin: 默认远程版本库 Index / Stage:暂存区 Workspace:工作区 Repository:仓库区(或本地仓库) Remote:远程仓库
一、新建代码库
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/Szib8ySqErWKL2Az6xbJJaDIA9kWceSXibQa8ozJhZRW2j2K7JvQAsUh0DsOyBfBgC6RXrHISYRjSOvF0v3U6nB5pUeybKgarG/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;"># 在当前目录新建一个Git代码库</span><br/>$ git init<br/><span style="color: #75715e;line-height: 26px;"># 新建一个目录,将其初始化为Git代码库</span><br/>$ git init [project-name]<br/><span style="color: #75715e;line-height: 26px;"># 下载一个项目和它的整个代码历史</span><br/>$ git <span style="color: #a6e22e;line-height: 26px;">clone</span> [url]<br/> |
这两个项目初始化的命令,一般只在项目新建时使用到,比如我们在 Github 里面新建了一个仓库:
你能看到他的引导流程里面,最开始的就是 git init。
二、配置
我们时常会遇到并不想把某些文件提交到 Git 库里面去,比如 .idea、node_models 等,这类开发工具的配置目录,三方库依赖等。
这类需求就可以通过 .gitconfig 文件来进行配置。
它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
我们以 kubernetes 的代码为例:
你能看到他里面忽略了很多文件。
其他的 git 配置命令如下,基本不太常用:
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/Szib8ySqErWKL2Az6xbJJaDIA9kWceSXibQa8ozJhZRW2j2K7JvQAsUh0DsOyBfBgC6RXrHISYRjSOvF0v3U6nB5pUeybKgarG/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;"># 显示当前的Git配置</span><br/>$ git config --list<br/><span style="color: #75715e;line-height: 26px;"># 编辑Git配置文件</span><br/>$ git config -e [--global]<br/><span style="color: #75715e;line-height: 26px;"># 设置提交代码时的用户信息</span><br/>$ git config [--global] user.name <span style="color: #a6e22e;line-height: 26px;">"[name]"</span><br/>$ git config [--global] user.email <span style="color: #a6e22e;line-height: 26px;">"[email address]"</span><br/> |
三、增加/删除/修改文件
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/Szib8ySqErWKL2Az6xbJJaDIA9kWceSXibQa8ozJhZRW2j2K7JvQAsUh0DsOyBfBgC6RXrHISYRjSOvF0v3U6nB5pUeybKgarG/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;"># 查看状态</span><br/>$ git status<br/><span style="color: #75715e;line-height: 26px;"># 查看变更内容</span><br/>$ git diff<br/><span style="color: #75715e;line-height: 26px;"># 添加指定文件到暂存区</span><br/>$ git add [file1] [file2]<br/><span style="color: #75715e;line-height: 26px;"># 添加指定目录到暂存区,包括子目录</span><br/>$ git add [dir]<br/><span style="color: #75715e;line-height: 26px;"># 添加当前目录的所有文件到暂存区</span><br/>$ git add<br/><span style="color: #75715e;line-height: 26px;"># 添加每个变化前,都会要求确认,对于同一个文件的多处变化,可以实现分次提交</span><br/>$ git add -p<br/><span style="color: #75715e;line-height: 26px;"># 删除工作区文件,并且将这次删除放入暂存区</span><br/>$ git rm [file1] [file2]<br/><span style="color: #75715e;line-height: 26px;"># 停止追踪指定文件,但该文件会保留在工作区</span><br/>$ git rm --cached [file]<br/><span style="color: #75715e;line-height: 26px;"># 改名文件,并且将这个改名放入暂存区</span><br/>$ git mv [file-original] [file-renamed<br/> |
这里面我们用得最多就是 git add 和 git rm 命令。
当我们在工程里面新建了一个文件,默认他是不会自动添加到 git 里面进行管理,而是需要你通过 git add 到里面。
如果是在项目初始化时,我们一般都直接使用 git add . 来把整合项目添加进去。
四、代码提交
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/Szib8ySqErWKL2Az6xbJJaDIA9kWceSXibQa8ozJhZRW2j2K7JvQAsUh0DsOyBfBgC6RXrHISYRjSOvF0v3U6nB5pUeybKgarG/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;"># 提交暂存区到仓库区</span><br/>$ git commit -m [message]<br/><span style="color: #75715e;line-height: 26px;"># 提交暂存区的指定文件到仓库区</span><br/>$ git commit [file1] [file2] ... -m [message]<br/><span style="color: #75715e;line-height: 26px;"># 提交工作区自上次commit之后的变化,直接到仓库区</span><br/>$ git commit <span style="line-height: 26px;">-a</span><br/><span style="color: #75715e;line-height: 26px;"># 提交时显示所有diff信息</span><br/>$ git commit -v<br/><span style="color: #75715e;line-height: 26px;"># 使用一次新的commit,替代上一次提交</span><br/><span style="color: #75715e;line-height: 26px;"># 如果代码没有任何新变化,则用来改写上一次commit的提交信息</span><br/>$ git commit --amend -m [message]<br/><span style="color: #75715e;line-height: 26px;"># 重做上一次commit,并包括指定文件的新变化</span><br/>$ git commit --amend [file1] [file2]<br/> |
五、分支
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/Szib8ySqErWKL2Az6xbJJaDIA9kWceSXibQa8ozJhZRW2j2K7JvQAsUh0DsOyBfBgC6RXrHISYRjSOvF0v3U6nB5pUeybKgarG/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;"># 显示所有本地分支</span><br/>$ git branch<br/><span style="color: #75715e;line-height: 26px;"># 列出所有远程分支</span><br/>$ git branch -r<br/><span style="color: #75715e;line-height: 26px;"># 列出所有本地分支和远程分支</span><br/>$ git branch -a<br/><span style="color: #75715e;line-height: 26px;"># 新建一个分支,但依然停留在当前分支</span><br/>$ git branch [branch-name]<br/><span style="color: #75715e;line-height: 26px;"># 新建一个分支,与指定的远程分支建立追踪关系</span><br/>$ git branch --track [branch] [remote-branch]<br/><span style="color: #75715e;line-height: 26px;"># 删除分支</span><br/>$ git branch -d [branch-name]<br/><span style="color: #75715e;line-height: 26px;"># 删除远程分支</span><br/>$ git push origin --delete [branch-namel<br/>$ git branch -dr [remote/branch]<br/><span style="color: #75715e;line-height: 26px;"># 新建一个分支,并切换到该分支</span><br/>$ git checkout -b [branch]<br/><span style="color: #75715e;line-height: 26px;"># 切换到指定分支,并更新工作区</span><br/>$ git checkout [branch-name]<br/><span style="color: #75715e;line-height: 26px;"># 切换到上一个分支</span><br/>$ git checkout -<br/><span style="color: #75715e;line-height: 26px;"># 建立追踪关系,在现有分支与指定的远程分支之间</span><br/>$ git branch --<span style="color: #a6e22e;line-height: 26px;">set</span>-upstream [branch] [remote-branch]<br/><span style="color: #75715e;line-height: 26px;"># 合并指定分支到当前分支</span><br/>$ git merge [branch]<br/><span style="color: #75715e;line-height: 26px;"># 衍合指定分支到当前分支</span><br/>$ git rebase <branch><br/><span style="color: #75715e;line-height: 26px;"># 选择一个commit,合并进当前分支</span><br/>$ git cherry-pick [commit] |
关于分支我更喜欢使用工具去处理切换合并这些,哈哈,偷懒。
六、标签
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/Szib8ySqErWKL2Az6xbJJaDIA9kWceSXibQa8ozJhZRW2j2K7JvQAsUh0DsOyBfBgC6RXrHISYRjSOvF0v3U6nB5pUeybKgarG/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;"># 列出所有本地标签 </span><br/>$ git tag <br/><span style="color: #75715e;line-height: 26px;"># 基于最新提交创建标签 </span><br/>$ git tag <tagname> <br/><span style="color: #75715e;line-height: 26px;"># 删除标签</span><br/>$ git tag -d <tagname> <br/><span style="color: #75715e;line-height: 26px;"># 删除远程tag</span><br/>$ git push origin :refs/tags/[tagName]<br/><span style="color: #75715e;line-height: 26px;"># 查看tag信息</span><br/>$ git show [tag]<br/><span style="color: #75715e;line-height: 26px;"># 提交指定tag</span><br/>$ git push [remote] [tag]<br/><span style="color: #75715e;line-height: 26px;"># 提交所有tag</span><br/>$ git push [remote] --tags<br/><span style="color: #75715e;line-height: 26px;"># 新建一个分支,指向某个tag</span><br/>$ git checkout -b [branch] [tag] |
标签一般在我们需要发版,或者代码里程碑时使用到,目的是标记一个时间点,等后期可以快速定位到这个点的代码。
七、查看信息
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/Szib8ySqErWKL2Az6xbJJaDIA9kWceSXibQa8ozJhZRW2j2K7JvQAsUh0DsOyBfBgC6RXrHISYRjSOvF0v3U6nB5pUeybKgarG/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;"># 显示有变更的文件</span><br/>$ git status<br/><span style="color: #75715e;line-height: 26px;"># 显示当前分支的版本历史</span><br/>$ git <span style="color: #a6e22e;line-height: 26px;">log</span><br/><span style="color: #75715e;line-height: 26px;"># 显示commit历史,以及每次commit发生变更的文件</span><br/>$ git <span style="color: #a6e22e;line-height: 26px;">log</span> --<span style="color: #a6e22e;line-height: 26px;">stat</span><br/><span style="color: #75715e;line-height: 26px;"># 搜索提交历史,根据关键词</span><br/>$ git <span style="color: #a6e22e;line-height: 26px;">log </span><span style="line-height: 26px;">-s</span> [keyword]<br/><span style="color: #75715e;line-height: 26px;"># 显示某个commit之后的所有变动,每个commit占据一行</span><br/>$ git <span style="color: #a6e22e;line-height: 26px;">log </span>[tag] HEAD --pretty=format:%s<br/><span style="color: #75715e;line-height: 26px;"># 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件</span><br/>$ git <span style="color: #a6e22e;line-height: 26px;">log </span>[tag] HEAD --grep feature<br/><span style="color: #75715e;line-height: 26px;"># 显示某个文件的版本历史,包括文件改名</span><br/>$ git <span style="color: #a6e22e;line-height: 26px;">log</span> --follow [file]<br/>$ git whatchanged [file]<br/><span style="color: #75715e;line-height: 26px;"># 显示指定文件相关的每一次diff</span><br/>$ git <span style="color: #a6e22e;line-height: 26px;">log</span> -p [file]<br/><span style="color: #75715e;line-height: 26px;"># 显示过去5次提交</span><br/>$ git <span style="color: #a6e22e;line-height: 26px;">log</span> -5 --pretty--oneline<br/><span style="color: #75715e;line-height: 26px;"># 显示所有提交过的用户,按提交次数排序</span><br/>$ git shortlog -sn<br/><span style="color: #75715e;line-height: 26px;"># 显示指定文件是什么人在什么时间修改过</span><br/>$ git blame [file]<br/><span style="color: #75715e;line-height: 26px;"># 显示暂存区和工作区的差异</span><br/>$ git diff<br/><span style="color: #75715e;line-height: 26px;"># 显示暂存区和上一个commit的差异</span><br/>$ git diff --cached [file]<br/><span style="color: #75715e;line-height: 26px;"># 显示工作区与当前分支最新commit之间的差异</span><br/>$ git diff HEAD<br/><span style="color: #75715e;line-height: 26px;"># 显示两次提交之间的差异</span><br/>$ git diff [first-branch]...[second-branch]<br/><span style="color: #75715e;line-height: 26px;"># 显示今天你写了多少行代码</span><br/>$ git diff --shortstat <span style="color: #a6e22e;line-height: 26px;">"@{0 day ago}"</span><br/><span style="color: #75715e;line-height: 26px;"># 显示某次提交的元数据和内容变化</span><br/>$ git show [commit]<br/><span style="color: #75715e;line-height: 26px;"># 显示某次提交发生变化的文件</span><br/>$ git show --name-only [commit]<br/><span style="color: #75715e;line-height: 26px;"># 显示某次提交时,某个文件的内容</span><br/>$ git show [commit]:[filename]<br/><span style="color: #75715e;line-height: 26px;"># 显示当前分支的最近几次提交</span><br/>$ git reflog<br/> |
这部分的命令用的并不太多,作为了解就好!
八、远程操作
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/Szib8ySqErWKL2Az6xbJJaDIA9kWceSXibQa8ozJhZRW2j2K7JvQAsUh0DsOyBfBgC6RXrHISYRjSOvF0v3U6nB5pUeybKgarG/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;"># 下载远程仓库的所有变动</span><br/>$ git fetch [remote]<br/><span style="color: #75715e;line-height: 26px;"># 取回远程仓库的变化,并与本地分支合并</span><br/>$ git pull [remote] [branch]<br/><span style="color: #75715e;line-height: 26px;"># 显示所有远程仓库</span><br/>$ git remote -v<br/><span style="color: #75715e;line-height: 26px;"># 显示某个远程仓库的信息</span><br/>$ git remote show [remote]<br/><span style="color: #75715e;line-height: 26px;"># 增加一个新的远程仓库,并命名</span><br/>$ git remote add [shortname] [url]<br/><span style="color: #75715e;line-height: 26px;"># 上传本地指定分支到远程仓库</span><br/>$ git push [remote] [branch]<br/><span style="color: #75715e;line-height: 26px;"># 强行推送当前分支到远程仓库,即使有冲突</span><br/>$ git push [remote] --force<br/><span style="color: #75715e;line-height: 26px;"># 推送所有分支到远程仓库</span><br/>$ git push [remote] --all<br/><span style="color: #75715e;line-height: 26px;"># 删除远程分支或标签 </span><br/>$ git push <remote> :<branch/tag-name> <br/><span style="color: #75715e;line-height: 26px;"># 上传所有标签</span><br/>$ git push --tags <br/> |
这里面的命令我们用得最多就是 git push,其他的都用得非常少,作为了解就可以了。
九、撤销
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/Szib8ySqErWKL2Az6xbJJaDIA9kWceSXibQa8ozJhZRW2j2K7JvQAsUh0DsOyBfBgC6RXrHISYRjSOvF0v3U6nB5pUeybKgarG/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;"># 撤销工作目录中所有未提交文件的修改内容 </span><br/>$ git reset --hard HEAD <br/><span style="color: #75715e;line-height: 26px;"># 撤销指定的未提交文件的修改内容 </span><br/>$ git checkout HEAD <file> <br/><span style="color: #75715e;line-height: 26px;"># 撤销指定的提交 </span><br/>$ git revert <commit> <br/><span style="color: #75715e;line-height: 26px;"># 退回到之前1天的版本</span><br/>$ git <span style="color: #a6e22e;line-height: 26px;">log </span>--before=<span style="color: #a6e22e;line-height: 26px;">"1 days"</span><br/><span style="color: #75715e;line-height: 26px;"># 恢复暂存区的指定文件到工作区</span><br/>$ git checkout [file]<br/><span style="color: #75715e;line-height: 26px;"># 恢复某个commit的指定文件到暂存区和工作区</span><br/>$ git checkout [commit] [file]<br/><span style="color: #75715e;line-height: 26px;"># 恢复暂存区的所有文件到工作区</span><br/>$ git checkout.<br/><span style="color: #75715e;line-height: 26px;"># 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变</span><br/>$ git reset [file]<br/><span style="color: #75715e;line-height: 26px;"># 重置暂存区与工作区,与上一次commit保持一致</span><br/>$ git reset --hard<br/><span style="color: #75715e;line-height: 26px;"># 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变</span><br/>$ git reset [commit]<br/><span style="color: #75715e;line-height: 26px;"># 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致</span><br/>$ git reset --hard [commit]<br/><span style="color: #75715e;line-height: 26px;"># 重置当前HEAD为指定commit,但保持暂存区和工作区不变</span><br/>$ git reset --keep [commit]<br/><span style="color: #75715e;line-height: 26px;"># 新建一个commit,用来撤销指定commit</span><br/><span style="color: #75715e;line-height: 26px;"># 后者的所有变化都将被前者抵消,并且应用到当前分支</span><br/>$ git revert [commit]<br/><span style="color: #75715e;line-height: 26px;"># 暂时将未提交的变化移除,稍后再移入 <br/></span>$ git stash<br/>$ git stash pop<br/> |
像代码回滚这类危险操作,个人建议新手还是使用工具操作,别问为什么,问就说明你坑踩得不够,哈哈。
十、其他
生成一个可供发布的压缩包。
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/Szib8ySqErWKL2Az6xbJJaDIA9kWceSXibQa8ozJhZRW2j2K7JvQAsUh0DsOyBfBgC6RXrHISYRjSOvF0v3U6nB5pUeybKgarG/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;">$ git archive<br/> |
这个一般用得很少,现在基本都用 CICD 自动构建了,发布这些基本都是使用镜像发布了,Git 现在大都只用作代码托管用。
参考文献:
https://git-scm.com/docs