Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working on the same branch, how to pull without losing changes?

I am working on the same branch as my colleagues. Now I have committed some files and sent for code review and so has some other co-worker. Now he pushes his code before I do. I now need to pull his changes back and then add my changes. But it's already committed.

How do I get his changes and then add my changes without the history looking bad and me having to jump hoops? I am very new to git.

like image 471
user2759617 Avatar asked May 22 '15 07:05

user2759617


2 Answers

You can use git pull --rebase. This will fetch your collegues commits and then put your commits (that you haven't pushed) on top of them, keeping the history looking good as well.

Edit: Cyril CHAPON made some good points in his comment. Take a look at some of his links to fully understand the how rebase works and to avoid pitfalls.

like image 129
crea1 Avatar answered Sep 30 '22 20:09

crea1


As already stated you can do a git pull --rebase but there is also a different approach you go with: Imagine your history now look like this:

A -> B -> C -> D | | local master remote/master You can do the following:

$ git branch save_state $ git reset --hard C Basically this brings you to

A -> B -> C (remote/master) \ -> D (local save_state)

You know pull the changes from remote into your master

$ git pull origin master

Which will lead to a fast forward merge(history is clean up to here) And know you can do a

$ git checkout save_state $ git rebase master $ git checkout master && git merge save_state You can rebase this branch here without problems because it doesnt have been pushed to remote yet.

like image 26
ckruczek Avatar answered Sep 30 '22 20:09

ckruczek