What are the differences between 'revert', 'amend,' 'rollback', and 'undo' a commit?

萝らか妹 提交于 2019-11-27 02:41:51

问题


Although I use Git pretty often, I'm still a beginner.

Sometimes, I make a mistake but spot it only after I have committed it. At that point, I usually have to spend a long time on the Internet looking for the command I should use to get rid of it (before pushing).

Every time that happens, I find myself wondering what's the difference between the four terms that I usually come across:

  • revert,
  • amend,
  • rollback,
  • undo.

I've that it's finally time to learn those differences once and for all. What are they?


回答1:


The terms revert and amend have a well defined meaning in Git. In contrast, rollback and undo do not have such a well defined meaning, and are open to interpretation.

Reverting a commit...

...means creating (on the current branch) a new commit that applies the inverse changes that another commit introduced. It's the preferred approach for correcting a problem in a repo that has already been shared with others, because it doesn't involve any destruction (i.e. rewriting history).

To revert a commit identified by <commit>, simply run

    git revert <commit>

Amending a commit...

...means replacing the "current" commit by a new one that has the same parent(s); more details in How does git commit --amend work, exactly?

Be aware that

  • amending a commit that you've already pushed to a shared remote is bad practice, because it's a form of history rewriting (it "deletes" the most recent commit, which your collaborators may have already based their work on);
  • you can only amend the last commit on a given branch; to rewrite older commits, you need to bring out the big guns (e.g. interactive rebase).

    To amend a commit, make all the required changes and stage them, then run

    git commit --amend
    

    No need to specify any commit, here, because the last commit on the current branch is the one that will be amended. Your editor will then pop up, giving you the opportunity to modify the commit message.

Rolling back...

...usually means discarding (or stashing) any local changes and resetting the branch to a commit (or simply checking out a commit, but that puts you in detached-HEAD state) prior to commit one where things started to get messed up. Use

    git reset <commit-before-things-started-to-go-belly-up>

Undo a commit...

...can mean, depending on the context, either revert or amend a commit.




回答2:


Undoing Changes: git checkout, git revert, git reset, git clean

  • git revert: This command reverts the changes of a commit. it maintains track record in logs.
  • git reset: This command reverts the changes of a commit. it does not maintain track record in logs i.e. reset to the reverted commit. Note: It an destructive function. have to be careful using this command.

    • git reset --soft HEAD^ Undo last commit, put changes into staging
    • git reset --hard HEAD^ Undo last commit and all changes
    • git reset --hard HEAD^^ Undo last 2 commits and all changes
  • git commit --amend where amend means add to the last commit. Sometimes we forgot to add files to commit. for example abc.txt file was forgot, we can add as follows: git add abc.txt and git commit --amend -m "New commit message"




回答3:


git --amend You should use the git --amend command only for commits which have not been pushed to a public branch of another Git repository. The git --amend command creates a new commit ID and people may have based their work already on the existing commit. In this case they would need to migrate their work based on the new commit

git revert You can revert commits via the git revert command. This command reverts the changes of a commit. Such commits are useful to document that a change was withdrawn.

Here is a [enter link description here][1]

[1]: How do you roll back (reset) a Git repository to a particular commit? on rollback techniques



来源:https://stackoverflow.com/questions/28166547/what-are-the-differences-between-revert-amend-rollback-and-undo-a-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!