Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub: Working on two branches locally

Tags:

git

github

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.
like image 399
Bram Vanroy Avatar asked Nov 09 '22 02:11

Bram Vanroy


1 Answers

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
like image 196
houtanb Avatar answered Nov 15 '22 04:11

houtanb