简体   繁体   中英

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.

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

Don't concentrate on the file system. The file is changed of course, in the branch it is checked out currently. Just checkout the branch you want to work in before changing files and then work with the files. After commiting you can checkout to another branch.

You can create one simple local branch from master, try to read a little about gitflow and if it gets complicated for you, you might also use SourceTree that will manage your branch creation for you.

So, now lets say you changed many files on the local machine, you can commit them without need to modify your master unless you synchronize it (merge it). So, you can work on different copies of your master without modify master itself, unless you want to

I do not feel others have answered this question. I have now encountered the same issue. I have found two possible solutions.

OPTION A

A) Create a separate user account on your desktop so you can have multiple branches locally. This is not an optimal solution.

OPTION B (note that B1 and B2 are both part of option B and are different steps)

B1) Create your local repository as normal, rename the directory to indicate that it's a different branch. Then, go to Repository > remove. That way you can clone a second time and repeat. Now you'll have two local branches, neither of which are recognized by github desktop, even though they might be in the same directory as your other github projects. Now work on these branches with whatever external editing programs you want.

B2) When ready to upload, in github desktop choose "add an existing repository from your harddrive", navigate to the branch you want to upload. Add it. Github desktop will recognize the repository. Now you can upload to the branch of your selection using the dropdown at the top. Finally, go to repository > remove again, at the top. Then continue your editing as desired.

Option B is extensible to an arbitrary number of branches, and does not require you to reinstall all of your environments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM