Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move to another branch and 'saving' current changes

Tags:

git

github

Here's the scenario : I'm working on branch A but then something urgent need to be fix, how can I create a new branch (says it's branch B) without bringing over current changes?

I don't want to checkout all the files in branch A because I want to be able continue if the work in branch B is done.

I don't want to push branch A because it's unfinished.

like image 386
Jamie Anderson Avatar asked Dec 10 '16 02:12

Jamie Anderson


People also ask

Do you lose changes if you switch branches?

If you have uncommitted changes when you switch branches, they will be lost.

How do I move current changes to a new branch?

Using the git checkout Command The git checkout -b <BranchName> command will create a new branch and switch to it. Moreover, this command will leave the current branch as it is and bring all uncommitted changes to the new branch.

Should you stash changes before switching branches?

There are times when it's not wise to use git stash to keep your uncommitted changes before switching branches. I found that when I'm unsure how long I'm going to be working on another branch, it's better to not use git stash . The reason is simple: you might forget that you'd done work there.


3 Answers

Option 1: You can stash your changes

If you have changes in Branch A: while in branch A, do

git stash save "hint_to_what_stash_contains" 

To retrieve the changes,

git stash apply

If you have multiple stashes, you can list them and choose which stash to apply.

git stash list
git stash apply stash@\{<STASH_NUMBER>\}

Option 2: Commit your changes.

First, commit all your changes in branchA but not push it to origin, then create your branch b to do your work. Once branchB is done, you can checkout branch A to resume the work.

To resume work in branchA

git checkout brancha
edit files
git add <file(s)>
git commit --amend # You can also change the commit message if you want.

Option 3: Which i don't recommend, is the patch thing.. I have seen some people use it. Do a git diff and save it in a file Eg "branch_a_changes.patch" and when you want to resume your work just do git apply branch_a_changes.patch

like image 120
Sam Daniel Avatar answered Oct 05 '22 01:10

Sam Daniel


From git worktree --help (https://git-scm.com/docs/git-worktree)

You are in the middle of a refactoring session and your boss comes in and demands that you fix something immediately. You might typically use git-stash(1) to store your changes away temporarily, however, your working tree is in such a state of disarray (with new, moved, and removed files, and other bits and pieces strewn around) that you don't want to risk disturbing any of it. Instead, you create a temporary linked working tree to make the emergency fix, remove it when done, and then resume your earlier refactoring session.

       $ git worktree add -b emergency-fix ../temp master
       $ pushd ../temp
       # ... hack hack hack ...
       $ git commit -a -m 'emergency fix for boss'
       $ popd
       $ rm -rf ../temp
       $ git worktree prune
like image 20
IrLED Avatar answered Oct 05 '22 02:10

IrLED


You can commit your changes in A. Then Checkout B. When B is done, back to A and continue.

Or, If you don't want to commit the changes in 'A', stash (save other place) the changes. And Checkout to B and when you back to A just take (stash pop) the changes from stash.

Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time

  • Options-1: Commit

    $ git checkout A                      # chekcout A
    $ git commit -am 'tmp commit'         # add & commit
    
    $ git checkout B                      # checkout B
    // fix your urgent changes here
    
    $ git checkout A
    // continue A
    
  • Options-2: Stash

    $ git checkout A    
    $ git add .
    $ git stash                           # save unfinished changes
    
    $ git checkout B                      # checkout B
    // fix your urgent changes here
    
    $ git checkout A                      # checkout A
    $ git stash apply                     # retrieve the changes from stash
    
    // you can clear the stash
    $ git stash drop                      # remove the changes from stash 
    
like image 42
Sajib Khan Avatar answered Oct 05 '22 00:10

Sajib Khan