Reputation: 1013
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?
Upvotes: 2
Views: 3863
Reputation: 1013
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
Upvotes: 1