How to Work with Git Branches
This is where the branch function shines! Branch allows each developer to isolate his/her work from others by creating a new branch from the original code base.
What Is a Branch?
Branch is an independent line of development. It works as a pointer to your next commits. Whenever a new branch is created, Git creates a new pointer while keeping the original code base untouched.
When you make your first commit in a repository, Git will automatically create a master branch by default. Every next commit you make will go to the master branch until you decide to create and switch over to another branch.
Creating Branches
Let's start with creating a new branch :
git branch hello-world
.This only creates the new branch. To start working on it, you will need to switch to the branch with
git checkout
. Now, you are ready to use standard git add
and git commit
commands. You can see two different branches pointing to the same commit. ==How does Git know which branch is currently checked out?== This is where the HEAD pointer comes into play!
HEAD always points to the currently checked out branch or commit. In our case, it is the master. Let's
git checkout hello-world
and see what happens.As you can see, the HEAD is now pointing to the
hello-world
branch instead of master. The next step is to modify some files and create a new commit with git commit -m "commit message"
A new commit C5 was created in the branch
hello-world
. Pointers always move to the latest commit in that branch that we checked out. Changes in the hello-world
branch did not affect any other branch. Branching enables you to isolate your work from others.It is a common practice to create a new branch for **each task* (e.g. bug fixing, new features etc), which is a good practice because it allows others to easily identify what changes to expect, and also for backtracking purposes to understand why a particular code change is implemented.* You can read more at Git Beginner's Guide for Dummies .
Create your own Ruby on Rails project and try it out! Make Rspec tests on a different branch and commit the changes.
Detached HEAD
As we have said before, HEAD always points to the currently checked out branch or commit. Checkout to a commit and see what happens with
git checkout C0
.Now, the HEAD is pointing to C0. We are currently checkedout to a remote branch. Is it possible to create a new commit while checkout to one? Time to find it out!
git commit -m "commit message"
The HEAD is detached and moves together with each new commit created. The newly created commit
C6
is pointing to C0
, that is now acting like a branch, but it is not.Commits that are not reachable by any branch or tag will be garbage collected and removed from the repository after 30 days.
To avoid this, we simply need to create a new branch for the newly created commit and checkout to it.
git checkout -b hotfix C6
.Always use branches when you are solving new problems to avoid disturbing your co-worker's features!
This post is originally pubslished on Kolosek Blog .
Reference
이 문제에 관하여(How to Work with Git Branches), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Kolosek/items/b7be01d6ba483ec6054d텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)