Git Advanced command
Rebase, Interactive Rebase, squash, revert, reset, cherrypick, amend, reflog.
Interactive Rebase (git rebase -i <base_commit>)
Interactive rebase in Git is a powerful feature that allows you to modify and organize your commit history. It enables you to rewrite, reorder, combine, and delete commits before incorporating them into a branch. This is especially useful when you want to clean up your commit history, squash multiple small commits into one, or split a large commit into smaller ones
Warning Note:
Do not use Interactive Rebase on commits that you’ve already/shared on a remote repository.
Instead, use it for cleaning up your local commit history before merging it into a shared team branch.
To perform an interactive rebase, follow these steps:
- Run
git log
to see the commit history and identify the commit you want to start the rebase from (the parent of the first commit you want to modify). - Run the interactive rebase command, replacing
<base_commit>
with the commit hash identified in the previous step:git rebase -i <base_commit>
- The text editor will open, showing the list of commits and actions. Edit the file to specify the desired action for each commit, save the changes, and close the editor.
If want to change the most recent comment msg(A in SS) then just use
git commit amend
But if want to change commit (B, C, D, E in the SS) the use command
git rebase -i HEAD~3 (mention head like B == 1, C == 2, D == 3)
Allow to open the terminal and mention what kind of manipulation you want. you will get it all command in the documented.
Squash: Allow to add previous commits. like want to merge commit C and D then move to head of E.
git rebase -i HEAD~4
squash command allows 1 commit and 2 commit init one commit.
For example, to squash the commits with the hashes 1234567890
, abcdef1234
, and fedcba0987
, you would use the following command:
git squash 1234567890 abcdef1234 fedcba0987
Cherrypick
Moving a commit to a different branch. If mistakenly commit a change to a different branch then cherrypick will help it move into the correct branch.
like a commit, a change in the master branch mistakenly and want to move into the feature branch the steps;
- checkout to the feature branch and pick the commit hash then run command
git cherry-pick 26bf1b48 (This allows to move the commit in the feature branch)
Then delete it commit from the Master branch by using the command.
git reset — hard HEAD~1 (Delete first one commit from Master branch)
Reflog:
Recovered deleted commits if mistakenly deleted.
Rebase: rebase takes all the commits from feature branch move into top of the master commits. If I rebase feature branch to master branch.
Linkhttps://www.youtube.com/watch?v=qsTthZi23VE&t=1771s
Revert:
In Git, revert is a way to undo a commit. This can be useful if you have made a mistake in a commit or if you need to go back to a previous version of your code.
To revert a commit in Git, you can use the git revert
command. The syntax for the git revert
command is as follows:
git revert <commit-hash>
For example, to revert the commit with the hash 1234567890
, you would use the following command:
git revert 1234567890
The git revert
command will create a new commit that undoes the changes made in the specified commit. The commit message for the new commit will be the commit message of the specified commit, prefixed with the word "revert".
It is important to note that reverting a commit can potentially introduce conflicts. This is because the git revert
command will try to undo the changes made in the specified commit, even if there are conflicts. If there are conflicts, you will need to resolve them before you can continue with the revert.
To resolve a conflict, you will need to open the file that contains the conflict and manually resolve the conflict. Once you have resolved the conflict, you can continue with the revert by running the git revert --continue
command.
If you do not want to resolve the conflict, you can abort the revert by running the git revert --abort
command.
Revert is a powerful tool that can be used to undo mistakes or to go back to a previous version of your code. However, it is important to be aware of the potential risks of reverting, such as the introduction of conflicts.
Different between git merge and git rebase
git merge: It combines the changes from two different branches into one. When we merge one branch into another, a new commit is created, that holds the commit history of both branches. which indicates that the branches have been merged.The merge commit can add clutter to the commit history, especially in complex branching scenarios.
Let us take an example if we have a project with 3 commits on the master branch as commit 1,2,3 and feature branch commits as commit A and B. If we perform a git merge operation then commits A and B will be merged as commit 4 onto the master branch. This is depicted in Fig:9.
Advantages:
- The logs are very exhaustive and can help in understanding the complete history of how and when each merge happened
- It is easy to find mistakes and resolve them.
Disadvantages:
- Results in a clumsy log / history
- Not very user-friendly
git rebase: It moves the changes from one branch to another by applying each commit of the branch being rebased onto the tip of the target branch.
This results in a linear history without any merge commits. Essentially, looks as if the feature branch was developed directly on top of the target branch.
Let us take an example if we have a project with 3 commits on the master branch as commit 1,2,3 and feature branch commits as commit A and B. If we perform a git rebase operation then the commits A and B will be rebased on to the master branch as commit 4 and 5 and there will be no logs of the feature branches. This is depicted in Fig 12.
Advantages:
- The logs are linear
- It’s easy to move through the project.
Disadvantages:
- We cannot track, when and how the commits were merged on the target branch