2

I changed a few files in my local branch. I only want to push one file to origin. So I added, committed, and merged and pushed only that file to the origin. Now, I want to checkout another branch to do work in there. But git says that since I have uncommitted modified files I can't checkout another branch. What should I do when I want to checkout another branch but don't want to add all my changed files to origin?

coderk
  • 71
  • 12

1 Answers1

1

You can either:

  • stash them (git stash)
    (you will apply that stash later when you come back to that branch),
  • or commit them in your original branch before checkout the other one.
    Committing them won't mean you are pushing them right away,
  • or create a temporary branch and commit them in that new branch (that you can merge later to your original branch),
  • or, with git 2.5+, checkout another branch with the same repo.
    See Multiple working directories with Git?

    git worktree add <path> [<anotherbranch>]
    

The last way allows you to have two different folders (for two different branches), and the same repo.
You switch between branch just by changing your current working directory (cd)


what if I don't want them to ever be added to origin and they're just for my personal use on my local branch?

Then it is an entirely different question:

You can either:

  • remove them from history (git rm --cached) and add them to your .gitignore.
    (but once pushed, those files will be gone for others as well)
  • or you can ignore the changes locally

    git update-index --skip-worktree -- path
    

Then any local modification would not show up in git status.
That is purely local to your repo, and is not pushed.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • when I commit them, does that mean when I do a push later, they'll be added? – coderk Oct 22 '15 at 05:39
  • what if I don't want them to ever be added to origin and they're just for my personal use on my local branch? is git stash the best option in this case? – coderk Oct 22 '15 at 05:41
  • @coderk I am not aware of "git cache": "git stash" is the closest of "caching" (putting away) those files. But in your case, `git update-index --skip-worktree -- path` is an appropriate solution. – VonC Oct 23 '15 at 04:05