123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808 |
- <!DOCTYPE HTML>
- <html lang="" >
- <head>
- <meta charset="UTF-8">
- <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
- <title>git~F.A.Q · 代码收集</title>
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <meta name="description" content="">
- <meta name="generator" content="GitBook 3.1.1">
-
-
-
-
- <link rel="stylesheet" href="../gitbook/style.css">
-
-
-
- <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
-
-
-
- <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <meta name="HandheldFriendly" content="true"/>
- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
- <meta name="apple-mobile-web-app-capable" content="yes">
- <meta name="apple-mobile-web-app-status-bar-style" content="black">
- <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
- <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
-
- <link rel="next" href="../node/0.html" />
-
-
- <link rel="prev" href="push.html" />
-
- </head>
- <body>
-
- <div class="gb-page-wrapper">
- <header class="gb-page-header">
- <div class="container">
-
-
- <div id="book-search-input" role="search">
- <input type="text" placeholder="Type to search" />
- </div>
- <a href=".." class="logo">
- <img src="../gitbook/images/logo/128.png">
- <h1>代码收集</h1>
- </a>
-
-
- <a href="https://github.com/seamong/myBlogs/blob/doc/git/faq.md" target="_blank" class="btn btn-link pull-right hidden-xs">
- <i class="octicon octicon-mark-github"></i> 在GitHub编辑本页
- </a>
- <a href="../faq.md" class="btn btn-link pull-right hidden-xs">
- F.A.Q
- </a>
- <a href="https://github.com/seamong/myBlogs/README.md" target="_blank" class="btn btn-link pull-right hidden-xs">
- 当前版本0.0.1
- </a>
-
- </div>
- </header>
- <div class="gb-page-body">
- <div class="gb-page-inner">
- <div class="container">
- <div class="row">
- <div class="col-md-3">
-
-
- <div class="panel panel-default">
-
- <div class="panel-heading">
- <h3 class="panel-title">介绍</h3>
- </div>
-
- <div class="list-group">
-
-
-
- <a href="../" class="list-group-item ">
-
-
-
- 关于本文档
-
- </a>
-
-
-
- </div>
- </div>
-
- <div class="panel panel-default">
-
- <div class="panel-heading">
- <h3 class="panel-title">JavaScript</h3>
- </div>
-
- <div class="list-group">
-
-
-
- <a href="../JavaScript/principle/" class="list-group-item ">
-
-
-
- <i class="octicon octicon-chevron-right"></i>
-
- 原理解析
-
- </a>
-
-
-
-
- <a href="../JavaScript/cases/" class="list-group-item ">
-
-
-
- <i class="octicon octicon-chevron-right"></i>
-
- 案例解析
-
- </a>
-
-
-
- </div>
- </div>
-
- <div class="panel panel-default">
-
- <div class="panel-heading">
- <h3 class="panel-title">angular</h3>
- </div>
-
- <div class="list-group">
-
-
-
- <a href="../angular/study_notes/" class="list-group-item ">
-
-
-
- <i class="octicon octicon-chevron-right"></i>
-
- Study notes
-
- </a>
-
-
-
-
- <a href="../angular/ng.html" class="list-group-item ">
-
-
-
- ng
-
- </a>
-
-
-
- </div>
- </div>
-
- <div class="panel panel-default">
-
- <div class="panel-heading">
- <h3 class="panel-title">css</h3>
- </div>
-
- <div class="list-group">
-
-
-
- <a href="../css/principle/" class="list-group-item ">
-
-
-
- 原理解析
-
- </a>
-
-
-
-
- <a href="../css/cases/" class="list-group-item ">
-
-
-
- <i class="octicon octicon-chevron-right"></i>
-
- 案例收集
-
- </a>
-
-
-
- </div>
- </div>
-
- <div class="panel panel-default">
-
- <div class="panel-heading">
- <h3 class="panel-title">vue</h3>
- </div>
-
- <div class="list-group">
-
-
-
- <a href="../vue/components/" class="list-group-item ">
-
-
-
- <i class="octicon octicon-chevron-right"></i>
-
- components
-
- </a>
-
-
-
-
- <a href="../vue/router/" class="list-group-item ">
-
-
-
- <i class="octicon octicon-chevron-right"></i>
-
- router
-
- </a>
-
-
-
-
- <a href="../vue/vuex/" class="list-group-item ">
-
-
-
- VUE
-
- </a>
-
-
-
- </div>
- </div>
-
- <div class="panel panel-default">
-
- <div class="panel-heading">
- <h3 class="panel-title">ubuntu</h3>
- </div>
-
- <div class="list-group">
-
-
-
- <a href="../ubuntu/serves/" class="list-group-item ">
-
-
-
- <i class="octicon octicon-chevron-right"></i>
-
- 服务器生存指南
-
- </a>
-
-
-
-
- <a href="../ubuntu/instruction.html" class="list-group-item ">
-
-
-
- 常用指令
-
- </a>
-
-
-
-
- <a href="../ubuntu/software/" class="list-group-item ">
-
-
-
- 常用软件安装
-
- </a>
-
-
-
- </div>
- </div>
-
- <div class="panel panel-default">
-
- <div class="panel-heading">
- <h3 class="panel-title">GIT</h3>
- </div>
-
- <div class="list-group">
-
-
-
- <a href="git.html" class="list-group-item ">
-
-
-
- git 命令
-
- </a>
-
-
-
-
- <a href="push.html" class="list-group-item ">
-
-
-
- git~push
-
- </a>
-
-
-
-
- <a href="faq.html" class="list-group-item active">
-
-
-
- git~F.A.Q
-
- </a>
-
-
-
- </div>
- </div>
-
- <div class="panel panel-default">
-
- <div class="panel-heading">
- <h3 class="panel-title">NODE&NPM</h3>
- </div>
-
- <div class="list-group">
-
-
-
- <a href="../node/0.html" class="list-group-item ">
-
-
-
- <i class="octicon octicon-chevron-right"></i>
-
- node
-
- </a>
-
-
-
-
- <a href="../node/npm/0.html" class="list-group-item ">
-
-
-
- npm
-
- </a>
-
-
-
- </div>
- </div>
-
- <div class="panel panel-default">
-
- <div class="list-group">
-
-
-
- <a href="../other/faq.html" class="list-group-item ">
-
-
-
- FAQ
-
- </a>
-
-
-
-
- <a href="../other/examples.html" class="list-group-item ">
-
-
-
- Examples
-
- </a>
-
-
-
- </div>
- </div>
-
- </div>
- <div class="col-md-9">
-
- <div id="book-search-results">
- <div class="search-noresults">
-
-
- <div class="gb-markdown book-page-inner">
- <h1 id="gitfaq">git~F.A.Q</h1>
- <p>在git的一般使用中,如果发现错误的将不想提交的文件add进入index之后,想回退取消,则可以使用命令:git reset HEAD <file>...,同时git add完毕之后,git也会做相应的提示,比如:
- 引用</file></p>
- <h1 id="changes-to-be-committed">Changes to be committed:</h1>
- <h1 id="use-git-reset-head-to-unstage">(use "git reset HEAD<file>..." to unstage)</file></h1>
- <p> #</p>
- <h1 id="new-file-testscala">new file: Test.scala</h1>
- <p>git reset [--hard|soft|mixed|merge|keep] [<commit>或HEAD]:将当前的分支重设(reset)到指定的<commit>或者HEAD(默认,如果不显示指定commit,默认是HEAD,即最新的一次提交),并且根据[mode]有可能更新index和working directory。
- 下面列出一些git reset的典型的应用场景:
- A) 回滚add操纵
- 引用</commit></commit></p>
- <pre><code class="lang-bash"><span class="hljs-comment"># 编辑文件frotz.c, filfre.c,做了些更改,并把更改添加到了index</span>
- edit
- git add frotz.c filfre.c
- <span class="hljs-comment"># 查看邮件,发现某人要你pull,有一些改变需要你merge下来</span>
- mailx
- <span class="hljs-comment"># 然而,你已经把index搞乱了,因为index同HEAD commit不匹配了,</span>
- <span class="hljs-comment"># 但是你知道,即将pull的东西不会影响已经修改的frotz.c和filfre.c,</span>
- <span class="hljs-comment"># 因此你可以revert这两个文件的改变。revert后,</span>
- <span class="hljs-comment"># 那些改变应该依旧在working directory中,因此执行git reset。</span>
- git reset
- <span class="hljs-comment"># 然后,执行了pull之后,自动merge,</span>
- <span class="hljs-comment"># frotz.c和filfre.c这些改变依然在working directory中。</span>
- $ git pull git://info.example.com/ nitfol (4)
- </code></pre>
- <p>B) 回滚最近一次commit
- 引用</p>
- <pre><code class="lang-bash">git commit ...
- <span class="hljs-comment"># 当提交了之后,你又发现代码没有提交完整,</span>
- <span class="hljs-comment"># 或者你想重新编辑一下提交的comment,</span>
- <span class="hljs-comment"># 执行git reset --soft HEAD^,</span>
- <span class="hljs-comment"># 让working tree还跟reset之前一样,不作任何改变。</span>
- <span class="hljs-comment"># HEAD^指向HEAD之前最近的一次commit。</span>
- git reset --soft HEAD^
- <span class="hljs-comment"># 对working tree下的文件做修改</span>
- edit
- <span class="hljs-comment"># 然后使用reset之前那次commit的注释、作者、日期等信息重新提交。</span>
- <span class="hljs-comment"># 注意,当执行git reset命令时,git会把老的HEAD拷贝到文件.git/ORIG_HEAD中,</span>
- <span class="hljs-comment"># 在命令中可以使用ORIG_HEAD引用这个commit。</span>
- <span class="hljs-comment"># commit 命令中 -a 参数的意思是告诉git,</span>
- <span class="hljs-comment"># 自动把所有修改的和删除的文件都放进stage area,未被git跟踪的新建的文件不受影响。</span>
- <span class="hljs-comment"># commit命令中-c <commit> 或者 -C <commit>意思是拿</span>
- <span class="hljs-comment"># 已经提交的commit对象中的信息(作者,提交者,注释,时间戳等)提交,</span>
- <span class="hljs-comment"># 那么这条commit命令的意思就非常清晰了,把所有更改的文件加入stage area,</span>
- <span class="hljs-comment"># 并使用上次的提交信息重新提交。</span>
- git commit <span class="hljs-_">-a</span> -c ORIG_HEAD
- </code></pre>
- <p>C) 回滚最近几次commit,并把这几次commit放到叫做topic的branch上去。
- 引用</p>
- <pre><code class="lang-bash"><span class="hljs-comment"># 你已经提交了一些commit,但是此时发现这些commit还不够成熟,</span>
- <span class="hljs-comment"># 不能进入master分支,但你希望在新的branch上润色这些commit改动。</span>
- <span class="hljs-comment"># 因此执行了git branch命令在当前的HEAD上建立了新的叫做 topic/wip的分支。</span>
- git branch topic/wip
- <span class="hljs-comment"># 然后回滚master branch上的最近三次提交。</span>
- <span class="hljs-comment"># HEAD~3指向当前HEAD-3个commit的commit,</span>
- <span class="hljs-comment"># git reset --hard HEAD~3即删除最近的三个commit</span>
- <span class="hljs-comment">#(删除HEAD, HEAD^, HEAD~2),将HEAD指向HEAD~3。</span>
- git reset --hard HEAD~3
- <span class="hljs-comment"># 切换到`topic/wip` 分支</span>
- $ git checkout topic/wip
- </code></pre>
- <p>D) 永久删除最后几个commit</p>
- <pre><code class="lang-bash">git commit ...
- <span class="hljs-comment"># 最后三个commit(即HEAD, HEAD^和HEAD~2)提交有问题,你想永久删除这三个commit。</span>
- git reset --hard HEAD~3 (1)
- </code></pre>
- <p>E) 回滚merge和pull操作
- 引用</p>
- <pre><code class="lang-bash"><span class="hljs-comment"># 从origin拉下来一些更新,但是产生了很多冲突,</span>
- <span class="hljs-comment"># 你暂时没有这么多时间去解决这些冲突,因此你决定稍候有空的时候再重新pull。</span>
- $ git pull
- Auto-merging nitfol
- CONFLICT (content): Merge conflict innitfol
- Automatic merge failed; fix conflicts andthen commit the result.
- <span class="hljs-comment"># 由于pull操作产生了冲突,因此所有pull下来的改变尚未提交,</span>
- <span class="hljs-comment"># 仍然再stage area中,</span>
- <span class="hljs-comment"># 这种情况下git reset --hard与 git reset --hard HEAD意思相同,</span>
- <span class="hljs-comment"># 即都是清除index和working tree中被搞乱的东西。</span>
- $ git reset --hard
- <span class="hljs-comment"># 将topic/branch合并到当前的branch,这次没有产生冲突,并且合并后的更改自动提交。</span>
- $ git pull . topic/branch
- Updating from 41223... to 13134...
- Fast-forward
- <span class="hljs-comment"># 但是此时你又发现将topic/branch合并过来为时尚早,</span>
- <span class="hljs-comment"># 因此决定退滚merge,执行git reset --hard ORIG_HEAD回滚刚才的pull/merge操作。</span>
- <span class="hljs-comment"># 说明:前面讲过,执行git reset时,</span>
- <span class="hljs-comment"># git会把reset之前的HEAD放入.git/ORIG_HEAD文件中,</span>
- <span class="hljs-comment"># 命令行中使用ORIG_HEAD引用这个commit。</span>
- <span class="hljs-comment"># 同样的,执行pull和merge操作时,</span>
- <span class="hljs-comment"># git都会把执行操作前的HEAD放入ORIG_HEAD中,以防回滚操作。</span>
- $ git reset --hard ORIG_HEAD
- </code></pre>
- <p>F) 在被污染的working tree中回滚merge或者pull
- 引用</p>
- <pre><code class="lang-bash"><span class="hljs-comment"># 即便你已经在本地更改了一些你的working tree,</span>
- <span class="hljs-comment"># 你也可安全的git pull,</span>
- <span class="hljs-comment"># 前提是你知道将要pull的内容不会覆盖你的working tree中的内容。</span>
- $ git pull
- Auto-merging nitfol
- Merge made by recursive.
- nitfol | 20 +++++----
- ...
- <span class="hljs-comment"># git pull完后,你发现这次pull下来的修改不满意,</span>
- <span class="hljs-comment"># 想要回滚到pull之前的状态,从前面的介绍知道,</span>
- <span class="hljs-comment"># 我们可以执行git reset --hard ORIG_HEAD,</span>
- <span class="hljs-comment"># 但是这个命令有个副作用就是清空你的working tree,</span>
- <span class="hljs-comment"># 即丢弃你的本地未add的那些改变。为了避免丢弃working tree中的内容,</span>
- <span class="hljs-comment"># 可以使用git reset --merge ORIG_HEAD,</span>
- <span class="hljs-comment"># 注意其中的--hard 换成了--merge,</span>
- <span class="hljs-comment"># 这样就可以避免在回滚时清除working tree。</span>
- $ git reset --merge ORIG_HEAD (2)
- </code></pre>
- <p>G) 被中断的工作流程
- 在实际开发中经常出现这样的情形:你正在开发一个大的feature,此时来了一个紧急的bug需要修复,但是目前在working tree中的内容还没有成型,还不足以commit,但是你又必须切换的另外的branch去fix bug。请看下面的例子</p>
- <pre><code class="lang-bash">$ git checkout feature ;<span class="hljs-comment"># you were workingin "feature" branch and</span>
- $ work work work ;<span class="hljs-comment">#got interrupted</span>
- <span class="hljs-comment"># 这次属于临时提交,因此随便添加一个临时注释即可。</span>
- $ git commit <span class="hljs-_">-a</span> -m <span class="hljs-string">"snapshot WIP"</span>
- $ git checkout master
- $ fix fix fix
- $ git commit ;<span class="hljs-comment"># commit with real log</span>
- $ git checkout feature
- <span class="hljs-comment"># 这次reset删除了WIP commit,并且把working tree设置成提交WIP快照之前的状态。</span>
- $ git reset --soft HEAD^ ;<span class="hljs-comment"># go back to WIPstate</span>
- <span class="hljs-comment"># 此时,在index中依然遗留着“snapshot WIP”提交时所做的uncommit changes,</span>
- <span class="hljs-comment"># git reset将会清理index成为尚未提交"snapshot WIP"时的状态便于接下来继续工作。</span>
- $ git reset
- </code></pre>
- <p>(H) Reset单独的一个文件
- 假设你已经添加了一个文件进入index,但是而后又不打算把这个文件提交,此时可以使用git reset把这个文件从index中去除。</p>
- <pre><code class="lang-bash"><span class="hljs-comment"># 把文件frotz.c从index中去除,</span>
- $ git reset -- frotz.c
- <span class="hljs-comment"># 把index中的文件提交</span>
- $ git commit -m <span class="hljs-string">"Commit files inindex"</span>
- <span class="hljs-comment"># 再次把frotz.c加入index</span>
- $ git add frotz.c
- </code></pre>
- <p>(I) 保留working tree并丢弃一些之前的commit
- 假设你正在编辑一些文件,并且已经提交,接着继续工作,但是现在你发现当前在working tree中的内容应该属于另一个branch,与这之前的commit没有什么关系。此时,你可以开启一个新的branch,并且保留着working tree中的内容。</p>
- <pre><code class="lang-bash">git tag start
- git checkout -b branch1
- edit
- <span class="hljs-comment"># 这次是把在branch1中的改变提交了。</span>
- git commit ...
- edit
- <span class="hljs-comment"># 此时发现,之前的提交不属于这个branch,此时你新建了branch2,并切换到了branch2上。</span>
- git checkout -b branch2
- <span class="hljs-comment"># 此时你可以用reset --keep把在start之后的commit清除掉,但是保持working tree不变。</span>
- git reset --keep start
- </code></pre>
- </div>
-
- <hr>
- <div class="btn-group btn-group-justified">
-
- <a class="btn" href="push.html"><b>上一页:</b> git~push</a>
-
-
- <a class="btn" href="../node/0.html"><b>下一页:</b> node</a>
-
- </div>
-
- </div>
- <div class="search-results">
- <div class="has-results">
-
- <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
- <ul class="search-results-list"></ul>
-
- </div>
- <div class="no-results">
-
- <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
-
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- <footer class="gb-page-footer">
- <div class="container">
- <ul class="menu">
- <li class="pull-right">
- <span class="service-info">Copyright GitBook 2018</span>
- </li>
- <li>
- <a href="#">Return to the top</a>
- </li>
- <li>
- <a href="#">Updated May 31st 18</a>
- </li>
- </ul>
- </div>
- </footer>
- </div>
- <script src="../gitbook/gitbook.js"></script>
-
- <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
-
-
- <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
-
-
- <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
-
-
- <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
-
- <script>
- (function() {
- gitbook.page.hasChanged({"page":{"title":"git~F.A.Q","level":"7.3","depth":1,"next":{"title":"node","level":"8.1","depth":1,"path":"node/0.md","ref":"./node/0.md","articles":[{"title":"nodejs+phantomjs+nodemailer","level":"8.1.1","depth":2,"path":"node/01_nodejs_phantomjs_nodemailer.md","ref":"./node/01_nodejs_phantomjs_nodemailer.md","articles":[]}]},"previous":{"title":"git~push","level":"7.2","depth":1,"path":"git/push.md","ref":"./git/push.md","articles":[]},"dir":"ltr"},"config":{"plugins":["theme-official@2.1.1","-sharing","-fontsettings","sitemap","-search","search-pro"],"root":"./docs/","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"sitemap":{"hostname":"https://blog.honghaitao.net/"},"search-pro":{"cutWordLib":"nodejieba","defineWord":["小需求","基础建设"]},"highlight":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"theme-official":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{"version":"0.0.1"},"title":"代码收集","gitbook":"3.1.1"},"file":{"path":"git/faq.md","mtime":"2018-05-31T04:27:37.042Z","type":"markdown"},"gitbook":{"version":"3.1.1","time":"2018-05-31T08:39:58.544Z"},"basePath":"..","book":{"language":""}});
- })();
- </script>
-
- </body>
- </html>
|