Change between branchs in the GIT and changes keep in the current branch

5

Well, I had a question and when I asked the question here on the same topic that I'm opening I answered myself but I 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 in it without following me if I change the branch during development, well it's simple just give git commit so the changes will stay in the branch where they are at the time of git commit and therefore when changing branch they will not go to the new branch as it happens if you do not commit before changing your branch.

Well this was the solution I found I do not know if it is the best solution for this, but if you know other options please share.

Help your next , when giving git commit do not forget to put a comment git commit -m "[COMENTÁRIO]" this will facilitate a future interpretation for you and your colleagues.

    
asked by anonymous 29.09.2015 / 22:16

1 answer

4

Hello, Matthew. I often use another alternative

The form you've exposed really solves the case. Branching is allowed, but you end up with one more commit in the history. I'm personally purist about the code's history, and when I do these "dirty", "not ready" commits I usually use a rebase or cherry-pick later to clean the house.

There's a git-like feature in git that works just fine for this scenario: You committed a few minutes ago and continued editing. You want to switch from branch to branch and want your clean working files.

Stash compares your work files with the last commit you made on the current branch (the famous HEAD), takes the differences, saves them in a drawer, and returns the state to HEAD, causing your working tree to be clean again . At this point the alternation of branches may follow. Later, you can take what git stash saved and reapply on the files. This does not commit, and you can go back to work precisely from the point where you stopped. It's like taking something out of the passenger seat to get someone on a ride and then returning the thing to the original seat when the person leaves: D.

Here are some examples:

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

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

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

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

git stash apply stash@{1} apply stash 1, returning the changes it saved to the files. It can be run at any time in the future, not necessarily as soon as you return to the branch.

It's a very simple feature that I miss too much in SVN. Allows you to toggle without bothering your head by saving patches around.

I hope I have helped.

    
30.09.2015 / 02:54