Git暂存与清理

Git允许对未提交的修改作暂存和清理操作。

git stash

先使用status查看变更内容:

$ git status
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   index.html

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   lib/simplegit.rb

git stash命令能暂存当前分支未提交的记录。

$ git stash
Saved working directory and index state \
  "WIP on master: 049d078 added the index file"
HEAD is now at 049d078 added the index file
(To restore them type "git stash apply")

在需要时可用git pop找回。

现在,工作目录回到未修改时的状态。

$ git status
# On branch master
nothing to commit, working directory clean

查看暂存内容:

$ git stash list
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

找回暂时记录:

git stash apply stash@{2}

git stash apply默认找回最近的暂存记录,也可通过名字指定找回内容。

$ git stash apply
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#      modified:   index.html
#      modified:   lib/simplegit.rb

--index选项找回内容,不会将记录从暂存列表中删除。

$ git stash apply --index
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   index.html

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   lib/simplegit.rb

git stash pop找回记录的同时删除记录。

$ git stash list
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
$ git stash drop stash@{0}
Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)

git stash drop

删除暂存记录。

git stash变种

--keep-index告诉git不存储已被git add操作过的文件。

$ git status -s
M  index.html
 M lib/simplegit.rb

$ git stash --keep-index
Saved working directory and index state WIP on master: 1b65b17 added the index file
HEAD is now at 1b65b17 added the index file

$ git status -s
M  index.html

默认git stash只存储修改和分段跟踪的文件。

--include-untracked或-u, 告诉git在创建存储文件中包含untracked文件。

$ git status -s
M  index.html
 M lib/simplegit.rb
?? new-file.txt

$ git stash -u
Saved working directory and index state WIP on master: 1b65b17 added the index file
HEAD is now at 1b65b17 added the index file

$ git status -s

--patch参数以交互方式询问希望暂存的变更内容:

$ git stash --patch
diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index 66d332e..8bb5674 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -16,6 +16,10 @@ class SimpleGit
         return `#{git_cmd} 2>&1`.chomp
       end
     end
+
+    def show(treeish = 'master')
+      command("git show #{treeish}")
+    end

 end
 test
Stash this hunk [y,n,q,a,d,/,e,?]? y

Saved working directory and index state WIP on master: 1b65b17 added the index file

暂存处创建分支

暂存一段时间后继续开发,重新找回内容时可能会出现冲突,此时,可使用git stash branch <branch>创建一个新分支,检出暂存内容执行提交:

$ git stash branch testchanges
M    index.html
M    lib/simplegit.rb
Switched to a new branch 'testchanges'
On branch testchanges
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   index.html

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   lib/simplegit.rb

Dropped refs/stash@{0} (29d385a81d163dfd45a452a2ce816487a6b8b014)

使用新分支恢复存储继续开发。

清理工作目录

git clean可去除冗余文件,或清理工作目录。

git clean -f -d可移除工作目录中未被git跟踪的文件及空目录。

-n选项提供“演习”操作,以确认要移除的内容。

$ git clean -d -n
Would remove test.o
Would remove tmp/

git clean默认移除未被忽略且未被跟踪的文件。

与.gitiignore或其他忽略文件中模式匹配的文件不会被移除。

git clean -x选项可清理忽略文件。如:删除构建工具生成的文件。

$ git status -s
 M lib/simplegit.rb
?? build.TMP
?? tmp/

$ git clean -n -d
Would remove build.TMP
Would remove tmp/

$ git clean -n -d -x
Would remove build.TMP
Would remove test.o
Would remove tmp/

git clean交互模式

$ git clean -x -i
Would remove the following items:
  build.TMP  test.o
*** Commands ***
    1: clean                2: filter by pattern    3: select by numbers    4: ask each             5: quit
    6: help
What now>

该模式可明确知道所要清理的文件。