Merge Past Commit Into Master Again
Reverting a merge commit
Movement back to an older commit in Git using revert
REVERTING A COMMIT
git revert <commit-sha>
The revert command in git takes in a commit id and compares the changes with the parent. The delta or the diff is calculated and the negation of it applied as a new commit. In case the commit-sha is not specified, information technology is defaulted to the commit-sha of the Caput commit.
$ git revert 9735432
This command creates a commit fbb1df5 on meridian of the Caput negating the changes fabricated by 9735432. The new HEAD will and so point to the fbb1df5. The resultant tree will behave as if the commit 9735432 does not exist.
THE MERGE COMMIT
Unlike other commits, the merge commit is a commit which has multiple (more often than not ii) parents.
For instance, when a co-operative named feature is merged with master, a new commit is created on the branch master which has 2 parents, the previous head of main and the head of feature.
On merging the feature to chief.
$ git checkout principal
$ git merge characteristic
These commands create a new merge commit 1c32600.
The merge commit 1c32600 has two parent commits
- 9735432 from principal before the merge
- 1484b1a from co-operative characteristic
On running git show, the new commits display both the parents
$ git show
commit 1c32600da72208f8964da85fddb80e7dd43b15c9 (HEAD -> primary)
Merge: 9735432 1484b1a
REVERTING THE MERGE COMMIT
While reverting the merge commit, the parent with which it needs to be compared should exist specified. The parent may be specified with the -k flag in git revert followed past the parent-number.
The parent number is assigned from left. To revert the changes brought in by the feature branch, revert the commit with respect to the second parent (1484b1a).
$ git revert Caput -thousand 1
This volition revert all the changes made by the second branch (feature) on principal. The resulting tree will behave as the branch characteristic was never merged to principal.
Similarly, to revert the changes from the commits in the first parent of the merge commit run
$ git revert HEAD -m i
This will revert all the commits that were made on the main after the feature branch was created leaving simply the commits in the characteristic branch in the master.
In the above example,9745432 and b15b045 would be removed from the principal branch leaving only the commits 1484b1a and 79fe70a and the older commits which are in feature.
Also, the master branch may be merged to feature branch.
$ git checkout feature
$ git merge main
The changes in the commit in the 9745432 and b15b045 in the master co-operative will get included in the characteristic branch. This fourth dimension for the merge commit the start parent is the original co-operative which is the commit 1484b1a itself whereas the commit in the master branch 9745432 is the second parent.
Caution
If the merge of master to the feature branch was unintentional. The right manner to disengage it is to reset the branch. This tin can be done by running the following in the feature branch.
$ git reset Head~1
On the other hand, reverting a merge commit negates all the changes fabricated by the branch of the specified parent.
For case if later reverting the merge commit(177a8b) in the feature co-operative, it were merged back to master , information technology would wipe out the changes (9745432 and b15b045) fabricated in the master branch.
Now the chief branch pointing to the commit a75ebc volition non have the changes done in the commits 9745432 and b15b045.
Thank you for reading. Promise this was helpful. Delight go out your comments if you have any questions or you feel something more needs to be added.
Also do cheque out this web log 👆🏽 for a deeper explanation of how the git objects are structured under the hood.
Source: https://levelup.gitconnected.com/reverting-a-merge-commit-7de2e9114c7d
0 Response to "Merge Past Commit Into Master Again"
Post a Comment