Friday 30 August 2013

Git branching and merging

1) git branch
This will list all the branches of your repository in your local.

2) git branch branchname
To create a new branch of your current working repository. Type following code:-

Eg:- git branch newbranch

This will create a new branch in your local..

3) git checkout
This command will switch you to the branch name you entered.

Eg:-git checkout newbranch

4) git checkout -b branchname
This will create a new branch and will switch you to the same.

5) git push origin branchname
Push the branch from local to the github repository.

6) git merge name-of-the-branch-to-be-merged
To merge two branches first checkout the branch to which you want to merge.

Suppose you want to merge the changes from branch A to branch B then do the following:
git checkout B
git merge A

7) git stash
Sometimes you want to switch to some other branch without committing and pushing the changes of the current branch. However you still want to save the changes you have done in that branch. This can be done with the help of stashing.

Suppose you are in branch A and you want to save its changes before switching to branch B.
git stash
This will save the status of the current working directory.

Now you can switch to other branch suppose B
git checkout B
Do the changes, commit it and push it.

Now switch back to branch A
git checkout A
git stash apply //to apply the status u saved before.
git stash apply --index //to reapply the staged changes





No comments:

Post a Comment