2

When switching to an other branch, there can be conflicts when I have uncommitted changes. I would like to switch branches, but keep the modified files as they are, instead of committing the changes or using git stash (which might lead to conflicts).

This way is more intuitive with the workflow I use, as the changed files normally represent the most recent version of the files. Sometimes I switch to another branch to commit a minor change. I do not yet want to commit my changes on the current branch, as they are not complete. A minor change might be a single function that needs to be committed on an other branch with a partial commit, while other changes in the same file should stay as they are. Also, I do not want to use the stash, as applying the stash on the other branch might lead to conflicts. Further, once or twice, I forgot to apply a stashed change, and for fear of loosing data, I often do not drop the stashes. As I use many temporary branches, this leads to a cluttered history with references to branches I have already deleted.

I know that this is technically possible, as I could create temporary copies of all modified files, reset the working copy, switch to an other branch and move the temporary files to the working copy. So I could create a function to do this. But is there an already available or more simple solution?

phobic
  • 914
  • 10
  • 24
  • 1
    Making a temporary commit or using `git stash` are the two time-tested ways to switch to another branch when your working directory has unommitted changes. May I ask what is wrong with using one of these 2 methods (I don't know of any other methods). – Tim Biegeleisen Jan 27 '16 at 06:03
  • @Tim: As I mentioned, this is what I currently do. Actually, I tried to explain the drawbacks and why I would prefer an other solution in the post. Should I give a more concrete example? – phobic Jan 27 '16 at 06:21
  • Git is not a caching service, it is a repository service. `git stash` is really a hack which lets you backup your dirty in progress files, but I try to shy away from this. I would recommend making a temporary commit. In the worst case scenario, you can always delete that commit. If you decide to keep the changes, you can amend it. – Tim Biegeleisen Jan 27 '16 at 06:24
  • 1
    @phobic, I encourage you to write that little script that does exactly what you have in mind. Don't be afraid of trying this even though it is not a standard practice. This is how new things emerge. The only way to know if it's good or not is to just try it. – Adi Levin Jan 27 '16 at 06:27
  • @Adi: I wrote a simple script. I will publish the script, but only after a week of testing as I don't want anyone to experience data loss :) – phobic Jan 27 '16 at 09:05

1 Answers1

1

I wrote two scripts to accomplish my goals. So far they work fine. With git-switch you can switch to a branch and it will keep the contents of all modified files as they are now. It is based on gitstash, which can stash complete files if they are modified or restore them by overwriting the current files.

git switch mybranch2

Is the same as:

gitstash stash myStash
git switch mybranch2
gitstash pop myStash

gitstash is also useful when doing an interactive rebase. You can use it to save your changes, then do a rebase, and restore the files as they were before. You can then simply keep your file as is without resolving conflicts. If you made changes during the rebase that are not reflected in the currently stashed version, you can use a tool like smartgit or eclipse, to compare the file to the HEAD revision and include changes. This is in my experience more comfortable, as you do not have to remove any conflict markers. Further, you can apply a stash with gitstash apply myStash or list available stashes with gitstash list.

Advantages:

  • Always keep the current version of your files around when switching branches.

  • No need to git stash your changes manually.

  • No need to resolve conflicts.

  • Avoid conflict markers after a rebase.

git-switch: https://gist.github.com/anonymous/7f9e457335ff1d9f4c24

gitstash: https://gist.github.com/joe42/984fb415942b67ddf70f

phobic
  • 914
  • 10
  • 24
  • Updated gitstash to backup untracked files (prevents [...] would be overwritten by checkout error), and prevent reset --hard from deleting the stash. – phobic Feb 16 '16 at 12:17
  • What is the difference between your gittstash and the standard git stash? – setholopolus Jul 27 '17 at 21:47
  • @setholopolus: With gitstash you won't ever get conflict markers when you apply a stash (looks like this: https://stackoverflow.com/questions/10657315/git-merge-left-head-marks-in-my-files). The files are simply restored to their original content before you have stashed them. Also, untracked files are stashed, too. – phobic Jul 29 '17 at 08:55