Git : Operations - Branch Focus
You will find in this article, some operations to know when you work with the tool Git.
More specifically, regarding the handling of commits/branches
Modifying the name of a remote branch
In order to change the name of a branch (error during initial naming, or change of naming standard after creation of the remote branch), it is possible to perform the following operations.
In the example, we take the following branch names :
- br_src for the source branch name
- br_cbl for the target branch name
The steps for doing the change are the following :
- Get the source branch
- Rename the source branch with the name of the target branch locally
- Delete the source branch from the remote repository
- Push the target branch to the remote repository
1# Step n°1
2git checkout br_src
3# Step n°2
4git branch -m br_src br_cbl
5# Step n°3
6git push origin :br_src
7# Step n°4
8git push --set-upstream origin br_cbl
Merge commits of a branch
In order to make the commits of a branch more readable, it can be interesting to merge several commits into one identifiable commit.
Note: In the example, we will work on a history of 4 commits in order to merge the 2 commits that correspond to the same operation.
- Before starting the merge operation, we have to get the ID of the commit we want to work from by using the command
git log
(It will not be affected by the merge operation)
Note : We get the ID 8afa079e45c3b5c7493f160db5ebefde06aef2bb
which corresponds to the last commit we do not want to modify.
- To start the merge operation, execute the command
git rebase -i 8afa079e45c3b5c7493f160db5ebefde06aef2bb
Note : It is also possible to use the command with the term HEAD to take into account only the X last commits of a branch : git rebase -i HEAD~X
- The term pick must be modified to squash (or s) to mark the commits to be merged. (To validate, save the changes and exit)
Note :
- The commit marked as squash will be merged with the first commit above it that is marked as pick
- It is possible to to multiple squash operations on multple commits in a single merge operation. To do this, you must have several commits marked as pick followed by at least one commit marked as squash
- After the validation of the previous step, Git display the description of the commits to be merged.
- In order to define the desired description for merging commits, you need to modify the descriptions. (To validate, save the changes and exit)
Note : In the example, we delete the description of the second commit in order to have a commit with only one description at the end of the merge.
- To check the merge operation result, you must execute the command
git log
Appy changes based on a specific commit (Cherry-Pick)
In order to be able to carry over the contents of a specific commit to another branch, you need to use the cherry-pick operation.
Note : It could be very interesting to perform a merge of several commits before performing a cherry-pick operation to carry over all the changes from a single commit.
The steps are the following :
- Get the branch from which you wish to make the cherry-pick operation
- Merge the wished commit content from source branch in the current branch
- Check the merge operation result
1# Step n°1
2git checkout br_cbl
3# Step n°2
4git cherry-pick br_src
5# Step n°3
6git log
Note : It is possible to define which commit you want to merge with the command git cherry-pick <commit-id>
If there are conflicts during the cherry-pick operation, this must be handled as for a merge between two branches using the following procedure :
- Modify the conflicting files
- Take into account the changes made with the command
git add <files>
- Finalize the cherry-pick operation with the command
git cherry-pick --continue
Resources
This work is based on the following resources :