官术网_书友最值得收藏!

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:

  1. 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
    
  2. The 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
    
  3. If you want to add a description to the branch, you can do it with the --edit-description option for the git branch command:
    $ git branch --edit-description newBugFix
    
  4. 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. 
    
  5. 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:

  1. 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
    
  2. 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 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'
    
  3. 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:
    $ gitk
    
  4. Instead of using Gitk, you can also add -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
    
  5. 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
     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
    
主站蜘蛛池模板: 专栏| 凭祥市| 南宫市| 荥经县| 喀喇沁旗| 上饶县| 平塘县| 故城县| 萝北县| 婺源县| 招远市| 怀宁县| 大关县| 曲麻莱县| 梅河口市| 丽水市| 彩票| 苏尼特左旗| 天峻县| 景洪市| 无棣县| 密山市| 黎城县| 柘荣县| 儋州市| 孙吴县| 时尚| 清镇市| 高州市| 贡山| 古丈县| 通山县| 嘉义县| 泗洪县| 开封市| 马尔康县| 肇源县| 应城市| 喀什市| 德清县| 乳山市|