Untracked files between branches in Git

谁说胖子不能爱 提交于 2019-12-02 15:49:05
Randal Schwartz

"I'm not ready to commit the changes locally yet."

Commits in git are local things that can be undone, redone and re-re-done at will. It's only when you push the commit somewhere that you need to pay attention.

Plus, commits are visible to local tools like gitk, and can have diffs taken of them and can be rebased onto other commits, etc. It's a very powerful tool. Learn to use it.

I frequently do:

git add .; git commit -a -m 'WIP'

just to stash away everything in the current work tree if I think I might be interrupted. If I make a few more changes, I use:

git add .; git commit --amend

to update my "WIP" commit in-place. When I'm finally ready for the real commit, I just:

git reset --soft HEAD~; git reset

and now I can carefully control what the final commit will be.

They're not showing up in the master branch - if you did a hard reset and a clean, they'd disappear. Git is merely preserving your local modifications when you switch branches.

This is commonly useful; you might have realized you want to commit those modifications to a different branch than the one you're currently on. If the modifications conflicted with the difference between the two branches, git would refuse to switch branches.

You're right about the best approach, though - switching branches cleanly is in my experience one of the most common uses of git stash.

It sounds like you created the branch with git branch profiles, but didn't switch to it, so you stayed in master, and it got the file changes when you did a commit.

After creating the branch, you need to explicitly switch to it with git checkout (just create new branch on-the-fly and switch to it in one step using git checkout -b ).

If you have changes you don't want to lose (or commit to the current branch), but instead put into the other branch, do:

git add -A
git stash
git checkout <other branch>
git stash pop

More info on git stash is available from git-scm.com

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!