Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I do either push or pull first?

Please imagine this:

My co-worker and I are working on the master branch. I've changed (added and removed) some code. At the same time my co-worker did some changes and pushed a commit to the master branch.

Now my current working directory is different than master branch and I want to keep both the changes on the master branch and my working directory.

What should I do in this case?

If I do a push, the master branch will be the same as my working directory (my co-worker's changes will be gone), If I do a pull first, naturally all my changes will be gone. Anyway, how can I handle such situation?

like image 761
stack Avatar asked Oct 28 '25 05:10

stack


2 Answers

Neither your changes nor the changes of your co-worker can get lost so easily when using Git. Once something is committed, it takes some effort to actually delete those committed changes again. That's one of the beautiful aspects of Git.

In the situation you describe, you won't be able to push anyway, because Git will detect that there are changes in the remote repository that are not available in your local repository. You are going to have to fetch those changes and then either merge them in or rebase your changes on top of the ones fetched from the remote repository.

To make it easy, you should probably do a git pull. Git will automatically merge your co-workers changes into your working copy, if possible. If your co-workers and your own changes conflict in a more complicated way, you'll have to merge the changes manually. But that should actually not happen very often if both of you follow a structured Git workflow.

like image 125
anothernode Avatar answered Oct 30 '25 22:10

anothernode


Assuming that you've committed the code locally, You should first do:

git pull origin master --rebase

This will fetch the code from remote and rebase it. Once this is done, simply push back the code to remote.

git push origin master
like image 33
Nerve Avatar answered Oct 30 '25 21:10

Nerve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!