-1

I'm following a book about Django and when I'm trying to push my file to Bitbucket repo , I got this error : I wrote in cmd:

git init 
git status
git add -A
git commit -m "comment"
  • Creating an account in Bitbucket

in cmd:

git remote add origin https://dd@bitbucket.org/dd/simple-app.git
git push -u origin master

I received this Error:

! [rejected]        master -> master (non-fast-forward) error: failed
to push some refs to 'https://dd@bitbucket.org/dd/simple-app.git'
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.
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Dani MHM
  • 37
  • 1
  • 1
  • 6
  • Possible duplicate of [Updates were rejected because the tip of your current branch is behind](https://stackoverflow.com/questions/39399804/updates-were-rejected-because-the-tip-of-your-current-branch-is-behind) – phd Jul 27 '19 at 13:30
  • https://stackoverflow.com/search?q=%5Bgit%5D+hint%3A+Updates+were+rejected+because+the+tip+of+your+current+branch+is – phd Jul 27 '19 at 13:30

3 Answers3

1

To avoid these headaches in the future, a good practice would be to clone the remote repo if it's empty or not.

git clone https://remote.repo

cd into the new directory and commit, push, pull to your hearts content!

If you do plan on working with something local, then git init is the way to go, and then you can create an empty Bitbucket repo later down the road. Add the ref to origin like you did with git remote add origin [url] and then continue from there.

By cloning, you avoid using git init altogether and can commit freely without the hassle of worrying about having two completely separate commit histories.

The beauty of Git is it's ability to perform otherwise complex version control tasks in a simple manner, don't over think it.

gigalul
  • 416
  • 3
  • 13
0

As the hint says - pull in remote changes. Doing git pull origin master and then git push -u origin master should fix your problem.

mariusz-r
  • 56
  • 3
  • Thank you for answer . I did this command already but i had this error : * branch master -> FETCH_HEAD fatal: refusing to merge unrelated histories – Dani MHM Jul 27 '19 at 11:45
  • What about `git fetch origin master` and then `git rebase origin/master` ? – mariusz-r Jul 27 '19 at 11:55
  • mariusz I tried this : [{ git push --force origin master } and it was successful but i just wanna know what's problem because the common way is without < --force > – Dani MHM Jul 27 '19 at 12:23
  • When you created a repo (remote) on bitbucket it probably was created with some commits. Then you've created a local repo with **different** commit, so the remote had totally different commits/history. Git will refuse to merge by default in this situation. One solution then is to overwrite the remote using --force as you did. You don't need to use --force on succeeding pulls. Rebasing as I've proposed is safer, as it won't overwrite the remote history and possibly loose data. – mariusz-r Jul 27 '19 at 12:34
  • Thank you @mariusz-r for your comprehensive answer. I was thinking the same reason. When you creat repo on BitBucket they put a Readme.dm file in the repo , I guess this reason first I have to pull this file to my local and push all files again to the remote repo?? – Dani MHM Jul 27 '19 at 12:58
  • Yes, exactly, but as you've seen a pull won't work also. I think next time when you create a repo on bitbucket you should test the `rebase` method. – mariusz-r Jul 27 '19 at 13:18
0

When you created the Project in Bitbucket, I assume you have marked the option to initialize the repository with a README file. This result in an initial commit.

You local git repository does not have this commit so when you try to push your code you get the error which tells you that you need to pull before the push.

All you have to do is to commit your local changes (which you did according to your question), so now simply pull the changes and then push again

# get the "updates" from the server
git pull 

# Now if there are no conflicts push your code
git push 

Note

Since git version 2.X you don't need to add `git push since it will push the current branch

Git v2.0 Release Notes

When "git push [$there]" does not say what to push, we have used the traditional "matching" semantics so far (all your branches were sent to the remote as long as there already are branches of the same name over there). In Git 2.0, the default is now the "simple" semantics, which pushes:

  • only the current branch to the branch with the same name, and only when the current branch is set to integrate with that remote branch, if you are pushing to the same remote as you fetch from; or

  • only the current branch to the branch with the same name, if you are pushing to a remote that is not where you usually fetch from.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Thank you for answer. I did git pull but still had problem . I guess I should write 'git pull Readme.me ' or 'git pull Readme.me master' ?? – Dani MHM Jul 27 '19 at 13:15
  • Almost, when you pull you don't specify a path, you pull the **whole** content, to pull "part" of your repository is a different story referred as "sparse checkout" – CodeWizard Jul 27 '19 at 13:17