Search code examples
gitgithub

GitHub: Working on two branches locally


I am using the GitHub application for Windows, and it's working great. However, I'm confused how I can work on multiple branches at the same time on my local files.

Let's say that I cloned a repository to C:\github, if I create a new branch nothing changes in my file structure. Meaning that whenever I change a file, it should also change for the master branch, right?

How can I have two branches locally on my machine that I can work on seperately, without having any conflicts. So that when I change file X in branch A, X hasn't changed in the master branch?

  • note I'm quite new to GitHub and GH desktop.
  • note I'm talking about the file system, not about commits.

Solution

  • The behavior you're asking for is the default behavior for git. When you create a new branch, you're effectively saying that you want to fork the development history from the point where you create the branch. Once you checkout a branch, all work done on files will happen in that branch.

    To make this more concrete, imagine you have cloned a repository and just have one branch called master. To create and checkout a new branch, you would do:

    git checkout -b new_branch
    

    Now, you would modify file X as desired, add it, commit it, and if you want to share it with others, push it. Now, to see the version of X before the changes you made, simply return to the master branch:

    git checkout master