We realized that some previous changes merged to the remote master branch are breaking the application functionality, so we need to do something to fix it. Let's see two ways of do it.
git rebase: rewriting the history
This solution can be applied only when we are allowed to delete the remote master branch and create it again.
This is the initial git log (WTF is glog?):
$ glog
501da39 2023-08-26 19:28:44 -0600 rulo4 raul4 commit4
A commit4
f216419 2023-08-26 19:28:36 -0600 rulo4 raul4 commit3
A commit3
a27a902 2023-08-26 19:28:29 -0600 rulo4 raul4 commit2
A commit2
765df57 2023-08-26 19:28:17 -0600 rulo4 raul4 commit1
A commit1
commit3 contains the changes that broke the app, so we need to rebase on top of the commit2.
This means that for the commits after commit2, we are going to select which of them we keep and which ones not.
1. create a work branch:
$ git checkout -b branch-for-rebase
Switched to a new branch 'branch-for-rebase'
2. Tell git that want to rebase on top of commit2 (using the commit2 id):
git rebase -i a27a902
A file named .git/rebase-merge/git-rebase-todo
will be opened listing all commits after commit2, starting by the oldest. Each commit is shown with pick action by default, means that the commit will be included in the resulting history:
pick f216419 commit3
pick 501da39 commit4
3. We need to change from pick to drop all the bad commits to be removed from the history, in this case is the commit3. Then, the file should look as follows:
drop f216419 commit3
pick 501da39 commit4
Then, after saving changes and closing the file, a message will be shown:
Successfully rebased and updated refs/heads/branch-for-rebase.
4. Let's see again the git log and verify that the commit3 with the bad code is not in the history, is like if never existed:
$ glog
2c3d846 2023-08-26 19:48:52 -0600 rulo4 raul4 commit4
A commit4
a27a902 2023-08-26 19:28:29 -0600 rulo4 raul4 commit2
A commit2
765df57 2023-08-26 19:28:17 -0600 rulo4 raul4 commit1
A commit1
So, now the remote master branch can be deleted, and we can recreate it again from our work branch.
git revert: fixing errors keeping them in the history
We can use this method when is not possible for us to change the remote master branch, in other words we cannot change the history.
Let's start by seeing again the original history:
$ glog
501da39 2023-08-26 19:28:44 -0600 rulo4 raul4 commit4
A commit4
f216419 2023-08-26 19:28:36 -0600 rulo4 raul4 commit3
A commit3
a27a902 2023-08-26 19:28:29 -0600 rulo4 raul4 commit2
A commit2
765df57 2023-08-26 19:28:17 -0600 rulo4 raul4 commit1
A commit1
1. Create the branch to do the revert:
$ git checkout -b branch-for-revert
Switched to a new branch 'branch-for-revert'
2. Tell to git that want to revert the commit3, as this is the one with bad code:
$ git revert f216419
A file named .git/COMMIT_EDITMSG
is opened, indicating the changes to be reverted:
Revert "commit3"
This reverts commit f216419653ce9cd1a5562d3ba61b8eb0a1a956c7.
3. After closing the previous file, a summary of the changes will be shown:
[branch-for-revert 6d8db03] Revert "commit3"
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 commit3
4. Let's see again the git log and verify that the commit3 with the bad code is still there in the history, but another commit was added to revert it:
$ glog
6d8db03 2023-08-26 20:04:44 -0600 rulo4 raul4 Revert "commit3"
D commit3
501da39 2023-08-26 19:28:44 -0600 rulo4 aul4 commit4
A commit4
f216419 2023-08-26 19:28:36 -0600 rulo4 raul4 commit3
A commit3
a27a902 2023-08-26 19:28:29 -0600 rulo4 raul4 commit2
A commit2
765df57 2023-08-26 19:28:17 -0600 rulo4 raul4 commit1
A commit1
So, we can proceed to create a pull request from our work branch to the remote master branch.
Final words
This is clearly a too simple example where no conflicts are required to be solved, the intention is to understand the basic concepts.
Top comments (0)