Home > front end >  How to update our branch from main & stash files?
How to update our branch from main & stash files?

Time:08-09

  1. how to update our local branch from main
  2. How to stash the changes ?

I am getting these errors while trying to update my branch from main.

I don't want to merge into main, I want to update my local branch (keeping my changes and getting new changes from main).

Thanks

enter image description here

CodePudding user response:

You should first do commit before runing pull or rebase.

If your work is not ready to commit, you can still do a temporary commit and pull rebase so that your commit becomes the latest and reset your HEAD to the previous commit, removing the temporary commit and leaving your changes in the working copy.

Something like this depending on the situation:

git commit -a -m 'temporary commit'
git pull --rebase
git reset HEAD~1

CodePudding user response:

You could stash the modified files first before making pull or rebase.

By running git stash, Git will save the working directory and index state for latterly applying back to your working area. After running git stash, all the modified files would be saved and back to the state before the modification has made, and you could check the stashed items by running git stash list and git stash show, as the following example,

$ git status                                                                                                                                                              
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   example.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git stash                                                                                                                                                               
Saved working directory and index state WIP on main: e856771 add file # the latest commit (HEAD) in your working branch

$ git stash list                                                                                                                                                         
stash@{0}: WIP on main: e856771 add file

$ git stash show stash@{0}                                                                                                                                                
 example.txt | 2  -
 1 file changed, 1 insertion( ), 1 deletion(-)

After stashing the dirty working directory, you would be able to make git pull from the remote branch.

The last step is restoring the temporary stashed files back to your working directory, which can be achieved by running git stash apply,

$ git stash apply stash@\{0\}                                                                                                                                              
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   example.txt

no changes added to commit (use "git add" and/or "git commit -a")

Alternatively, if the changes you want to restore is exactly the one you just stashed, you also could use git stash pop to pop out the latest stashed changes in the working directory. Noted that git stash drop would also remove it from your stash entries, but git stash apply wouldn't do that.

$ git stash pop                                                                                                                                                            
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   example.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (f28a4a5937601f0b7c23c2fb42444a08a89d7042)
  • Related