Branches

There can be many branches of a repository, different branches might contain different  version of source file. "master" is the default branch of a repository.
There are two types of branches:
  1. Local repository branches
  2. Remote repository branches

Create a new branch

syntax

$ git checkout -b [newBranchName]

$ git checkout -b v2
Switched to a new branch 'v2'

Changing branch

$ git checkout master
Switched to branch 'master'


Benefits of branches

Writing file to 1.txt from master branch

#this is a comment
#writing file to 1.txt from master
$ echo "This is 1.txt from master" >> 1.txt

#displaying file of 1.txt
$ head 1.txt
1.txt This is 1.txt from master

#adding and commiting
$ git add .
$ git commit -m "Files from master"

Writing file to 1.txt from v2 branch

#changing branch
$ git checkout v2
Switched to branch 'v2'

#writing and displaying
$ echo "This is 1.txt from version 2" >> 1.txt
$ head 1.txt
1.txt This is 1.txt from master
This is 1.txt from version 2

#adding and commiting
$ git add .
$ git commit -m "Files from v2"

Now when you switch to branch master you not see the changes made by v2. The changes made by version v2 can be only seen when you switch to branch v2.

$ git checkout master
Switched to branch 'master'
$ head 1.txt
1.txt This is 1.txt from master

$ git checkout v2
Switched to branch 'v2'
$ head 1.txt
1.txt This is 1.txt from master

You can try using your text editor to see the changes in file 1.txt .

Adding source to remote branch from local branch

$ git push [repositoryname] [localBranch]:[RemoteBranch]
example

$ git push ajeshRepository master:v2
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create pull request for v2:
remote: https://bitbucket.org/aj3sh/myrepository/pull-requests/new?source=v2 HYPERLINK "https://bitbucket.org/aj3sh/myrepository/pull-requests/new?source=v2&t=1"& HYPERLINK "https://bitbucket.org/aj3sh/myrepository/pull-requests/new?source=v2&t=1"t=1
remote:
To https://bitbucket.org/aj3sh/myrepository.git
 * [new branch]      master -> v2



Pushing source to remote branch

$ git push [repositoryName] [branchName]
example

$ git add .
$ git commit -m "Commit message"
$ git push ajeshRepository v2
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 300 bytes | 100.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create pull request for v2:
remote:   https://bitbucket.org/aj3sh/myrepository/pull-requests/new?source=v2 HYPERLINK "https://bitbucket.org/aj3sh/myrepository/pull-requests/new?source=v2&t=1"& HYPERLINK "https://bitbucket.org/aj3sh/myrepository/pull-requests/new?source=v2&t=1"t=1
remote:
To https://bitbucket.org/aj3sh/myrepository.git
   620a270..3203026  v2 -> v2


Force push to remote branch

$ git push --force [repositoryName] [branchName]
example

$ git add .
$ git commit -m "Commit message"
$ git push --force ajeshRepository v2
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 301 bytes | 100.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create pull request for v2:
remote:   https://bitbucket.org/aj3sh/myrepository/pull-requests/new?source=v2 HYPERLINK "https://bitbucket.org/aj3sh/myrepository/pull-requests/new?source=v2&t=1"& HYPERLINK "https://bitbucket.org/aj3sh/myrepository/pull-requests/new?source=v2&t=1"t=1
remote:
To https://bitbucket.org/aj3sh/myrepository.git
 + b56167e...620a270 v2 -> v2 (forced update)


Pull source from remote branch

$ git pull [repositoryName] [branch]
example

$ git pull ajeshRepository v2
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://bitbucket.org/aj3sh/myrepository
 * branch            v2         -> FETCH_HEAD
   3203026..c50e588  v2         -> ajeshRepository/v2
Updating 3203026..c50e588
Fast-forward
 2.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)


List all local branch

$ git branch -a
* master
  v2
  remotes/ajeshRepository/master
  remotes/ajeshRepository/v2


Deleting remote branch

$ git push ajeshRepository --delete v2
To https://bitbucket.org/aj3sh/myrepository.git
 - [deleted]         v2


Deleting local branch

$ git branch -D v2
Deleted branch v2 (was c50e588).



You must login to comment

No comment yet


  • Online: 8
    Registered users: 7
    Unique visitors: 173
    Total hits: 490