3.21.2014

Commit your local git branch changes first, then checkout another branch

One of the things people get confused about is the branching in Git, specifically, the local branching. If you make a local branch, make code changes and then commit those changes to the local branch, you are free to switch to another branch and Git will automagically manage your files for you.  From the git book:

Git resets your working directory to look like the snapshot of the commit that the branch you check out points to.

But it's very easy to make a mistake here.  If you don't commit the changes in your new branch and instead check out another branch, those changes will be there too.  I've seen this cause confusion with developers.  So if you make a local branch off your "develop" line:

git checkout -b NewBranch develop 

and make some changes to files, you have to commit those changes to "NewBranch" or you'll still see them  as modified files if you switch back to the "develop" or "master" branches. Now if you commit those changes to NewBranch line first, then checkout "develop," you won't see them as modified, and the files will instead be in their state from the "develop" branch.

Happy Local Branching!

No comments:

Post a Comment