git使用杂记

newvnartr 7年前
   <h3>前言</h3>    <p>作为一个开发者,如果现在还不知道git或者还不会使用git,那么你应该好好的反省。自己去好好看一遍的入门介绍吧。今天只是对自己在日常中使用git的一些常用命令的介绍与自己认为不错且能提高我们办公效率的命令。内容可能会有点杂乱,但绝对都是经典的命令,在此记下笔记,也希望能帮助来赏脸关顾的你们。</p>    <p>区域</p>    <p>在这之前,来介绍一下git的三个区域</p>    <ul>     <li>工作区(working directory)</li>     <li>暂存区(stage index)</li>     <li>本地历史区(history)</li>    </ul>    <p>通过一张图就能简洁易懂的明白它们之间的转化。</p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/24c829c692f6ac6dc4dfec9f6666a8a2.png"></p>    <p><strong>clone</strong></p>    <p>先从clone命令来介绍,使用过git的都知道它。git colne命令拉取远程仓库到本地。但当我们要拉取到指定的文件夹下时,你可能会直接mkdir,其实无需如此,一条命令就能搞定git clone 远程仓库 文件名,就是如此简单。</p>    <p><strong>rm</strong></p>    <p>我们在工作中可能会遇到这么一种情况,使用git add .直接将工作区的所有修改的文件加入到暂存区了,但是后面发现有一个文件先不要加进去,此时我们就可以使用如下命令就可以将该文件退回到工作区中。</p>    <pre>  git rm --cached <file>   </pre>    <p><strong>stash</strong></p>    <p>有这么一种情况,当你正在开发中时,有一个线上的紧急bug需要修复,此时开发中的功能又没有完成你不想提交,此时你可以使用git stash将工作区的文件都存放起来。这时你就可以放心的去切分支修复bug,修复完之后执行git stash pop可以将先前存放的取出,当然也有一些其他的相关命令例如:git stash list查看存放的记录,git stash drop丢弃存放的记录。</p>    <p><strong>tag</strong></p>    <p>可能在开发中我们要打标签git tag tagName,并且要将相应的标签推送到远程仓库中,此时可以使用如下命令进行推送。</p>    <pre>  git push --tags tagName   </pre>    <p><strong>amend</strong></p>    <p>当你commit以后,发现有一个文件没有加进上次的commit中,或者又修改了一些文件。此时你并不想增加新的commit信息,只是想将其加入到上次的commit中。这时你就可以使用</p>    <pre>  git commit --amend <file>   </pre>    <p>将暂存区的文件加入其中,并且你也可以修改此时的commit信息。</p>    <p><strong>reset</strong></p>    <p>reset也能实现前面的rm的效果,可以使用如下命令来替代前面的git rm --cached <file>命令</p>    <pre>  git reset HEAD <file>   </pre>    <p>但reset用途更广,结合soft参数可以回撤到任意的commit节点进行操作</p>    <pre>  git reset --soft index   </pre>    <p>执行该命令之后,就回到index处,工作区不变、暂存区回到当时的index处。另外还有一个hard参数。</p>    <pre>  git reset --hard index   </pre>    <p>与soft可以说对立,它的效果就在于工作区与暂存区的不同,它会清空这两个区。</p>    <p><strong>rebase</strong></p>    <p>对于rebase是重定向的意思,如果你当前的分支与远程的分支commit信息存在差异时,会提醒你此时不能进行push,必须先将远程的commit信息拉去到本地来,才能进行提交。对于这种情况就可以使用rebase命令了。如下当前处在develop分支</p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/32e8d7dd8968efed2cfde9768c80a8f2.png"></p>    <p>此时应该先执行rebase命令</p>    <pre>  git fetch   git rebase origin/master    </pre>    <p>执行完之后,最后再push到远程master</p>    <pre>  git push origin master   </pre>    <p>最终各个分支的情况就是上图的效果了。如果觉得命令多难记,这里也可用一条命令来完成上面的效果</p>    <pre>  git pull --rebase origin master   </pre>    <p>这是rebase的简单运用,也是常见的命令了。下面介绍rebase的一个可选参数--onto。</p>    <p>--onto</p>    <p>使用场景:开发过程中我们都会创建不同的分支进行开发不同的功能,当你在分支A上创建了新分支B进行开发功能并且也提交了一些commit时,此时你发现原来A分支上有错误的commit,如果要rebase到master上时,不能将这个错误的commit也附带上。这个时候就该--onto大显神通了。</p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/5c4c0a83fd62db988dddb39ef927d2c6.png"></p>    <p>当前处在B分支,要得到上面的结果,只需执行如下命令</p>    <pre>  git rebase --onto master <b的commit hash code> B   </pre>    <p>这个不仅可以针对不同的分支,也能作用于同一个分支上。所以针对上面的情况可以只对分支B进行操作,等价命令如下:</p>    <pre>  git rebase --onto <a的commit hash code> <b的commit hash code> B   </pre>    <p>--interactive</p>    <p>当我们要修改commit信息的名称时,如果要修改的commit处在第一个时,可以使用</p>    <pre>  git commit --amend   </pre>    <p>如果不是第一个时,我们就要使用到rebase的--interactive可选参数了,可以简写为-i。</p>    <pre>  git rebase -i <commit hash code>   </pre>    <p>参数后面的commit hash code为需要修改的commit的前一个。执行之后就会出现如下类似的信息:</p>    <pre>  pick 137cf0a First coommit   pick 163dc38 Second commit      # Rebase f9aee6e..163dc38 onto f9aee6e (2 command(s))   #   # Commands:   # p, pick = use commit   # r, reword = use commit, but edit the commit message   # e, edit = use commit, but stop for amending   # s, squash = use commit, but meld into previous commit   # f, fixup = like "squash", but discard this commit's log message   # x, exec = run command (the rest of the line) using shell   #   # These lines can be re-ordered; they are executed from top to bottom.   #   # If you remove a line here THAT COMMIT WILL BE LOST.   #   # However, if you remove everything, the rebase will be aborted.   #   # Note that empty commits are commented out    </pre>    <p>根据提示我们可以有6个可选择的操作。相信提示已经说的很明显了,对于我们这种要修改First coommit的情况,需要使用r。</p>    <pre>  r 137cf0a First commit   pick 163dc38 Second commit    </pre>    <p>执行之后会跳到修该First coomit的界面,进行修改即可。</p>    <pre>  First commit      # Please enter the commit message for your changes. Lines starting   # with '#' will be ignored, and an empty message aborts the commit.   #   # Date:      Thu Jan 26 23:07:10 2017 +0800   #   # rebase in progress; onto f9aee6e   # You are currently editing a commit while rebasing branch 'master' on 'f9aee6e'.   #   # Changes to be committed:   #       new file:   file1    </pre>    <p>至于其他的操作项,有兴趣的可以自己去尝试一下。例如s操作就可以用来合并commit。</p>    <p><strong>branch</strong></p>    <p>相信branch都很熟悉,我这里要说的是他的另一种可能会用到的情况。场景是这样的:如果在你进行创建新的分支时,并不想从当前的commit信息节点进行创建分支。</p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/f39d816d8d8776e298e9eb5fb8d7a554.png"></p>    <p>要实现如上效果只需在创建分支时在后面再添加额外的参数,该参数就是你所需调到的commit节点的hash code</p>    <pre>  git branch new_branch <commit hash code>   </pre>    <p><strong>push</strong></p>    <p>这里提一下push的--set-upstream,它的效果是设置上游分支,当我们将远程不存在的本地分支推送到远程时,如果不在推送的分支上,我们一般会使用如下命令进行推送。</p>    <pre>  git checkout push_branch   git push origin push_branch    </pre>    <p>下面是简洁的方法,使用该参数无需切换分支,可以直接使用如下命令进行推送。</p>    <pre>  git push --set-upstream origin push_branch   </pre>    <p><strong>cherry-pick</strong></p>    <p>这个命令的场景是:当你所在的分支没用,你要删除它,但其中的一个commit你还是想推送到远程master上。</p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/7ff56f452206c7985132bd0ff342ea72.png"></p>    <p>将分支切换到master,执行以下命令:</p>    <pre>  git cherry-pick <b的 commit hash code>   </pre>    <p><strong>merge</strong></p>    <p>我们所熟知的是使用merge来进行分支的合并,每次使用merge时都会自动将副分支合并成一个commit进行推送到主分支上,那么如果我不想它自动推送到主分支上时(可能我还需要进行修改),这时就可以使用--squash操作</p>    <pre>  git merge --squash dev_branch   </pre>    <p>执行完以上命令后,我们就可以在暂存区看到一个还未提交的文件状态。</p>    <p><strong>reflog</strong></p>    <p>当我们切分支太频繁了之后,可能会忘了一些分支是从哪个分支切过来的,此时可以使用如下命令查看:</p>    <pre>  git reflog    </pre>    <pre>  894a16d HEAD@{0}: commit: commit another todo   6876e5b HEAD@{1}: checkout: moving from solve_world_hunger to kill_the_batman   324336a HEAD@{2}: commit: commit todo   6876e5b HEAD@{3}: checkout: moving from blowup_sun_for_ransom to solve_world_hunger   6876e5b HEAD@{4}: checkout: moving from kill_the_batman to blowup_sun_for_ransom   6876e5b HEAD@{5}: checkout: moving from cure_common_cold to kill_the_batman   6876e5b HEAD@{6}: commit (initial): initial commit    </pre>    <p>这样我们就可以看到所用的操作历史了。这样如果我们使用git reset命令不小心删除了需要的东西。可以通过此来查找到删除操作的hash code,之后就可以通过如下命令进行恢复。</p>    <pre>  git checkout <hash code>   </pre>    <p>目前想到的就这些了,希望能有所帮助</p>    <p> </p>    <p>来自:http://mobile.51cto.com/android-530100.htm</p>    <p> </p>