Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git_8 撤销操作(git commit --amend、git reset HEAD <file>) #68

Open
Qingquan-Li opened this issue Oct 12, 2017 · 0 comments
Open
Labels

Comments

@Qingquan-Li
Copy link
Owner

Qingquan-Li commented Oct 12, 2017

Git 撤销操作

References:
https://git-scm.com/docs/git-reset
https://www.liaoxuefeng.com/wiki/896043488029600/897013573512192

在任何一个阶段,你都有可能想要撤消某些操作。 注意,有些撤消操作是不可逆的。 这是在使用 Git 的过程中,会因为操作失误而导致之前的工作丢失的少有的几个地方之一。

  • git commit --amend :重新提交。这个命令会将暂存区中的文件提交(如果自上次提交以来你还未做任何修改,那么快照会保持不变,而你所修改的只是提交信息)
  • git reset HEAD <file> :取消暂存。
    • 示例: git add test.md 暂存了 test.md 文件,运行 git reset HEAD test.md 后, test.md 被改回未暂存状态。

注:在 Git 中任何 已提交的 东西几乎总是可以恢复的。 甚至那些被删除的分支中的提交或使用 --amend 选项覆盖的提交也可以恢复(方法:通过特定的 Git 命令;通过 Git 分支控制)。 然而,任何你未提交的东西丢失后很可能再也找不到了。



Expand: Git HEAD, reset, revert ...

References:
https://learngitbranching.js.org/

1. Detaching HEAD

Different ways to move through the commit tree.

First we have to talk about "HEAD". HEAD is the symbolic name for the currently checked out commit -- it's essentially what commit you're working on top of.

HEAD always points to the most recent commit which is reflected in the working tree. Most git commands which make changes to the working tree will start by changing HEAD.

Normally HEAD points to a branch name (like bugFix). When you commit, the status of bugFix is altered and this change is visible through HEAD.

01-before-Detaching-HEAD

# Replace C4 with hash (use `$ git log` to view)
$ git checkout C4

02-after-Detaching-HEAD


2. Relative Refs

  • Moving upwards one commit at a time with ^
  • Moving upwards a number of times with ~<num>

Example:

check out the parent commit of bugFix. This will detach HEAD.
You can specify the hash if you want, but try using relative refs instead!

03-before-Relative-Refs

$ git checkout bugFix
$ git checkout HEAD^  # or: git checkout HEAD~1

04-after-Relative-Refs

Branch forcing

One of the most common ways I use relative refs is to move branches around. You can directly reassign a branch to a commit with the -f option.

Example:

05-before-Branch-forcing

Moving (by force) the main branch to three parents behind HEAD:

$ git branch -f main HEAD~3

There we go! Relative refs gave us a concise way to refer to C1 and branch forcing (-f) gave us a way to quickly move a branch to that location.

06-after-Branch-forcing


3. Reversing Changes in Git

There are two primary ways to undo changes in Git -- one is using $ git reset and the other is using $ git revert.

git reset

reverses changes by moving a branch reference backwards in time to an older commit. In this sense you can think of it as "rewriting history;" git reset will move a branch backwards as if the commit had never been made in the first place.

Example:

07-before-reset

🌟 $ git reset HEAD^

# Undo and go back at the commit before resetting:
$ git reset HEAD@{1}

08-after-reset

Git Revert

While resetting works great for local branches on your own machine, its method of "rewriting history" doesn't work for remote branches that others are using.

In order to reverse changes and share those reversed changes with others, we need to use git revert.

Example:

09-before-Git-Revert

$ git revert HEAD

10-after-Git-Revert

@Qingquan-Li Qingquan-Li changed the title Git_8 撤销操作 Git_8 撤销操作(git commit --amend、git reset HEAD <file>) Jun 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant