Git Branch

What is branch?

Branch is just text reference to the commit.

-  Default branch is master.
-  Multiple branches can exists in the same repository.
- Pointers for all branches are located in .git/refs/heads
folder.
-  Current branch track new commits.
-  Branch pointer moves automatically after every new commit.
-  hange branch git checkout <branch>

Now we will create a branch and below is the command to create
it which has name staging:
[varelite1@managed1 repo]$ git branch staging
Now you can go ahead and switch to the branch and perform some 
tasks:
[varelite1@managed1 repo]$ git checkout staging
Switched to branch 'staging'

[varelite1@managed1 repo]$ git branch
  master
* staging

[varelite1@managed1 repo]$ touch test1 test2 test3

[varelite1@managed1 repo]$ git add .

[varelite1@managed1 repo]$ git commit -m "added new branch"
[staging 55946ab] added new branch
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test1
 create mode 100644 test2
 create mode 100644 test3

[varelite1@managed1 repo]$ git push origin staging
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 273 bytes | 54.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'staging' on GitHub by visiting:
remote:      https://github.com/somethingtostudy12/managed1/pull/new/staging
remote:
To github.com:somethingtostudy12/managed1.git
 * [new branch]      staging -> staging
[varelite1@managed1 repo]$
That has been pushed to remote. Now you can go ahead on managed 
to push the details. You need to remember that push will not
fetch the branch. You need to run fetch command.
 [varelite2@managed2 managed1]$ git branch
* master

[varelite2@managed2 managed1]$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

[varelite2@managed2 managed1]$ git fetch
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:somethingtostudy12/managed1
 * [new branch]      staging    -> origin/staging

[varelite2@managed2 managed1]$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/staging
[varelite2@managed2 managed1]$

[varelite2@managed2 managed1]$ git branch
* master
It will not show you the latest branch until you checkout it 
once.
[varelite2@managed2 managed1]$ git checkout staging
Branch staging set up to track remote branch staging from origin.
Switched to a new branch 'staging'
[varelite2@managed2 managed1]$ ls
code1  code2  index.html  index.php  managed1.txt  managed2.txt  test1  test2  test3  varelite.txt

[varelite2@managed2 managed1]$ git branch
  master
* staging

[varelite2@managed2 managed1]$ git checkout master
Switched to branch 'master'

[varelite2@managed2 managed1]$ git branch
* master
  staging
Now we want to merge staging code changes to master branch. We 
will go to github.com and will raise pull request to merge the
branches.
Click on Pull request and create a “New Pull Request”. Here 
base branch will be master where we are moving new changes from
staging.
Now you can login via your maintainer user id and look for pull 
request here we are using same user id. You can see the request.
Now merge into it.
Now you can do a merge commit and you will have all data in 
master branch. 

Leave a Reply

Your email address will not be published. Required fields are marked *