Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pull from git while having unpushed commits

I have several commits that aren't pushed yet. But there're several new commits in the repository. I want to pull the new commits and push my commits, but I'm not sure what's the correct way to do this. If I do a pull as is I think it tries to merge and I'm not sure what becomes of my commits, please suggest what's the solution for such situation.

like image 427
Natasha Avatar asked Jul 29 '16 07:07

Natasha


3 Answers

You have two main options here, you can either merge the remote branch into your branch then push, or you can rebase your branch on the remote, and then fast-forward the remote branch.

Option 1: merge

git pull origin yourBranch           # does a fetch, followed by a merge
git push origin yourBranch           # push merged branch to remote

Option 2: rebase

git pull --rebase origin yourBranch  # does a fetch, followed by a rebase
git push origin yourBranch           # ideally this will fast-forward the remote,
                                     # meaning all your commits will be played on top

Merging will collapse your commits into a single merge commit which will appear in the remote branch, whereas rebasing will preserve your commits, in order, in the remote branch.

In either case, you won't lose the work you have done, though some (or all) of the commits you made might not be preserved in the remote branch if you go with the merge option.

like image 162
Tim Biegeleisen Avatar answered Nov 11 '22 16:11

Tim Biegeleisen


git pull --rebase is often the most useful thing to do here. Effectively it will:

  1. Roll back your repository to before your unpushed changes
  2. Pull from upstream (this won't require a merge, just a fast-forward)
  3. Attempt to re-apply your changes that were rolled back in step 1.

If there are no conflicts, then this will go ahead without any questions asked, and you'll be in a state where you can push your changes cleanly without any merging required. If there are conflicts, you will have to resolve them during the rebase process — potentially a lot of work, depending on the size of the conflicts and the number of unpushed commits.

like image 42
hobbs Avatar answered Nov 11 '22 16:11

hobbs


You will need to do git pull in any ways. Then resolve merge (if it will be), then make new commit and only then push to the repository.

like image 1
Nataliya U Avatar answered Nov 11 '22 14:11

Nataliya U