Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Can't sync my local repo to remote repo

Lluis Camps
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 24, 2018

Hi.

I'm very new on git so please excuse my ignorance.

I'm trying to sync my local repository to my bitbucket repository without success.

First I added a remote through "git remote add origin url".

Then I tried pushing my local repository to my online repository by typing "git push -u origin master". That failed and I got this message:

! rejected
failed to push some refs to 'url'
Updates were rejected because the remote contains work that you do
not have locally. This is usually caused by another repository pushing
to the same ref. You may want to first integrate the remote changes
(e.g., 'git pull ...') before pushing again.
See the 'Note about fast-forwards' in 'git push --help' for details.

I've read around the notes given in that answer, but I can't figure out how to solve it.

Any help will be appreciated.

1 answer

1 accepted

0 votes
Answer accepted
Tyler T
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 24, 2018

Your local branch is not up-to-date with the changes in the remote so you are not being allowed to push new changes until your local branch is synced with the latest version of the remote branch.

You may want to first integrate the remote changes
(e.g., 'git pull ...') before pushing again. 

I would recommend the following:

# Create a new branch with your current work

git checkout -b 'new-branch'
git push -u origin new-branch

Now that branch will be in Bitbucket and you can either use the Bitbucket UI or the command line to sync your branch with master. If you use the Bitbucket UI you will still need to reset your local master branch to the remote.

Using the Bitbucket UI

  • Find your branch in Bitbucket
  • It should have a list of commits and something like #+ commits behind master
  • Click 'Sync now'

Using the command line*

# Sync your local master branch with the remote
# Use with caution: This will reset all changes in your local master branch

git checkout master
git fetch
git reset --hard origin/master
git checkout new-branch
git merge master

# Your new branch should not have the latest changes in master
# If there are conflicts you will need to fix them before moving on

git push origin new-branch

Now you can create a pull request from your new-branch to master. Using feature branches will help avoid this type of issue in the future. You can read more about that workflow here: https://www.atlassian.com/git/tutorials/comparing-workflows/feature-branch-workflow

 

* original post had incorrect command for git reset

Lluis Camps
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 24, 2018

Hi Tyler Tadej, thanks a lot for your reply.

Everything worked ok except for:

git reset --hard origin master 

which gave me this response:

fatal: ambiguous argument 'origin': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Anyway I managed to sort it out by typing the same without origin.

My new branch is now synced to my bitbucket repo.

Still I realise I need some (much) learning through the tutorial you send me, and I will.

Thanks again.

Tyler T
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 24, 2018

Oops, the correct command was:

git reset --hard origin/master

Glad it worked out 👍

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events