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.
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
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Oops, the correct command was:
git reset --hard origin/master
Glad it worked out 👍
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.