Github Reverted Merge and Now Want to Merge Again
Revert Merge Commit in Git
- Revert Git Commit That Is Not Pushed
- Revert Git Commit That Is Already Pushed
Sometimes, we demand to merge two or more than branches so push the commit to the desired co-operative. But then we realize that nosotros don't need that merge in that repository, and so the question here is how to undo or revert a merge commit that is already pushed. Nothing to worry nigh. Git has an excellent solution for this trouble which is discussed below.
In that location are two ways to bargain with this situation depending on the problem; ane is that it has not been pushed withal to the repository but has been committed. Another is that it is committed and pushed both onto the repository. both situations have been discussed below:
Revert Git Commit That Is Not Pushed
It is impossible to revert a commit in Git directly. Despite this, we need to bank check out the commit before reverting it. After reverting it, now the position of the working directory is the same as the position of the working directory before we pushed the commit.
We can now apply the git reset
command to motility the alphabetize pointer dorsum to its identify,i.e. the position before the commit. For instance, if we desire to keep the changes of the commits Commit1
and Commite3
but revert the changes of the commit Commit2
, execute this command.
git reset Caput~3
This control will return the index pointer to the position before the commit Commit2
. After reverting the commit, we need to add the changed files to the working directory over again. For instance, execute the following commands:
git checkout Commit1 git add Commit1 git checkout Commit3 git add Commit3 git commit
Later that, we can push the commits Commit1
and Commit3
to the remote repository.
git push
If we desire to merge 2 branches and and so revert the changes of a branch, we can apply the following command:
git merge --abort
This command is used to cancel the merge process. After that, we can revert the changes to the co-operative. The commands discussed in this department are useful if nosotros desire to disengage the effect of the terminal commit or several commits.
Notwithstanding, if we want to undo the effect of the last two or more commits, we need to reset the working directory to the country before the last commit. It is because it is impossible to revert one or more commits directly.
Revert Git Commit That Is Already Pushed
In the other case, If we have already pushed the merge commit to the remote co-operative and pushed it too, we take to make a new commit that reverts the changes. Nosotros will execute the following command:
git revert -m 1 <merge-commit-hash>
This will develop a new commit that will opposite the changes from the previous merge commit. However, this new commit will also incorporate all the changes from the original merge commit, so we volition demand to edit the commit message to indicate that this is a revert commit. Whereas the selection that we take mentioned, -m ane
will tell Git that we want to go along the merged co-operative.
The revert control
does not change the working tree. It just modifies the alphabetize. The revert command
besides has an choice to restore the original message of the commit. In the case given to a higher place, the -chiliad
option is used to restore the original bulletin of the commit. The revert command can also exist used to restore a specific file from the integrated commit.
Git revert is a useful tool to utilise on our local branches when we are going with a change and decide that we no longer want to go on that change. Information technology'due south not recommended to be used on public branches.
As we know, git revert is purely reversible, it'southward incommunicable to disengage its changes. If we execute git revert on a commit that is not the current commit on its branch, we must be very conscientious because git revert will develop new commits that will have new commit IDs.
If we subsequently execute git reset –hard to revert the changes that git revert makes, nosotros will too remove the changes fabricated by the developer who created the commit that git revert undo. Information technology is possible to use git revert on a public co-operative, but doing so is non a good idea as per my opinion. It is better to develop a new branch to make a change and then merge it into the primary
branch when we are done.
If we desire to use git revert on a public branch, exist sure that we are the simply i working on that branch, or we will be undoing the work of others.
Write for us
DelftStack manufactures are written by software geeks similar you. If you lot also would like to contribute to DelftStack by writing paid articles, y'all can check the write for united states of america page.
Related Article - Git Revert
Related Article - Git Merge

williamsontobt1980.blogspot.com
Source: https://www.delftstack.com/howto/git/revert-merge-commit-in-git/
0 Response to "Github Reverted Merge and Now Want to Merge Again"
Post a Comment