Rakesh Juyal
Rakesh Juyal

Reputation: 36759

Why I am always one commit behind from my own remote git repo?

Everytime I try to push changes to my own git repo, I get the error:

error: failed to push some refs to '--myrepo-here--'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Completed with errors, see above

Here is my workflow.
We have one git repo. [ master ]
Everyone in the team have forked the main repo.

  1. Do some changes in your repo (local).
  2. Push the changes to your own remote repo. Create the merge request.
  3. Do some changes in your repo (local). Meanwhile merge request got approved.
  4. Pull everything from [ master ] remote repo.
  5. Push the changes to your own remote repo. Create the merge request.

In any case no one can push to my repo. But I am getting this error frequently. It says I am 47ahead 1behind from remote repo.

Upvotes: 2

Views: 1509

Answers (2)

Michael Durrant
Michael Durrant

Reputation: 96484

Try rebasing against the changed master using this approach:

# Add the original remote, e.g. "upstream":
git remote add upstream https://github.com/whoever/whatever.git
# Fetch all the branches of that remote into remote-tracking branches,
# such as upstream/master:
git fetch upstream
# Make sure that you're on your intended branch, e.g. master:
git checkout master
# Rewrite your branch so that any commits of yours that
# aren't already in upstream/master are replayed on top of that branch:
git rebase upstream/master
# resolve any merge conflicts
# push the end result
git push origin master

Upvotes: 2

Leon
Leon

Reputation: 38

Seems like the result of doing a pull --rebase rather than just git pull MainRepo master.
You should read up on Git rebase.
And here's a Git Cheatsheet.

These steps would suffice for the workflow you're trying to accomplish:

When you're ready to do a pull and push your changes
git pull origin master

After resolving any conflicts
git commit -m "commit message" files

git push (or git push -u origin/your-branch)

Upvotes: 0

Related Questions