简体   繁体   中英

How to integrate local changes with changes on the remote repository in git?

I have a local project which is also present as a git repository.

Recently, me and my colleague worked on two different changes on the project. He modified some files and pushed it on the remote repository. I made some changes which are present in my local repository and do not conflict with any of his changes on the remote repository.

I need to first get his changes from remote repository and push my changes along with his that I just got to the remote repository. How do I do it?

Will something like - git pull origin master override my local changes with the current project on the remote repository?

Is there a way I can merge these commits?

First commit your work via:

git add <path/to/file1>
git add <path/to/file2>
# ... etc. for each file
git commit -m 'commit message goes here'

Then you can either bring in his changes via merging:

git pull origin master
git push origin master

Or you can rebase your work on the remote master branch:

git pull --rebase origin master
git push origin master

In both cases, so long as you have committed your work, nothing will be lost. Your commit will be there somewhere in your local master branch when you go to push to the remote repository.

A git pull --rebase (meaning fetch+rebase) is the best practice.

It will replay your local commit on top of the updated origin/master .
Then git push will publish your commits.

If you have Git 2.9 or more , I would recommend:

git config --global pull.rebase true
git config --global rebase.autoStash true

Then a simple git pull is enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM