609

For some reason, I can't push now, whereas I could do it yesterday. Maybe I messed up with configs or something.

This is what happens:

When I use the git push origin master

gitbashscr

What my working directory and remote repository looks like:

Screenshot of Windows file folder with these directories: .git, css, js. And these files: index.php, readme, setsu.php. The word "local" with an arrow points to the css-folder. Below, screenshot with heading "github", and a css-folder and index.php-file

Jonta
  • 386
  • 5
  • 25
leipzy
  • 7,275
  • 5
  • 14
  • 20
  • 9
    looks like your local repo is not in sync with the git repo. did you try to do git pull ? – R11G Jun 09 '14 at 06:25
  • 1
    yes, but I have no idea with the following syntax after git pull it says git pull , can you let me see an example syntax for git pull? – leipzy Jun 09 '14 at 06:29
  • 3
    Do check this similar question - http://stackoverflow.com/questions/18588974/git-prevents-pushing-after-amending-a-commit – R11G Jun 09 '14 at 06:33
  • 4
    @R11G thank you sir! this link helped me http://stackoverflow.com/a/18589043/3626672 – leipzy Jun 09 '14 at 06:42
  • 3
    I got that error on a new repo. This helped: http://stackoverflow.com/a/6518774/2067690 – HumanInDisguise Jul 08 '15 at 20:38
  • Kindly check if you are in `master` branch then run `git checkout -b main` and then `git push origin main` – Dhiraj Feb 16 '21 at 13:48

47 Answers47

845

(Note: starting Oct. 2020, any new repository is created with the default branch main, not master. And you can rename existing repository default branch from master to main.
The rest of this 2014 answer has been updated to use "main")

If the GitHub repo has seen new commits pushed to it, while you were working locally, I would advise using:

git pull --rebase
git push

The full syntax is:

git pull --rebase origin main
git push origin main

With Git 2.6+ (Sept. 2015), after having done (once)

git config --global pull.rebase true
git config --global rebase.autoStash true

A simple git pull would be enough.
(Note: with Git 2.27 Q2 2020, a merge.autostash is also available for your regular pull, without rebase)

That way, you would replay (the --rebase part) your local commits on top of the newly updated origin/main (or origin/yourBranch: git pull origin yourBranch).

See a more complete example in the chapter 6 Pull with rebase of the Git Pocket Book.

I would recommend a:

# add and commit first
git push -u origin main

That would establish a tracking relationship between your local main branch and its upstream branch.
After that, any future push for that branch can be done with a simple:

git push

See "Why do I need to explicitly push a new branch?".


Since the OP already reset and redone its commit on top of origin/main:

git reset --mixed origin/main
git add .
git commit -m "This is a new commit for what I originally planned to be amended"
git push origin main

There is no need to pull --rebase.

