GitHub如何在保留当前未提交代码的同时检查上一次提交的文件?

时间:2018-07-05 07:10:58

标签: git

我知道这是一个简单的问题,但我不知道如何在git中完成。

例如,与上次提交相比,我更改了10〜15行。但是现在我想看看原始的10〜15行是什么样子。因此,我需要返回上一次提交,并且能够不提交而返回到当前代码。

4 个答案:

答案 0 :(得分:1)

使用git stash命令保存当前更改而无需提交更改,以后再返回。

从文档中:

  

隐藏会占用您的工作目录的脏状态,即,   您修改过的跟踪文件和分阶段进行的更改-将其保存到   一堆未完成的更改,您可以随时重新应用(即使在   一个不同的分支)

以下是在master分支上使用隐藏功能的示例用法:

1)说,上一次提交有2个文件。

# Commit message: Adding file1.txt and file2.txt
file1.txt
file2.txt

2)检查没有要提交的内容。

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

3)编辑file1.txt

$ vim file1.txt

4)现在,工作目录处于脏状态。

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
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:   file1.txt

5)藏匿变更

$ git stash
Saved working directory and index state WIP on master: e1b8933 Adding file1.txt and file2.txt
HEAD is now at e1b8933 Adding file1.txt and file2.txt

6)查看file1.txt提交时(即在本地修改之前)

$ cat file1.txt

7)查看储物清单

$ git stash list
stash@{0}: WIP on master: e1b8933 Adding file1.txt and file2.txt

8)从存储中恢复更改

$ git stash apply
On branch master
Your branch is up-to-date with 'origin/master'.
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:   file1.txt

no changes added to commit (use "git add" and/or "git commit -a")

答案 1 :(得分:0)

您可以使用git diff来显示差异。

您可以使用git show HEAD:foo.txt来显示文件。

答案 2 :(得分:0)

您可以使用以下方式隐藏提交: git stash

然后,您可以通过使用转到先前的版本 git checkout [revision]

要还原,请使用git checkout [branch_name]

可以说,那么您的分支是主分支,请尝试使用此分支找回 git checkout master

要取回您的修改,请使用弹出更改 git stash pop

答案 3 :(得分:0)

1.git隐藏

#-k to keep the index intact, -u to stash the untracked files
git stash push -k -u

#checkout the previous commit xxx
git checkout xxx

#go back to the original commit ooo
git checkout ooo

#apply the stash to restore the uncommitted changes
git stash apply --index

2.git工作树

#create a worktree and checkout the previous commit xxx's code under /path/foo
git worktree add /path/foo xxx

#the uncommitted changes are still in the current repo

#remove the worktree
git worktree remove /path/foo

3.git show

#show the content of the previous commit xxx's file bar.txt
git show xxx:bar.txt

#show Lines 10~15
git show xxx:bar.txt | sed -n 10,15p

4.git责备

#show Line 10-15 of the previous commit xxx's file bar.txt
git blame -s -L 10,15 xxx -- bar.txt