Branching and Merging
Git enable us to maintain multiple versions
of project in same working directory through branching. Initially our
projects have master branch
$ git branch
* master
We can see that we only have one branch ‘master’ and the ’*’ indicates that we are currently on it
Creating new branches
To create a new branch, we can use ‘git branch (branchname)’ which will create a branch at the point we’re currently at.
$ git branch newbranch
To switch to that branch so that the work we do is saved to it
instead of the ‘master’ branch, we run the ‘git checkout’ command’
$ git checkout newbranch
Switched to branch "newbranch"
$ git branch
* newbranch
master
Now we can see that we have a new branch and that we’re on it. Now we
can edit files and commit without worrying about messing up our pristine
‘master’ branch that we know works perfectly. We don’t have to share
the changes we make in our ‘newbranch’ branch until we are certain they
are ready.
Merging and removing finished branches
When you are done with work on a branch, you can either remove it and
ignore the changes made on it if the work is not acceptable, or you can
merge it into one of your long running branches.
$git checkout master
$ git merge newbranch
Now we navigated to initial states of the project and merged changed corresponding to new branch to the initial version, master.
No comments:
Post a Comment