Is it possible to keep a branch with pending changes in Git?

Asked

Viewed 418 times

3

Imagine I have a commit from a file in the master branch texte.txt. So I create a branch called teste2, I check it out and create a file called teste2.txt, but I don’t commit.

If I give a git checkout master and check the git status, it consists of the changes (in this case the creation of teste2.txt) as belonging to the master branch, even though I made these changes when I was on the branch teste2.

There’s a way to keep these changes in the branch teste2 without having to commit? Because I might not want to commit if I haven’t yet completed what I’m implementing.

1 answer

4


It is only possible to have pending changes to the branch that is currently being used.

When you have pending changes, and you want to change branch, it is customary to store changes in a stash.

For example, using the use case in the question, if we’re in the branch teste2, we can store the changes in a stash like this:

git stash                                //stash anonima    
git stash save "adicionado teste2.txt"   //stash com nome

Now we can switch to the branch master and work on other files.

git checkout master

When we get back to the branch teste2, suffice apply a stash again to restore the initial state:

git checkout teste2
git stash list                           //ver lista das stashes

git stash apply                          //aplica a ultima stash que foi gravada
git stash apply stash@{0}                //aplica a stash no index 0
git stash apply stash^{/adicionado}      //aplica uma stash especifica (pesquisa por regex)

Note: When to do apply, stash is still stored in the stack. If we want to apply and erase the stash, use git stash pop. We can also use git stash list to list all stashes currently in the stack.

Git stash man page.

  • Good answer! It was just what I was looking for. =)

Browser other questions tagged

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