Modified files in a git branch are spilling over into another branch

一曲冷凌霜 提交于 2019-11-26 04:47:33

This is the default behaviour of git.

You can use -f flag to checkout to do "clean checkout" if you like.

Why is that the file is shown as modified in master branch even though it was modified in git-build branch?

The key to remember is that the file was not modified in the git-build branch. It was only modified in your working copy.

Only when you commit are the changes put back into whichever branch you have checked out

If you want to temporarily store your changes to one branch while you go off to do work on another, you can use the git stash command. It's one of the amazing little unsung perks of using git. Example workflow:

git stash #work saved
git checkout master
#edit files
git commit
git checkout git-build
git stash apply #restore earlier work

git stash stores a stack of changes, so you can safely store multiple checkpoints. You can also give them names/descriptions. Full usage info here.

The modified files are not put in the repository until you add and commit them. If you switch back to your topic branch and commit the file, then it won't appear on the master branch.

  • It is not like git branches are dependent on each other but also they do not have a complete code base separately for each branch either.
  • For each commit, Git stores an object that contains a pointer to the changes. So each branch points to its own latest commit and HEAD points to the branch currently you are in.
  • When you switch the branch, the HEAD pointer points to that particular commit of the branch. So if there are modified files, the default behavior is to copy over them.

You can do the following things to overcome this issue.

  1. Use -f option to ignore the changes.

If you want to save the changes:

  1. Commit the changes locally in the same branch and then switch the branch.
  2. Use git stash, switch the branch, do your work, switch back to the original branch and do git stash apply.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!