In git, branches are a lightweight way to manage multiple lines of development. You might use a separate branch for work on an experimental feature, or to maintain parallel lines of code with just a few differences. In either case, it's often essential to copy changes ("merge") from one branch to another to keep them synchronized, and git handles this very well.
To create a new branch named mynewfeature and switch to it:
git branch mynewfeature
git checkout mynewfeature
Now, commits you make will appear on the mynewfeature branch, but will not affect the default branch (which is named master). Running gitk --all conveniently shows you commits and branches, and lets you see graphically when your branches diverged or were synchronized.
To resume working on the master branch:
git checkout master
Git will put away all the file versions associated with your branch and restore the file versions associated with the master.
Typically, after you branch, new changes will be committed both on the branch and on the master (main line). Merging changes on the master into your branch regularly will ensure that your branch contains the latest updates from the main line (but does not apply to the master any changes you've made on the branch). Assuming you have checked out a branch, you can merge changes from the master with:
git merge master
Git applies all the changes made on the master since your branch diverged from it (or since the last time you merged). It then (usually) creates a new commit representing the merged state. If there is a merge conflict, Git will not make the commit: you will be asked to fix up the merge and then git commit -a the result.
Once you are done with your new feature, it's time to merge it back into the master! Checkout the master and then merge your branch like so:
git merge mynewfeature
You can then delete the branch:
git branch -d mynewfeature
No comments:
Post a Comment