69

I really like git. At least, I like the idea of git. Being able to checkout my master project as a separate branch where I can change whatever I want without risk of screwing everything else up is awesome. But it's not working.

Basically, my workflow is like this:

  1. Checkout stable version to a new branch to experiment with new code
  2. Make a bunch of changes - I have no intent on keeping any of this, I'm just experimenting.
  3. Look at all the stuff I changed
  4. Add all the changes to be tracked
  5. Commit the branch and push branch to origin (if it worked, otherwise skip this step)
  6. Decide to try another method, go back to master
  7. Observe artifacts from the experimental branch I was working in, even though I'm in the master branch.

Every time I checkout a branch to another branch, make changes to the one branch, and then checkout the original branch, I still have all the files and changes that happened in the other branch. This is getting extremely frustrating. I've read that this can happen when you have files open in the IDE while doing this, but I've been pretty careful about that and both closed the files in the IDE, closed the IDE, and shut down my rails server before switching branches, and this still happens. Also, running 'git clean -f' either deletes everything that happened after some arbitrary commit (and randomly, at that), or, as in the latest case, didn't change anything back to its original state.

I thought I was using git correctly, but at this point, I'm at my wit's end here. I'm trying to work with a bunch of experimental code using a stable version of my project, but I keep having to manually track down and fix all the changes I made. Any ideas or suggestions?

git checkout -b photo_tagging
git branch # to make sure it's right
# make a bunch of changes, creations, etc
git status # see what's changed since before
git add . # approve of the changes, I guess, since if I do git commit after this, it says no changes
git commit -m 'these are changes I made'

git checkout master
git branch #=> *master

# look at files, tags_controller is still there, added in photo_tagging
# and code added in photo_tagging branch are still there in *master

This seems to happen whether I do a commit or not on the branch.

4
  • 1
    Can you give a more concrete example? Perhaps a series of commands that you have entered: git clone x; echo "foo" >> y; git clone x; grep y foo.
    – Nick Orton
    Commented Dec 23, 2010 at 3:35
  • Did you commit or stash your changes anywhere in there? Without more detail this is a rant, not a question.
    – Brad Mace
    Commented Dec 23, 2010 at 3:39
  • I just added the list of commands I've been using. Maybe I've been doing something wrong. Commented Dec 23, 2010 at 3:44
  • this happens to me too and the answers are so frustrating because, like, I am doing it right and sometimes it works, sometimes it doesn't. I just decided to abandon a pretty big chunk of code that was all made on a separate branch and - behold! - all that code is still sitting on my main branch as if I'd never checked out in the first place. Commented Jan 23, 2023 at 20:15

6 Answers 6

37

I had this issue when I tried to switch temp to another branch from the master branch and when I would change something on that temp page and without committing the changes I would checkout the master branch again, it actually merged all the changes from the TEMP branch into MASTER.

ANSWER:

Whenever you checkout to a TEMP branch, COMMIT your changes. That way if you commit them on the TEMP branch and checkout MASTER again, it will work as supposed.

7
  • 5
    Does anyone know why this is? It seems a shame to be forced to commit if your only half done just to switch branches...not that I'm afraid of commitment.
    – Chortle
    Commented Aug 17, 2016 at 22:39
  • 5
    @Chortle You can use git stash instead. No need to commit
    – shabany
    Commented Mar 17, 2017 at 8:53
  • 1
    @Chortle Just use git stash pop and it will bring your changes back, on that branch, however if you want those change to be present in another branch, you will have to commit the changes and merge the branches
    – shabany
    Commented Mar 18, 2017 at 21:44
  • 1
    This is definitely the answer! Committing definitely worked for a temp branch. Commented Jul 24, 2019 at 15:44
  • 1
    This commit solution works, and it trains my grammar skills as well 👍
    – Hang Chen
    Commented Apr 27, 2022 at 4:18
21

Here is an example of how to use git and branches.

$ git branch
* master
  organize

$ git branch networking
$ git checkout networking

$ git branch
  master
* networking
  organize

Now Master has been updated many times since anyone has done anything on networking

$ git pull origin networking
From github.com:dlundquist/Asteroids
 * branch            networking -> FETCH_HEAD
CONFLICT (rename/delete): Rename src/ServerClientThread.java->src/ServerConnectionThread.java in 92d5b7c4e7c4110803eabf5d5152e7f2a337d722 and deleted in HEAD
Auto-merging src/Actor.java
Auto-merging src/Asteroids.java
CONFLICT (content): Merge conflict in src/Asteroids.java
Auto-merging src/BasicWeapon.java
CONFLICT (content): Merge conflict in src/BasicWeapon.java
CONFLICT (delete/modify): src/DedicatedServer.java deleted in HEAD and modified in 92d5b7c4e7c4110803eabf5d5152e7f2a337d722. Version 92d5b7c4e7c4110803eabf5d5152e7f2a337d722 of src/DedicatedServer.java left in tree.
Auto-merging src/MainMenu.java
CONFLICT (content): Merge conflict in src/MainMenu.java
CONFLICT (delete/modify): src/NetworkClientThread.java deleted in HEAD and modified in 92d5b7c4e7c4110803eabf5d5152e7f2a337d722. Version 92d5b7c4e7c4110803eabf5d5152e7f2a337d722 of src/NetworkClientThread.java left in tree.
CONFLICT (delete/modify): src/NetworkUpdate.java deleted in HEAD and modified in 92d5b7c4e7c4110803eabf5d5152e7f2a337d722. Version 92d5b7c4e7c4110803eabf5d5152e7f2a337d722 of src/NetworkUpdate.java left in tree.
Auto-merging src/ScenePanel.java
CONFLICT (content): Merge conflict in src/ScenePanel.java
Auto-merging src/Shield.java
CONFLICT (content): Merge conflict in src/Shield.java
Auto-merging src/Sprite.java
Auto-merging src/TripleShotWeapon.java
Auto-merging src/Weapon.java
Automatic merge failed; fix conflicts and then commit the result.
mjolnir:Asteroids Durandal$ git status
# On branch networking
# Changes to be committed:
#
#   modified:   src/Actor.java
#   modified:   src/Sprite.java
#   modified:   src/TripleShotWeapon.java
#   modified:   src/Weapon.java
#
# Unmerged paths:
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#   both modified:      src/Asteroids.java
#   both modified:      src/BasicWeapon.java
#   deleted by us:      src/DedicatedServer.java
#   both modified:      src/MainMenu.java
#   deleted by us:      src/NetworkClientThread.java
#   deleted by us:      src/NetworkUpdate.java
#   both modified:      src/ScenePanel.java
#   added by them:      src/ServerConnectionThread.java
#   both modified:      src/Shield.java

