How to Manage local branches (Git)

Reginald LynchAugust 8th 2020, 12:59

Let's start by creating a few local branches.

Step 1 : Use the following command to clone the jgit repository to match:

sudo apt install git
cd jgit 

Step 2 : Whenever you start working on a bug fix or a new feature in your project, you should create a branch. You can do so using the following code:

git branch newBugFix
git branch 

Step 3 : The newBugFix branch points to the current HEAD you were on at the time of the creation. You can see the HEAD with git log -1:

git log -1 newBugFix --format=format:%H 

Step 4 : If you want to add a description to the branch, you can do this with the --edit-description option for the git branch command:

git branch --edit-description newBugFix

Step 4 : Close the editor and the message will be saved.

Step 5 : To retrieve the description for the branch, you can use the --get flag for the git config command:

git config --get branch.newBugFix.description

Step 6 : The branch information is stored as a file in .git/refs/heads/newBugFix:

cat .git/refs/heads/newBugFix 

Step 7 : If you need a branch from a specific commit hash, you can create it with the git branch command as follows:

git branch anotherBugFix 3c807e0

Step 8 : As you can see, the abbreviated commit hash is shown when you use %h, and the full commit hash is shown when you use %H. You can see that the abbreviated commit hash is the same as the one used to create the branch. Most of the time, you want to create and start working on the branch immediately:

git checkout -b lastBugFix 3c807e0 

Step 9 : Git switches to the new branch immediately after it creates the branch. Verify with gitk to see whether the lastBugFix branch is checked out and another BugFix branch is at the same commit hash

Step 10 : Instead of using Gitk, you can also add -v to the git branch command or even another -v:

git branch -v

Step 11 : With -v, you can see the abbreviated commit hash for each branch, and with -vv, you can also see that the master branch tracks the origin/master branch:

git branch -vv