Note: git reset --mixed origin/main can also be written git reset origin/main, since the --mixed option is the default one when using git reset.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • is it OK to execute your suggested git pull --rebase...? coz I already done > git reset --mixed origin/master > git add . > git commit -m "This is a new commit for what I originally planned to be an amendmend" > git push origin master suggested here http://stackoverflow.com/questions/18588974/git-prevents-pushing-after-amending-a-commit/18589043#18589043 btw your answer looks helpful sir – leipzy Jun 09 '14 at 06:51
  • 8
    For me, I just needed to run "git commit". :( – Tyler Jan 28 '17 at 21:00
  • Thanks, fixed a stupid issue with Git LFS, I've surrendered myself to having to use the command line from now on as a result haha. – Tyler C Mar 30 '17 at 05:24
  • In my case, I can push using Github Desktop Application but I can't in IntellijIdea. I got the same error as mentioned by OP in IntellijIdea SVN. I'm not sure if this is related to the local repo. – MaXi32 Oct 03 '17 at 10:02
  • @MaXi32 Thenj it is best to ask a new question, with full details (OS, version of IntelliJ, Git, GitHub Desktop, URL,...) – VonC Oct 03 '17 at 10:44
  • 2
    Really super.. below commands worked for me... git reset --mixed origin/master git add . git commit -m "This is a new commit for what I originally planned to be amended" git push origin master Thank you @VonC – Hari Narayanan Dec 05 '17 at 05:28
  • Turns out github had problems. – David J Jul 22 '19 at 17:30
  • @DavidJ Yes: https://twitter.com/coreyhaines/status/1153335824563539968 and https://twitter.com/JustinBeckwith/status/1153333169011118080 – VonC Jul 22 '19 at 18:57
  • I also needed to do `git prune` in before pushing. – Joao Coelho Feb 01 '20 at 21:27
  • 1
    Thanks it works ! I was looking for hours for this solution! – Redion Xhepa Mar 12 '20 at 22:08
  • Warning, this will delete any existing local files. Before doing it take a copy of your local folder that contains your project files. – EngineSense Mar 09 '21 at 03:11
  • My whole project crashed, nice one – James 666 May 11 '21 at 11:45
  • @James666 I am sorry to hear that. To avoid others having the same issue, what part of this answer was problematic in your case? – VonC May 11 '21 at 11:51
  • 1
    Your last set of commands really helped me. Thank you – Krits May 11 '21 at 19:04
157

Did anyone try:

git push -f origin master

That should solve the problem.

EDIT: Based on @Mehdi ‘s comment below I need to clarify something about —force pushing. The git command above works safely only for the first commit. If there were already commits, pull requests or branches in previous, this resets all of it and set it from zero. If so, please refer @VonC ‘s detailed answer for better solution.

cagcak
  • 2,732
  • 2
  • 18
  • 21
  • 45
    Works but bad, please don't use it unless you know what you're doing. (probably you don't know what you're doing if you're looking in S.O) – Mehdi Jan 19 '18 at 09:42
  • 4
    If you're going to try `-f`/`--force` it's always safer to use `--force-with-lease` instead, which will abort if there are downstream changes that would get clobbered by the push. `--force-with-lease` is required for lots of everyday rebasing situations, but `--force` should almost never be needed. – Joshua Goldberg Jan 23 '19 at 19:13
94

If you just used git init and have added your files with git add . or something similar and have added your remote branch it might be that you just haven't committed (git commit -m 'commit message') anything locally to push to the remote... I just had this error and that was my issue.

ironcladmvtm
  • 1,078
  • 7
  • 5
  • 1
    just ran into this. Commit command didn't work during the git add. good call. Thanks – jgritten Aug 02 '18 at 02:44
  • 3
    Thanks man! That's it. I thought I committed my changes. Now git push -u origin master works fine. – tleo Jul 21 '19 at 11:47
48

I had same problem. I was getting this problem because i had not made any commit not even initial commit and still i was trying to push.

Once i did git commit -m "your msg" and then everything worked fine.

pramodpotdar17
  • 691
  • 5
  • 11
  • 13
    That doesn't make much sense. The original question is about the **local** git being **behind**. In no way "being behind" can be resolved my making a local commit! – GhostCat Jan 04 '17 at 13:19
  • Oh I also forget to commit :p – Shams Nahid Jun 16 '17 at 01:51
  • This is also possible it won't allow you to push with an empty commit – mboy Jun 22 '17 at 08:11
  • An initial commit fixed it. – Amit Bhagat Aug 07 '17 at 09:22
  • In my case, I had cherry-picked something onto my new branch and then tried to push, but got the error described in the new question; I needed to also add an "original" commit to the branch before I was able to push successfully. – Jon Schneider Oct 29 '18 at 21:49
  • 4
    I just had this problem and I forgot to commit. Error message should be more clear – Ivan Topić Nov 27 '18 at 00:26
  • Why people are upvoting this answer :P This answer does not apply to question asked. – Kumar Sukhani Jun 12 '19 at 12:22
  • 1
    It does apply to me since I was getting that exact error message and this solution fixed my problem. – Diego Fortes Oct 14 '19 at 17:38
  • Strange.. I did what you just suggested.. I had nothing to commit. But it worked. Just before I tried to commit again I did amend for changing last commit message.. don't really know if it relates.. – lastboy Apr 05 '20 at 20:04
30

Rename your branch and then push, e.g.:

git branch -m new-name
git push -u new-name

This worked for me.

clemens
  • 14,173
  • 11
  • 38
  • 52
p8ul
  • 1,174
  • 12
  • 14
  • 1
    Wow, this actually worked, but why? I had a hyphen in my local branch name: `my-branch_wont_push`. Once I renamed it to `my_branch_wont_push`, then `git push -u origin my_branch_wont_push` worked for me. – cdabel Apr 23 '20 at 06:20
  • Thanks. As @cdabel mentioned, the hyphen was the culprit. Changed it to underscore and my push went through. – Annie Lagang Oct 24 '20 at 07:48
17
  1. git init

  2. git remote add origin https://gitlab.com/crew-chief-systems/bot

  3. git remote -v (for checking current repository)

  4. git add -A(add all files)

  5. git commit -m 'Added my project'

  6. git pull --rebase origin master

  7. git push origin master

papanito
  • 1,387
  • 2
  • 16
  • 33
James Siva
  • 613
  • 7
  • 16
15

I find the solution to this problem in github help.

You can see it from:Dealing with non-fast-forward errors

It says:

You can fix this by fetching and merging the changes made on the remote branch with the changes that you have made locally:

$ git fetch origin
# Fetches updates made to an online repository
$ git merge origin branch
# Merges updates made online with your local work

Or, you can simply use git pull to perform both commands at once:

$ git pull origin branch
# Grabs online updates and merges them with your local work
Sealter
  • 219
  • 3
  • 6
  • 3
    This is the normal process whenever things are working as expected. It doesn't help anything when git thinks it is already up to date as @rubyandcoffee asked. – Tim Dec 22 '17 at 16:51
13

I followed the following steps and it worked for me.

 rm -rf .git
 git init
 git add .
 git commit -m"first message"
 git remote add origin "LINK"
 git push -u origin master
ASHISH RANJAN
  • 3,171
  • 1
  • 17
  • 11
11

I had faced same problem,fixed with below steps .

  1. git init
  2. git add .
  3. git commit -m 'Add your commit message'
  4. git remote add origin https://User_name@bitbucket.org/User_name/sample.git

    (Above url https://User_name@bitbucket.org/User_name/sample.git refers to your bit bucket project url )

  5. git push -u origin master

hint

check if your git hub account link with your local git by using:

git config --global user.email "you@example.com"
git config --global user.name "Your Name"
papanito
  • 1,387
  • 2
  • 16
  • 33
Chitu
  • 193
  • 2
  • 14
6

If you are using gerrit, this could be caused by an inappropriate Change-id in the commit. Try deleting the Change-Id and see what happens.

Jim Sime
  • 131
  • 2
  • 1
6

Remember to commit your changes before pushing to Github repo. This might fix your problem.

Alf Moh
  • 6,091
  • 3
  • 34
  • 46
5

Not commiting initial changes before pushing also causes the problem

Claw
  • 51
  • 1
  • 2
5
git push origin {your_local_branch}:{your_remote_branch}

If your local branch and remote branch share the same name, then can you omit your local branch name, just use git push {your_remote_branch}. Otherwise it will throw this error.

Yitong Feng
  • 147
  • 1
  • 7
5

I created an empty repo in GitHub, and have my code locally. I faced the same issue now, as I followed the below sequence,

git init
git commit -m 'Initial Commit'
git remote add origin https://github.com/kavinraju/Repo-Name.git
git add .
git push -u origin master

ISSUE WAS: I tried to commit before staging the files I have.

SO WE NEED TO STAGE THE FILES AND THEN COMMIT.

This is the correct sequence.

git init
git add .
git commit -m 'Initial Commit'
git remote add origin https://github.com/kavinraju/Repo-Name.git
git push -u origin master

Since I execute the wrong sequence first, I just execute the below commands

git add .
git commit -m 'Initial Commit'
git push -u origin master
Kavin Raju S
  • 577
  • 6
  • 20
5

Because maybe have nothing to push (really, no thing to push). Do like this

git remote add origin https://github.com/donhuvy/accounting133.git
git remote -v
git add .
git commit -m"upload"
git push --set-upstream origin master

Change the remote repository url in your case. Command git remote -v you can skip, just for checking.

Do Nhu Vy
  • 33,131
  • 37
  • 143
  • 202
4

before push you have to add and commit the changes or do git push -f origin master

Muhammad
  • 2,851
  • 4
  • 38
  • 64
3

Try this git command,

git push origin master –f
git push origin master --force
Zin Myo Swe
  • 422
  • 5
  • 19
2

Creating a new branch solved for me:

git checkout -b <nameOfNewBranch>

As expected no need to merge since previous branch was fully contained in the new one.

  • 1
    I had this issue exactly, I was on feature22 and was doing `git push origin feature22-fix`, but `feature22-fix` didn't exit neither local nor remote, so I had to first checkout the branch locally, then push – Honey Jun 24 '19 at 18:37
2

It may happen when you don't have any files. Try to create a text file then follow the following commands

git add .
git commit -m "first commit"
git push --set-upstream origin master
sushil suthar
  • 459
  • 5
  • 11
2

For me the Problem was, I did not add the files before the commit.

git add .

git commit -m "your msg"

Rohitsam
  • 53
  • 7
2

do these

git rm --cached *
git add .
git commit -m"upload"
git push --set-upstream origin master

Happy coding!

Do Nhu Vy
  • 33,131
  • 37
  • 143
  • 202
2

Best use rm -rf .git/hooks and then try git push

Binod Singh
  • 654
  • 3
  • 11
  • 20
2

I faced this issue but after 2 days I got a solution. Just run these two commands if you are deploying your site on GitHub pages first time.

git commit -m "initial commit"
git push origin +HEAD
azeem
  • 131
  • 4
2

In my case there was a problem with a git pre-push hook.

Run git push --verbose to see if there are any errors.

Double check your git-hooks in the directory .git/hooks or move them temporarily to another place and see if everything works after that.

Jochen Holzer
  • 1,029
  • 11
  • 16
1

Not sure if this applies, but the fix for me was to commit something locally after git init. Then I pushed to remote using --set-upstream ...

user1889992
  • 193
  • 2
  • 7
1

If you are attempting to initialize a directory with an existing GitHub repository, you should ensure you are committing changes.

Try creating a file:

touch initial
git add initial
git commit -m "initial commit"
git push -u origin master

That will place a file named initial that you can delete later.

Hope this answer helps! Goodluck!

Andreas Bigger
  • 3,471
  • 1
  • 9
  • 16
1

In my case, it was my husky package that disallows the push.

> husky - pre-push hook failed (add --no-verify to bypass)
> husky - to debug, use 'npm run prepush'
error: failed to push some refs to 'https://username@bitbucket.org/username/my-api.git'

To push it forcefully, just run git push origin master --no-verify

I ran npm run prepush to see debug the error, and this was the cause:

npm ERR! code ELOCKVERIFY
npm ERR! Errors were found in your npm-shrinkwrap.json, run  npm install  to fix them.
npm ERR!     Invalid: lock file's loopback-utils@0.8.3 does not satisfy loopback-utils@^0.9.0

Ran npm install and commit it, and the problem is fixed.

Jee Mok
  • 4,537
  • 7
  • 35
  • 66
1

In my case branch name prefix was already present at remote so basically if you have a branch name 'fix' you cannot push another branch with name 'fix/new_branch_name', renaming branch solved my problem.

1

The fact that GitHub changed master to main made me encounter this issue. So from now on, the solution to push to origin is:

git push -u origin main
Roland Lariotte
  • 1,487
  • 1
  • 6
  • 22
  • Do you means the remote repo on GitHub has only a branch main, not master? I have created a brand new repo on GitHub, and its main branch remains... master (for now) – VonC Jun 29 '20 at 20:21
1

This is probably not a common problem, but it happened to me, so I'll add it as a comment here in case someone else makes the mistake I did.

I created a custom pre-push file, and I forgot to end it with exit 0

That caused me to get this "failed to push some refs" error. I added exit 0 to the end of my pre-push hook and, of course, it works fine now.

Mike
  • 39
  • 1
  • 4
1

Using a Git repo in Azure DevOps, the problem was a branch policy requiring that all changes to the branch must be made via pull request. Trying to push changes directly to the branch generated the error "failed to push some refs to ...". I created a PR-branch and pushed without problem.

Markus
  • 191
  • 5
1

Github changed the default branch name from master to main. So if you created the repo recently, try pushing main branch

git push origin main

This is a common mistake, beginners can make.

Github Article

shivampip
  • 1,306
  • 11
  • 14
1

git push -f origin master.

this one is write

0

Well if none of the above answers are working and if you have messed up something with ssh-add lately. Try

ssh-add -D
Abhisek
  • 2,680
  • 3
  • 14
  • 24
0

You need to give some force

Just do push --force.

Ankit
  • 4,399
  • 1
  • 18
  • 14
0

For sourcetree users

First do an initial commit or make sure you have no uncommited changes, then at the side of sourcetree there is a "REMOTES", right-click on it, and then click 'Push to origin'. There you go.

Bowie Chang
  • 116
  • 8
0

This happens to me as I had a tag with the same as the branch and I tried to push the branch to remote.

Buminda
  • 595
  • 7
  • 15
0

In my case the problem was that (strangely) there was no branch called master. I took the repository from Github.

D063520
  • 53
  • 5
0

I was pushing existing typo 'evelop' branch which I did not have checkout yet, and instead, I wanted to push a branch called 'envelope'. So the branch must be existing/checkout out at local working copy in order to push of course, therefore that error, not to make a typo.

FantomX1
  • 1,023
  • 1
  • 10
  • 18
0

Unfortunately, I could not solve the problem with the other solution but my problem was that the branch name I want to push was not accepted by remote. I change it to the correct format and accepted.

it was test/testing_routes and needed to change it to testing_route which the / is not allowed by remote.

You should ensure that the branch name format is correct.

Mesut GUNES
  • 5,532
  • 2
  • 22
  • 43
0

This issue comes when remote server has some extra commit which is not available in your working directory. Below is the solution to fix this issue.

  1. To get latest code from remote server to local and then push

    >git pull
    >git push
    
  2. Directly do the force push to the remote server.

    >git push --force
    

    if #1 will not work then use #2 option

    Use below command to get all the options that is related to push

    > git push --help
    
Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
Sheo Dayal Singh
  • 1,173
  • 14
  • 9
0

In my case I misspelled the name of the branch. Locally I did something like:

git push --set-upstream origin feture/my-feature

where my branch name was missing the a in feature. I corrected it to:

git push --set-upstream origin feature/my-feature

And everything worked fine.

P.Brian.Mackey
  • 39,360
  • 59
  • 210
  • 327
0

In our case, retrying to push solved the problem. Probably a network slowness caused the issue.

Onat Korucu
  • 512
  • 6
  • 9
0

In my case, I missed amending, I just needed to run git commit --amend and then push. It fixed the issue. It might help someone who has previously committed code.

Ashvini Maurya
  • 461
  • 1
  • 7
  • 13
0

These steps worked for me:

  1. Switch to current branch & pull latest code

  2. Rename local branch

    git branch -m [new-name]

  3. Push local branch to server

    git push origin [new-name]

  4. Remove branch from server

    git push origin --delete [old-name]

-1

Solution:

Download git bash and run the command on it. Eventually it worked!

Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
Rajodiya Jeel
  • 25
  • 1
  • 6
-2

git error: failed to push some refs to also comes when the local repository name does match with the corresponding remote repository name. Make sure you are working on the correct pair of repository before you Pull changes to remote repository. In case you spell incorrectly and you want to remove the local repository use following steps

Remove the local repo from windows

  1. del /F /S /Q /A .git
  2. rmdir .git
  3. Correct the local folder name (XXXX02->XXXX20) or if it is a newly created repo delete it and recreate the repo (XXXX02 Repo name changed to XXXX20).
  4. git init
  5. Remap with remote repo if it is not mapped.
  6. git remote add origin https://github.com/<username>/XXXX20.git
  7. git push -u origin master
papanito
  • 1,387
  • 2
  • 16
  • 33