Plarent Haxhidauti
Plarent Haxhidauti

Reputation: 334

How can I safely commit my changes if the teammate has already pushed

I have the following situation. I have worked on some files and didn't commit/push. My teammate has worked on some other files (different from mine) and already committed/pushed his changes. How can I safely pull his changes, and then commit and push mine? Thank you in advance.

Upvotes: 2

Views: 1199

Answers (2)

Bill
Bill

Reputation: 1425

There are several different ways you could handle this. You could commit your changes and then pull. You could stash your changes, pull, and then apply your stash. You could fetch first, then commit and rebase. I personally like doing this a little more explicitly (with a fetch followed by rebase, instead of a pull which combines a fetch with a merge), but that is just personal preference.

  1. git fetch
  2. git add <files to add>
  3. git commit -m <commit message>
  4. git rebase <branch to replay commit onto (i.e. origin/master)>

The rebase step will provide you the opportunity to resolve any conflicts, if the other developer and you were working in the same space.

You should then be able to push your changes.

Upvotes: 4

Lajos Arpad
Lajos Arpad

Reputation: 76892

Pulling and merging

  • commit your changes
  • pull the other changes
  • fix merge conflicts if there are any
  • commit

Stashing

  • stash your changes
  • pull the commits from the repo
  • get back the stash code

Rebasing

Bill already covered that

branching

Create a branch where you commit your changes, switch back to the branch you were at and pull: Move existing, uncommitted work to a new branch in Git

Upvotes: 1

Related Questions