0

I'm completely new to git, and just tried creating a branch using the command line:

git checkout -b newbranch

In the branch contains a duplicate of my master's files, including a HTML file , some related JavaScript and CSS files etc. My question is, how do I actually "check out/ load" the HTML file (or any other file inside the branch for that matter)? I want to edit the html file inside the branch so it doesn't affect the master branch, but at the same time I need to be able to physically access this file inside the browser.

From what I understand branches in git do not actually exist as child folders in the file system I can just navigate to. Sorry for the newbie question.

Thanks,

coco puffs
  • 1,040
  • 12
  • 8
  • By default, you only have a single working directory for your repository. So when you check out `newbranch` you are replacing the previous state of your working directory (from the `master` branch) with that other branch. If you want the files from `master` back, you have to switch again. – You can use multiple work trees though, see the second answer in the linked question. Essentially you do `git worktree add ../my-project-master master` to checkout the master branch into a different folder. – poke Jan 09 '17 at 07:40
  • In general though, you would want to avoid this since having just a single working directory is considered a feature (so your editors do not need to work with multiple paths etc.). So unless you have a good reason for a separate folder, there’s no need for this. The question here would be why you need the master branch checked out somewhere when you’re actually working on a different branch. – poke Jan 09 '17 at 07:41

1 Answers1

2

First of all it depends from where you are creating a new branch. If you are currently on master branch and then you execute git checkout -b newbranch, it will create a newbranch which is same as your master branch as you created your new branch out of master (like cloning).

Now, as you are using checkout in your command, which will switch you to your newbranch automatically after it creates one. Here, you can edit any files you want and it won't affect your master branch. These changes will limit to your newbranch only.

Later, you need too switch to master branch and merge your newbranch like

--- I assume that you are currently on newbranch

git add <MODIFIED_FILE_PATH / NEW_FILE_PATH> # Ref [1]
git commit -m 'Your commit message'          # Commit your stacked files
git checkout master                          # Moves you to master branch
git merge newbranch                          # Merges your newbranch file changes to master branch

Note[1] : You need to replace <MODIFIED_FILE_PATH / NEW_FILE_PATH> with your literal file paths which you've modified or created in your newbranch

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • So creating a new branch doesn't actually create copies of all of the files in the master branch on the file system right? Physically I'm still working with one set of files, except Git somehow magically retains a copy of the original untouched files in memory by way of "master" that I can go back to if I want to? – coco puffs Jan 11 '17 at 05:02
  • @cocopuffs Yes. – Mr. Alien Jan 11 '17 at 05:35