Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

To help in my knowledge of git so I can use it day to day, what is the difference between:

  • revert
  • amend
  • rollback
  • undo

What are they and what do they do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?

enter image description here

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,

  • revert a commit,
  • amend a commit,
  • remove a commit via an interactive rebase.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...