0

My friend and I are working on a project on GitHub.

He recently pushed code to the repo which I didn't have and I already wrote some code, which isn't pushed. Meaning, the repo on GitHub has his commit but my local repo doesn't have that commit, so it's a commit behind. But I've made some changes of my own to the local repo, which I don't want to lose.

Now I'm stuck, I can't push without removing his code, and I can't pull without removing my changes. So how do I keep both the changes? I've heard of merge, but I can't figure out how to get his commit and merge it with mine.

How do I keep both mine and his changes?

svineet
  • 1,859
  • 1
  • 17
  • 28
  • 1
    Semi-related: [What's the difference between 'git pull' and 'git fetch'?](http://stackoverflow.com/questions/292357/whats-the-difference-between-git-pull-and-git-fetch) –  Mar 02 '14 at 16:36

2 Answers2

1

You can pull without removing your changes. What git pull does behind the scenes is equivalent to:

git fetch remote
git merge remote/branch

Therefore your changes will be safe and the changes of your coworker will be included. A new merge commit is created to merge the diverged working copies.

In the cases you did not make any changes git normally does a Fast Forward, which will set your state to the others. git only does this when it is safe to do so!


In case you did not commit your changes yet: Either commit them or read up about git stash, git won't allow you to pull until it is safe to do so!

TimWolla
  • 31,849
  • 8
  • 63
  • 96
1

You commit your changes to your local repo. Then you execute

git pull

which will merge his changes with yours, or

git pull --rebase

which will rewrite your changes on top of his changes, as if you had written them after.

Of course, whatever the strategy you choose, there is a possibility for conflicts, that you'll have to resolve. Follow the instructions.

This is covered by any Git book, and several are available for free online. Look them up.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255