Switch branch without executing working code

I am using git for the source code repository and the current code in a messy state and want to switch to other branches for a little work. The problem is that I do not want to do a job fixation with half the work, as I can return to this issue later.

+6
source share
6 answers

You have two options to do this.

Option 1: - Create a patch for the working code and verify that all the working code then switches to another branch.

Option 2: - You can pack your branches. Click here for a more detailed example: http://www.gitguys.com/topics/temporarily-stashing-your-work/

+2
source

You can use git stash to “roll back” changes for a while, and then when you want to restore your previous changes, use git stash pop *

You can also (and safer) do WIP work (work in progress), switch to another branch, do your work, return to the original branch, complete the original work, and then squash , your commit is only one.
Remember that commit is not equal to pushing, so you can have a dirty history locally, but after reinstalling and crushing you (and your team) will see only a clean version.

* note, because if you delay more than once, git pop will only show the last cache. In this case, you need to use git pop stash@ {<revision>}

+5
source

If you have a new git (> = ver.2.5.0), you can use git worktree.

In the following command, you can check the temporary operating line for use.

git worktree add sourcetree_dir element_name

After finishing work on this branch, you can use the following command to delete it.

git prune worktree

+2
source

You can pin your branches. Here is an example: http://www.gitguys.com/topics/temporarily-stashing-your-work/

+1
source

Can't you create another directory and clone the desired branch into it?

If you have a runtime configured to use a specific directory, make it a link to the clone directory of the branch you are currently working with. That way, you could have more than one branch locally without significantly shuffling your local resources (just a simple script to reinstall the directory used by your runtime / server).

0
source

Using the command line, short answer:

 git stash git checkout otherBranch 

If shoulder is used, use:

 git checkout origin otherBranch .. do your changes/commits .. git stash apply 

With git stash apply you revert your previous changes. You could find merge conflicts ... fixed them and fixed them again.

0
source

All Articles