Thursday, 8 May 2014

GIT BRANCHING AND SIMPLE MERGING COMMANDS


Branching

$git branch: git branch list your available branches.

The branch that you are currently working on will have a star next to it.
$ git branch
*master
This means that we have a 'master' branch and we are currently on it.
$git branch (branchname): Create a new branch.
$ git branch testing
$ git branch
* master
  testing
$ git checkout (branch name) : To switch to a branch.
$ git checkout testing
switched to branch 'testing' .
Simple Example:
$ ls
README hello.rb
$ echo 'test content' > test.txt
$ echo 'more content' > more.txt
$ git add *.txt
$ git commit -m 'added two files'
[master 8bd6d8b] added two files
2 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 more.txt
create mode 100644 test.txt
$ ls
README hello.rb more.txt test.txt
$ git checkout testing
Switched to branch 'testing'
$ ls
README hello.rb
So now we can see that when we switch to the 'testing' branch, our new files were removed. We could switch back to the 'master' branch and see them re-appear.
$ ls
README hello.rb
$ git checkout master
Switched to branch 'master'
$ ls
README hello.rb more.txt test.txt

$git checkout -b (branchname): Create and immediately switch to a branch.

$git branch -d (branchname): Delete a branch
EX: $git branch -d testing
$git branch
master *
Note: Here testing branch has been removed.
$ git push origin <name_of_your_new_branch>:Push the branch on github.



Merging


Simple Example:
$ git branch
* master
$ ls
README hello.rb more.txt test.txt
$ git checkout -b removals
Switched to a new branch 'removals'
$ git rm more.txt
rm 'more.txt'
$ git rm test.txt
rm 'test.txt'
$ ls
README hello.rb
$ git commit -am 'removed useless files'
[removals 8f7c949] removed useless files
2 files changed, 0 insertions(+), 2 deletions(-)
delete mode 100644 more.txt
delete mode 100644 test.txt
$ git checkout master
Switched to branch 'master'
$ ls
README hello.rb more.txt test.txt
Once you have work isolated in a branch, you will eventually want to incorporate it into your main branch. You can merge any branch into your current branch with the git merge command. Let's take as a simple example the 'removals' branch from above. If we create a branch and remove files in it and commit our removals to that branch, it is isolated from our main ('master', in this case) branch. To include those deletions in your 'master' branch, you can just merge in the 'removals' branch.
$ git branch
* master
removals
$ ls
README hello.rb more.txt test.txt
$ git merge removals
Updating 8bd6d8b..8f7c949
Fast-forward
more.txt | 1 -
test.txt | 1 -
2 files changed, 0 insertions(+), 2 deletions(-)
delete mode 100644 more.txt
delete mode 100644 test.txt
$ ls
README hello.rb


***************ALL THE BEST*********************

No comments:

Post a Comment