4 Git Basics - Undo Operations

Undo action

At any time, you may need to undo something you just did. Next, we will introduce some basic undo related commands. Please note that some undo operations are irreversible, so please be careful, if you make a mistake, you may lose part of your work.

Amend the last commit

Sometimes, after submitting, we find that we have missed a few files and have not added them, or that the submission information is wrong. To undo the commit just now, use the option to resubmit: --amend 

$ git commit --amend

This command will commit with the current staging area snapshot. If you just submitted without any changes, running this command directly is equivalent to having the opportunity to re-edit the submission description, but the snapshot of the file to be submitted is the same as the previous one.

After starting the text editor, you will see the description of the last submission, edit it to confirm that there is no problem, save and exit, and the new submission description will be used to overwrite the wrong submission.

If you forgot to stage some changes when you just submitted, you can add the stage operation first, and then run the submission: --amend 

$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend

The three commands above end up producing just one commit, and the second commit command fixes the content of the first commit.

Cancel an already staged file

The next two subsections will demonstrate how to cancel files in the staging area, and how to cancel modified files in the working directory. Don't worry, when you check the status of the file, you will be prompted how to undo, so you don't need to memorize it by rote. Let's look at the example below, there are two modified files, we want to commit them separately, but accidentally add them all to the staging area. How can I undo one of the files that are staged? Actually, the output of the command already tells us what to do: git add . git status 

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

        modified:   README.txt
        modified:   benchmarks.rb

Just below "Changes to be committed", there are hints in parentheses, you can use the method to cancel the temporary storage. Well, let's try unstaging the benchmarks.rb file: git reset HEAD <file>... 

$ git reset HEAD benchmarks.rb
Unstaged changes after reset:
M       benchmarks.rb
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.txt

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:   benchmarks.rb

This command looks a bit weird, don't worry about it, just use it. The benchmarks.rb file is now back in its previously modified, unstaged state.

Cancel changes to the file

如果觉得刚才对 benchmarks.rb 的修改完全没有必要,该如何取消修改,回到之前的状态(也就是修改之前的版本)呢?git status 同样提示了具体的撤消方法,接着上面的例子,现在未暂存区域看起来像这样:

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:   benchmarks.rb

在第二个括号中,我们看到了抛弃文件修改的命令(至少在 Git 1.6.1 以及更高版本中会这样提示,如果你还在用老版本,我们强烈建议你升级,以获取最佳的用户体验),让我们试试看:

$ git checkout -- benchmarks.rb
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.txt

可以看到,该文件已经恢复到修改前的版本。你可能已经意识到了,这条命令有些危险,所有对文件的修改都没有了,因为我们刚刚把之前版本的文件复制过来重写了此文件。所以在用这条命令前,请务必确定真的不再需要保留刚才的修改。如果只是想回退版本,同时保留刚才的修改以便将来继续工作,可以用下章介绍的 stashing 和分支来处理,应该会更好些。

记住,任何已经提交到 Git 的都可以被恢复。即便在已经删除的分支中的提交,或者用 --amend 重新改写的提交,都可以被恢复(关于数据恢复的内容见第九章)。所以,你可能失去的数据,仅限于没有提交过的,对 Git 来说它们就像从未存在过一样。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325717968&siteId=291194637