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

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
    
主站蜘蛛池模板: 扎囊县| 香河县| 红河县| 大姚县| 都匀市| 万载县| 深泽县| 北海市| 长白| 泉州市| 万宁市| 桃园县| 明星| 黎城县| 铁岭市| 通城县| 威海市| 韶关市| 永新县| 来宾市| 双牌县| 青阳县| 松滋市| 大丰市| 吴旗县| 华安县| 铁力市| 合江县| 高尔夫| 盖州市| 普宁市| 会宁县| 高州市| 两当县| 铜川市| 杭锦旗| 虞城县| 墨江| 嘉义县| 通海县| 车险|