0

Currently, I'm facing a scenario that I have no idea which git command to execute. I pulled the source code last week, and I made changes to a java file. However, when I need to push my code, someone else had updated new codes on this java file. So I was unable to pull the latest code because this is a conflict.

Is there a way to pull the updated code before I push my commits?

Many thanks

Daniel Gao
  • 13
  • 1
  • 1
    Uhm, yes, it's called pulling, as in `git pull`. Since you already use this word then I'm pretty sure we're missing something here. If you pulled last week, then committed new commits locally, and when you try to push now you're told that there are new work on the remote, the standard response would be to do a `git pull`, which will first fetch those new commits and branch statuses down to your local repository then merge the new work from the remote to your local branch. After that you should be able to push. – Lasse V. Karlsen May 23 '20 at 16:43
  • I am afraid explaining how Git works is too broad for StackOverflow. I suggest you take a look at the official Git book, which is free. – Acorn May 23 '20 at 16:43
  • You’ll need to resolve the conflicts. – evolutionxbox May 24 '20 at 08:46

1 Answers1

0

git pull fetches the latest changes and then merges them into your branch. Like any merge, there can be a conflict. You have to resolve the conflict.

You start with a repository like this, with some commits on top of the last commit you pulled. Git remembers the last position they saw master on the remote called origin with a remote tracking branch called origin/master.

A - B [origin/master]
     \
      C - D [master]

git pull goes in two steps. First it updates origin/master with the latest commits.

A - B - E - F [origin/master]
     \
      C - D [master]

Then it does a merge.

A - B - E - F [origin/master]
     \       \
      C - D - M [master]

If there are conflicts, you have to resolve them. This is the same as finishing up a normal commit. Git has tried its best to produce a merge commit, but it needs you to make some decisions it cannot. Edit the files to fix the conflicts, use git add to stage the changes, and once you've got it all fixed up, git commit.

See "Basic Merge Conflicts" in Pro Git for how to handle that.

Schwern
  • 153,029
  • 25
  • 195
  • 336