- Git Version Control Cookbook
- Aske Olsson Rasmus Voss
- 699字
- 2021-12-08 12:33:51
Managing your local branches
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.
Getting ready
Use the following command to clone the jgit
repository to match:
$ git clone https://git.eclipse.org/r/jgit/jgit $ cd jgit
How to do it…
Perform the following steps to manage your local branches:
- 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 * master newBugFix
- The
newBugFix
branch points to the currentHEAD
I was on at the time of the creation. You can see theHEAD
withgit log -1
:$ git log -1 newBugFix --format=format:%H 25fe20b2dbb20cac8aa43c5ad64494ef8ea64ffc
- If you want to add a description to the branch, you can do it with the
--edit-description
option for thegit branch
command:$ git branch --edit-description newBugFix
- The previous command will open an editor where you can type in a description:
Refactoring the Hydro controller The hydro controller code is currently horrible needs to be refactored.
- Close the editor and the message will be saved.
How it works…
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.
Tip
Remember to perform a checkout of newBugFix
before you start working on it. This must be done with the Git checkout of newBugFix
.
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
There's more...
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:
- If you need a branch from a specific commit hash, you can create it with the
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
- As you can see, the abbreviated commit hash is shown when you use
h
, and the full commit hash is shown when you useH
. 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'
- 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 anotherBugFix
branch is at the same commit hash:$ gitk
- Instead of using Gitk, you can also add
-v
to thegit branch
command or even anotherv
.$ 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
- With
-v
, you can see the abbreviated commit hash for each branch and with-vv
, you can also see that themaster
branch tracks theorigin/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
- Dependency Injection in .NET Core 2.0
- 零基礎(chǔ)學(xué)MQL:基于EA的自動(dòng)化交易編程
- Java Web開(kāi)發(fā)技術(shù)教程
- ADI DSP應(yīng)用技術(shù)集錦
- 零基礎(chǔ)學(xué)Python網(wǎng)絡(luò)爬蟲(chóng)案例實(shí)戰(zhàn)全流程詳解(高級(jí)進(jìn)階篇)
- Highcharts Cookbook
- Learning Concurrent Programming in Scala
- Unity 2D Game Development Cookbook
- 蘋(píng)果的產(chǎn)品設(shè)計(jì)之道:創(chuàng)建優(yōu)秀產(chǎn)品、服務(wù)和用戶體驗(yàn)的七個(gè)原則
- OpenGL Data Visualization Cookbook
- UI設(shè)計(jì)全書(shū)(全彩)
- Raspberry Pi Blueprints
- Python計(jì)算機(jī)視覺(jué)與深度學(xué)習(xí)實(shí)戰(zhàn)
- Python AI游戲編程入門(mén):基于Pygame和PyTorch
- ArcGIS Blueprints