How do I resolve git saying "Commit your changes or stash them before you can merge"?

  • 10,000
  • Tác giả: admin
  • Ngày đăng:
  • Lượt xem: 10
  • Tình trạng: Còn hàng

Before using reset think about using revert so sánh you can always go back.

https://www.pixelstech.net/article/1549115148-git-reset-vs-git-revert

On request

Source: https://www.pixelstech.net/article/1549115148-git-reset-vs-git-revert

git reset vs git revert   sonic0002        2019-02-02 08:26:39 

When maintaining code using version control systems such as git, it is unavoidable that we need đồ sộ rollback some wrong commits either due đồ sộ bugs or temp code revert. In this case, rookie developers would be very nervous because they may get lost on what they should bởi đồ sộ rollback their changes without affecting others, but đồ sộ veteran developers, this is their routine work and they can show you different ways of doing that. In this post, we will introduce two major ones used frequently by developers.

  • git reset
  • git revert

What are their differences and corresponding use cases? We will discuss them in detail below. git reset Assuming we have below few commits.

Commit A and B are working commits, but commit C and D are bad commits. Now we want đồ sộ rollback đồ sộ commit B and drop commit C and D. Currently HEAD is pointing đồ sộ commit D 5lk4er, we just need đồ sộ point HEAD đồ sộ commit B a0fvf8 to achieve what we want.  It's easy đồ sộ use git reset command.

git reset --hard a0fvf8

After executing above command, the HEAD will point đồ sộ commit B.

But now the remote origin still has HEAD point đồ sộ commit D, if we directly use git push đồ sộ push the changes, it will not update the remote repo, we need đồ sộ add a -f option đồ sộ force pushing the changes.

git push -f

The drawback of this method is that all the commits after HEAD will be gone once the reset is done. In case one day we found that some of the commits ate good ones and want đồ sộ keep them, it is too late. Because of this, many companies forbid đồ sộ use this method đồ sộ rollback changes.

git revert The use of git revert is đồ sộ create a new commit which reverts a previous commit. The HEAD will point đồ sộ the new reverting commit.  For the example of git reset above, what we need đồ sộ bởi is just reverting commit D and then reverting commit C. 

git revert 5lk4er
git revert 76sdeb

Now it creates two new commit D' and C', 

In above example, we have only two commits đồ sộ revert, so sánh we can revert one by one. But what if there are lots of commits đồ sộ revert? We can revert a range indeed.

git revert OLDER_COMMIT^..NEWER_COMMIT

This method would not have the disadvantage of git reset, it would point HEAD đồ sộ newly created reverting commit and it is ok đồ sộ directly push the changes đồ sộ remote without using the -f option. Now let's take a look at a more difficult example. Assuming we have three commits but the bad commit is the second commit. 

It's not a good idea đồ sộ use git reset đồ sộ rollback the commit B since we need đồ sộ keep commit C as it is a good commit. Now we can revert commit C and B and then use cherry-pick đồ sộ commit C again. 

From above explanation, we can find out that the biggest difference between git reset and git revert is that git reset will reset the state of the branch đồ sộ a previous state by dropping all the changes post the desired commit while git revert will reset đồ sộ a previous state by creating new reverting commits and keep the original commits. It's recommended đồ sộ use git revert instead of git reset in enterprise environment.  Reference: https://kknews.cc/news/4najez2.html