Suppose you are just having your local Git repository, and you have no intentions at the moment to share the code with others; however, you can easily share this knowledge while working with a repository with one or more remotes. Local branches with no remotes work exactly in this fashion. As you can see in the examples, we are cloning a repository, thus we have a remote.
Let's start by creating a few local branches.
Use the following command to clone the jgit
repository to match:
$ git clone https://git.eclipse.org/r/jgit/jgit $ cd jgit
Perform the following steps to manage your local branches:
$ git branch newBugFix $ git branch * master newBugFix
newBugFix
branch points to the current HEAD
I was on at the time of the creation. You can see the HEAD
with git log -1
:$ git log -1 newBugFix --format=format:%H 25fe20b2dbb20cac8aa43c5ad64494ef8ea64ffc
--edit-description
option for the git branch
command:$ git branch --edit-description newBugFix
Refactoring the Hydro controller The hydro controller code is currently horrible needs to be refactored.
Git stores the information in the local git config
file; this also means that you cannot push this information to a remote repository.
To retrieve the description for the branch, you can use the --get
flag for the git config
command:
$ git config --get branch.newBugFix.description Refactoring the Hydro controller The hydro controller code is currently horrible needs to be refactored.
This will be beneficial when we automate some tasks in Chapter 7, Enhancing Your Daily Work with Git Hooks, Aliases, and Scripts.
The branch information is stored as a file in .git/refs/heads/newBugFix
:
$ cat .git/refs/heads/newBugFix 25fe20b2dbb20cac8aa43c5ad64494ef8ea64ffc
Note that it is the same commit hash we retrieved with the git log
command
Maybe you want to create specific branches from specific commit hashes. The first thought might be to check out the commit, and then create a branch; however, it is much easier to use the git branch
command to create the branches without checking out the commits:
git branch
command as follows:$ git branch anotherBugFix 979e346 $ git log -1 anotherBugFix --format=format:%h 979e346 $ git log -1 anotherBugFix --format=format:%H 979e3467112618cc787e161097986212eaaa4533
h
, and the full commit hash is shown when you use H
. You can see 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 979e346 Switched to a new branch 'lastBugFix'
lastBugFix
branch is checked out and another BugFix
branch is at the same commit hash:$ gitk
-v
to the git branch
command or even another v
.$ git branch -v anotherBugFix 979e346 Interactive Rebase: Do actions if * lastBugFix 979e346 Interactive Rebase: Do actions if master 25fe20b Add missing package import for jg newBugFix 25fe20b Add missing package import for jg
-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 anotherBugFix 979e346 Interactive Rebase: Do actions if e * lastBugFix 979e346 Interactive Rebase: Do actions if e master 25fe20b [origin/master] Add missing package newBugFix 25fe20b Add missing package import for g