Woah, git is really mad. It looks like Master is too far ahead of Networking for git to do all the work for me.

$ git reset --hard networking
HEAD is now at 20d6ee8 done tweaking game on the main branch.  Fixed a few bugs with the TripleShotWeapon.  Rebalanced the gameMechanics() in Asteroids to increase the difficulty as the levels increase.
mjolnir:Asteroids Durandal$ git status
# On branch networking
nothing to commit (working directory clean)

Ok, now I am "back in time" when networking last pushed to origin. But I really should go back and merge with Master now before I do any work. Other wise it will be even hard to merge.

Time spent merging files

$ git add (insert conflict resolved files here)
$ git commit -a -m "Merged networking with master"
$ git checkout master
$ git branch
* master
  networking
  organize

Now lets apply our changes.

$ git merge networking
Already up-to-date.

You might want to do this if you like the "Yeah!"

$ git merge origin networking
Already up-to-date. Yeeah!

Push our changes to the world

$ git push origin master
6
  • 8
    This is helpful, but it's not really the problem that's happening. I'm the only one working on the project, so there are no conflicts that I need to resolve. I think the problem is that artifacts are showing up from other branches when (I think) checking out another branch should replace or hide any changes made in another branch. Effectively, switching from networking to master (not merging) still shows everything that happened in networking. Commented Dec 23, 2010 at 4:01
  • 6
    Then I suspect git add . is your error. I always git add * to add everying in the directory and not -the- directory Commented Dec 23, 2010 at 4:04
  • 7
    Touche, Enabren. That did it. When I first started working with git, I was using 'git add *', but in trying to learn it I read somewhere to use 'git add .'. I just did a couple of experiments and found that 'git add .' is exactly the source of the problem. Thank you so much. Headaches => gone. Commented Dec 23, 2010 at 4:18
  • If this solved your problem or answered your question. @Mitch Wheat points out you should mark it solved and upvote it. This will get more people to help you in the future. Commented Dec 23, 2010 at 4:20
  • 4
    I use git add <file> in my branch and get the same problem as you have. I really don't like add *, since I rarely want to add all changed files to the commit. Are you saying I shouldn't add single files to stage with this work flow?
    – chwi
    Commented Jan 12, 2016 at 9:34
4

I like to explain git in common language. Answer: its because of the assumption that changing branches (checkout) will get you to another "workspace". Git doesn't do that (altough it feels more natural, and many have this misunderstanding).

Instead, branches are like tags, so to speak. So you can dance around branches, do some work, modify things, dance around again, and then commit. Then, the commit will be at the last branch you went to! The checkout only changes the "tag"/branch you'll use, period.

UPDATE after Martas's comment

This applies to when your branch and your master are synced (regarding tracked files), or with untracked files. If in fact they are in different states, when you checkout then files and folders will actually disapear and reappear. If you have changes tracked but not commited, then git will actually abort checking out, and you have to either commit or stash them.

2
  • You described the real behaviour, how I observe it, but I am still unsure and hasitating whether it is the correct behaviour or mistake. I really expect, that changing the branch will take all uncommitted changes in files in working tree, tidy them somewhere up, and put there uncommitted changes from the new branch I made previously. And the same when switching the branch back, without any commits.
    – Marťas
    Commented Jan 8, 2018 at 15:01
  • 1
    @Martas you're not wrong. My answer applies to when your branch and your master are synced (regardind tracked files), or with untracked files. If in fact they are in different states, when you checkout then files and folders will actually disapear and reappear. If you have changes tracked but not commited, then git will actually abort checking out, and you have to either commit or stash them. Commented Jan 8, 2018 at 20:01
2

Please check if you have write access or not. This error comes up with read only access to the repository.

1
  • Thanks! Strange bug, but worked fine after forking and cloning that repo
    – Just Mohit
    Commented May 28, 2022 at 9:34
0
$ git branch
* master
$ git checkout -b develop
$ git branch
  master
* branch

Now change whatever you want and do git add .

$ git add .   <--Here is the source of the problem, always git add . at your branch to see the observable difference between two branches.
$ git commit -m "Some messages"

$ git checkout master
$ git branch
*master
develop

Everything is neat and clean.

0

in short: commit your changes(make sure you are checked out on the correct branch the "experimental one"), even if you dont care to keep it, you can delete the branch later if thats what you want) then checkout the branch you want (before your changes), your IDE will now show/update to the status of the files in the branch you just checked out)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.