Switch between branchs in GIT and changes remain in the current branch

Asked

Viewed 2,115 times

5

Well personal I was with a doubt and when asking the question here on this same topic I am opening I answered myself but decided to continue to help someone who might have the same doubt.

My question was how to make changes to a branch and make the modified files stay on it without "following" me if I changed branch during development, well it’s simple to give a git commit so the changes will be in the branch where they are at the time of git commit and therefore when changing branch they don’t go to the new branch as it happens if you don’t commit before changing your branch.

Well that was the solution I found I do not know if it is the best solution for this, but in case you know other options share doing favor.

Help the next, at the time of giving git commit do not forget to put a comment git commit -m "[COMENTÁRIO]" this facilitates a future interpretation for both you and your colleagues.

1 answer

4


Hello, Matthew. I usually use another alternative

The way you’ve exposed actually solves the case. Branch toggle is allowed, but you end up with an extra commit in the history. I am personally purist about the history of the code and when I do these "dirty" commits, I usually "not ready" to use a rebase or Cherry-pick afterwards to tidy up the house.

There’s a feature in git I like very much called "stash" that fits exactly this scenario: You committed a few minutes ago and continued editing. Ai wants to switch branch and, for this, wants his work files (Working Tree) cleaned.

The stash compares your work files to the last commit you did on the current branch (the famous HEAD), takes the differences, stores it in a drawer and goes back to HEAD, making your Working Tree clean again. At this point branch toggle can follow. Later, you can take what the git stash saved and reapply over the files. This does not generate a commit and you can get back to work precisely from the point that stopped. It’s like taking something out of the passenger seat to take someone on a ride and then going back to the original seat when the person leaves :D .

Here are some examples:

git stash list - lists stashs saved in a format similar to

stash@{0}: On master: mais conteudo para o arquivo
stash@{1}: On master: primeiro stash!

being the stash format@{n}: your branch : your message (n is a stash number, 0 pro being the latest.

git stash save "mensagem" saves the committed versioned files and resets the files to the last commit

git stash apply stash@{1} apply stash 1, returning the changes that this saved to the files. It can be executed at any time in the future, not necessarily once you return to the branch.

It is a very simple resource and it makes me miss too much in SVN. It allows you to change without heating your head saving patches around.

I hope I’ve helped.

  • Very good, also I am not in favor of giving commits so "unnecessary", I will definitely start working using git stash. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.