3

Every time I checkout to a different branch on my local Git repo, I need to do the following:

1. git stash
2. git checkout branch_name
3. git stash pop stash@{0}

So I can get my working and staging directories with the checked out branch.

Is there a better and shorter way to do so? Whether its a trick/workaround or a direct built in command?

My way to make it easier is through ZSH alias as following:

myfunction() {
    git stash
    git checkout $1
    git stash pop stash@{0}
}
alias gcost=myfunction
Wazery
  • 15,394
  • 19
  • 63
  • 95
  • 1
    small commits and short-lived branches – Davin Tryon Oct 01 '15 at 15:56
  • Yes, I try hard to do atomic commits, and make my branches' life span very short, but I really do need to switch branches very frequently as I use Gitflow and I need to move between feature/hotfix branches quite a lot. – Wazery Oct 01 '15 at 15:58
  • 1
    Why do you always have changes to stash when you switch branches? Normally you should be switching branches with a clean working directory because you have already committed all your changes. Seems like the problem is that you're editing code and then switching branches without committing the code you edited. – mkasberg Oct 01 '15 at 16:08
  • 4
    This is a really bad idea, because if there happens to be nothing to stash, `git stash` will be a no-op, and the subsequent `git stash pop` command will pop the wrong stash. User torek has a good post about this somewhere on the site... – jub0bs Oct 01 '15 at 16:14
  • 1
    @mkasberg Yes, mainly there are two reasons for this: 1. in my current project I have config files that I edited and can't commit, or ignore them so I keep them in my working dir, I think this can be solved by un-indexing them for my local repo, 2. I switch branches while still working on them, to fix some issues that arised in a branch that I used to open a PR on GitHub and pushes to it, or to add some more work on another feature branch. – Wazery Oct 01 '15 at 16:14
  • @Jubobs I hope you can add a link to this post. – Wazery Oct 01 '15 at 16:15
  • 1
    @Islam 1. `git update-index --assume-unchanged filename.txt` should make this easier to work with. 2. If you're working on a branch, and need to switch to another branch, you should stash your work-in-progress, but you should (in general) not be popping it from the stash after you switch branches. Leave it stashed, make your fix on the second branch, and pop it when you get back to the original branch. – mkasberg Oct 01 '15 at 16:23
  • 1
    @IslamWazery See http://stackoverflow.com/a/20480591/2541573 – jub0bs Oct 01 '15 at 16:34
  • @mkasberg 1. Thanks for the update index command. 2. Yes actually I apply the stash, I just felt that popping is a better thing to do and used it in this OP. – Wazery Oct 01 '15 at 18:32
  • @Jubobs Thanks for the link. – Wazery Oct 01 '15 at 18:32

2 Answers2

4

The shorter way to do this is just to checkout the other branch with no stashing. Changes stay in your working copy as long as there are no conflicts (in which case the checkout is refused, and you can use the stash/un-stash method). In other words: uncommitted changes follow you around from branch to branch automatically.

Wolf
  • 4,254
  • 1
  • 21
  • 30
-1

I suggest the below flow which I call Work-In-Progress (WIP).

On bug fix branch

git checkout bug_fix_branch
edit some code
git add -u
git commit -m 'WIP: Fix: Bug in network driver e1000'

On feature branch

git checkout feature_branch
git cherry-pick commit-id-of-the-above-commit
edit some code
git add -u
git commit --amend # update the last "WIP" commit with the changes

When the bug fix or the feature are ready to be pushed, just use git commit --amend to remove the "WIP" prefix and then push.

KostaZ
  • 710
  • 1
  • 7
  • 17