2

I'm still trying to understand the way branches work. I believed that when I create a new branch based on the master, checkout the new branch, and make changes in it, that the master should not change. But when I checkout the master, all the changes I've made in the new branch also appear in the master in Git Changes. What am I doing wrong?

Schwern
  • 153,029
  • 25
  • 195
  • 336
Rico
  • 141
  • 1
  • 8
  • 3
    Did you stage and commit the changes in the branch before switching to master? – Schwern Apr 11 '22 at 19:15
  • 1
    Please specify the commands that you've run. – Ar Rakin Apr 11 '22 at 19:15
  • I did not stage and commit the changes. I guess that's the reason. I thought I could switch back and forth without commiting the stages. Ar - I'm a total newbie gui user. – Rico Apr 11 '22 at 19:17
  • You might want to confirm before making the commit that you are truly on the newly created branch. You can do this by running git status and checking its output. – Marci Apr 11 '22 at 19:19
  • 1
    Also related: https://stackoverflow.com/q/19793929/184546 – TTT Apr 11 '22 at 19:40

2 Answers2

3

Changes are not automatically added to a branch, you have to stage and commit your changes and explain what they're for. Uncommitted changes will just follow you around as you switch branches. If you have changes and switch to master, Git will retain your uncommitted changes.

"Changes" is just any unstaged and uncommitted changes you have made to your files. "Staged changes" are uncommitted changes which have been "staged": the staging area is where you build your next commit.

Once staged, you commit them to the current branch. Then when you switch to master Git will checkout clean copies of the master versions of the files.

See Make a Git commit in Visual Studio for more about Visual Studio and Git. Read Pro Git for general concepts about Git and version control.

Schwern
  • 153,029
  • 25
  • 195
  • 336
2

What am I doing wrong?

You're not committing your changes before switching to a different branch. Your changes aren't added to the current branch until you add and commit them, so they remain in your file system when you switch branches. Sometimes git will tell you that you can't switch to a different branch because doing so would clobber your uncommitted changes, but if there are no conflicting changes in the branch you want to switch to, you can usually do it with no warning.

I believed that when I create a new branch based on the master, checkout the new branch, and make changes in it, that the master should not change.

If I'm right that you haven't committed your changes, then neither master nor your new branch will have been changed by your changes. Committing your changes will add them to the branch you're working in. Then, if you switch back to master or some other branch, you'll see that the changes you made aren't there.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    Thank you so much! I think I understand it now. I followed what you said in the last sentence and committed my changes to the new branch and then switched back to the master and it appeared as before as you said - with no changes in code or staged. Thanks again. – Rico Apr 11 '22 at 19:41