user3810618
user3810618

Reputation: 31

pull latest commit from a branch in git

my friend and I are working on a particular branch of a repository in Github at the same time, however, when I try to push my changes, if he updated the files before I pull and I try to push mines I get an error from github, and I was wondering if is there any correct to pull the changes before I have changed some files?

so far, I have tried to use fetch origin branch-name but this adds some HEAD content in my files, is there a missing step or a better way to do it?

all I basically need is be able to pull the latest changes on github repository and then push my changes.

thanks, any advice would be very much helpfull

Upvotes: 3

Views: 24930

Answers (2)

Matthew Jacobs
Matthew Jacobs

Reputation: 167

git fetch origin branch-name retrieves the information from the server but it doesn't do any merging.

For advanced users this is a good thing because it gives added control and flexibility. For new users it just adds unnecessary complexity. New users should be using git pull which, as @Jon said, is a shortcut for a fetch followed by a merge.

As @Jon said, you might be better off creating your own branch.

In my opinion, you should be working on the same branch if:

  • There are only two of you.
  • Neither of you has a lot of "work in progress" that can't be checked in right away.
  • Neither of you checks in very frequently.

If you add more people to the equation, it makes conflicts more likely and it makes it more likely that putting people on their own branch is a good solution. Similarly, if someone has to work on a feature that requires a lot of work but they don't want to check in intermediate steps on the main branch, that's another good reason to put that feature on its own branch. Finally, if there are frequent checkins on the main branch then it may be a good thing to work on your own branch. (Or it might not. This one depends on your philosophy.)

Upvotes: 1

Jon
Jon

Reputation: 10898

The easiest solution here would be to work on different branches (assuming you're working on different features) and then merge them once complete.

However, if that's not an option, you simply need to pull the remote changes onto your local branch before pushing yours. Assuming you're working on the branch named awesome-feature, you could do the following:

git fetch
git merge origin/awesome-feature

There is a handy shortcut for this exact process:

git pull

However, if you want to do anything other than a standard merge then you should simply fetch and do any merging/rebasing yourself manually.

Once you have the remote changes and your branch is up to date, you can commit and push without any issues.

Upvotes: 3

Related Questions