1

I was working on a new feature, for the new feature create a branch called example. Created a lot of new folders and files in this branch.
There was a task with high priority to perform, so I went back to the master branch using git checkout master.

Now I am in the master branch, made the changes whatever I want to and wanted to push this to my master.

I performed git add -A and git commit -m 'Priority 2016'

This got all the created folders and files in the testing branch.
What have I done wrong?
Any Help?

Shawn
  • 47,241
  • 3
  • 26
  • 60
Mohan Prasad
  • 682
  • 1
  • 9
  • 34
  • `git add -A` should only have added files or folders which are either new or represent things which you have changed. Which files/folders did you want to commit? – Tim Biegeleisen Nov 17 '16 at 09:42
  • when you git checkout all modif are move in the branch you choice unless you commit in example branch before git checkout. you can git stash too. – Stargateur Nov 17 '16 at 09:44
  • I just wanted to commit only the changes I made in master not the testing branch – Mohan Prasad Nov 17 '16 at 09:44
  • So you mean to say when I am in the branch every time I flipback to master i need to commit the changes and then flip back to master? – Mohan Prasad Nov 17 '16 at 09:47

1 Answers1

1

2016: git checkout master would not remove files which were added (but not committed) in another branch: see "Why git keeps showing my changes when I switch branches"

A git reset --hard and git clean -f -d might be needed (as shown here) at the time of your checkout master.

If you have not yet pushed, you can:

  • reset the HEAD and index: git reset @~
  • delete the extra files (or save them if you need them back in your branch)
  • git add -A, and commit again.

As the OP Mohan Prasad comments below:

The issue was whenever I was flipping from branch to master I did not commit my changes: I just did git checkout master which has caused this issue.


2022: using the more recent command git switch command, a git switch master (or git switch main, if the default branch has been renamed to main) would avoid the issue.

It would stop because the index or the working tree differs from the new HEAD.

Only git switch --discard-changes master would force the branch switch.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • No my question is , I dont want git to commit my branch changes as well. When I am in master and committing my master changes it is adding my branch changes as well.. – Mohan Prasad Nov 17 '16 at 09:49
  • @MohanPrasad Hence my answer: when you switched back to master to make your fixes, you should have done a reset --hard and clean to avoid keeping the files created in the other branch. – VonC Nov 17 '16 at 09:51
  • For example I have worked 50% on my branch by adding folders and files. Now i flip back to master and make some changes to my other code in master branch. I want to put the code live which i have made on master branch. I commit the changes on master. When I do this it is getting my 50% of branch code to the master commit as well. Thats the problem – Mohan Prasad Nov 17 '16 at 09:54
  • @MohanPrasad All you need to do *before* flipping back to master is to add and commit your files. Then you can flip back to master in confidence, and see only master files. – VonC Nov 17 '16 at 09:57
  • @MohanPrasad Again, my edited answer address your current situation, where you have a commit "too big" because it includes files that should not be there. – VonC Nov 17 '16 at 09:57
  • The issue was whenever I was flipping from branch to master I did not commit my changes I just did git checkout master which has caused this issue.. that's it nothing else – Mohan Prasad Nov 17 '16 at 16:11
  • @MohanPrasad Exactly. i have included your comment in the answer for more visibility. – VonC Nov 17 '16 at 16:15