1016

I can't push now, though I could do it yesterday.

When I use git push origin master, I get an error:

$ git remote -v
origin  https://github.com/REDACTED.git (fetch)
origin  https://github.com/REDACTED.git (push)

$ git push origin master
Username for 'https://github.com': REDACTED
Password for 'https://[email protected]':
To https://github.com/REDACTED.git
! [rejected]         master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/REDACTED.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.

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

12

67 Answers 67

1256

(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")

(The following assumes github.com itself is not down, as eri0o points out in the comments: see www.githubstatus.com to be sure)

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

# Or git 2.37 Q2 2022+
git config --global push.autoSetupRemote true
git push

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

Again, with Git 2.37+ and its global option push.autoSetupRemote, a simple git push even for the first one would do the same (I.e: establishing a tracking relationship between your local main branch and its upstream branch origin/main).

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.

14
  • 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 stackoverflow.com/questions/18588974/… btw your answer looks helpful sir
    – leipzy
    Jun 9, 2014 at 6:51
  • 53
    For me, I just needed to run "git commit". :(
    – Tyler
    Jan 28, 2017 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, 2017 at 5:24
  • 4
    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 Dec 5, 2017 at 5:28
  • 2
    Turns out github had problems.
    – David J
    Jul 22, 2019 at 17:30
253

Try:

git push -f origin master

That should solve the problem.

Based on @Mehdi‘s comment, a clarification 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 a better solution.

4
  • 93
    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, 2018 at 9:42
  • 7
    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. Jan 23, 2019 at 19:13
  • I don't recommend it. Jul 12, 2021 at 9:54
  • this one worked for me on Nginx Producntion Environment.
    – Asad Ali
    Feb 15, 2022 at 14:59
145

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.

3
  • 2
    just ran into this. Commit command didn't work during the git add. good call. Thanks
    – jgritten
    Aug 2, 2018 at 2:44
  • 7
    Thanks man! That's it. I thought I committed my changes. Now git push -u origin master works fine.
    – tleo
    Jul 21, 2019 at 11:47
  • On my case, the commit failed because I haven't added my credentials yet on a fresh install. Running the commit command again after adding my credentials fixed my issue Apr 4 at 4:54
74

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

Once I did git commit -m "your msg", everything worked fine.

10
  • 14
    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 4, 2017 at 13:19
  • Oh I also forget to commit :p Jun 16, 2017 at 1:51
  • This is also possible it won't allow you to push with an empty commit
    – mboy
    Jun 22, 2017 at 8:11
  • 4
    I just had this problem and I forgot to commit. Error message should be more clear Nov 27, 2018 at 0:26
  • 1
    It does apply to me since I was getting that exact error message and this solution fixed my problem. Oct 14, 2019 at 17:38
65

It has worked for me with this combination of several command lines:

git reset 
git remote -v
git pull --rebase
git init
git add -A
git commit -m "Add your commit"
git branch -M main
git push origin main --force

Be careful. If they have a Readme file, the git reset deletes them.

3
  • 5
    This sounds like dangerous advice. All implications and caveats ought to be explained in the answer. Apr 10, 2022 at 13:21
  • 1
    An explanation would be in order. E.g., what is the idea/gist? What is it supposed to do? What are all the risks? From the Help Center: "...always explain why the solution you're presenting is appropriate and how it works". Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today). Apr 10, 2022 at 13:23
  • thi s was the right one
    – kaybrian
    Aug 6 at 21:10
48

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

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

This worked for me.

3
  • 3
    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, 2020 at 6:20
  • Thanks. As @cdabel mentioned, the hyphen was the culprit. Changed it to underscore and my push went through. Oct 24, 2020 at 7:48
  • 1
    I simply renamed my branch to something else (which contained a hyphen) and it also worked. Don't know why though. Mar 22, 2022 at 15:29
37
  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

6
  • before pushing the code you need to pull from repository
    – James Siva
    Sep 11, 2018 at 4:27
  • you can simply write like git pull --rebase origin master
    – James Siva
    Sep 11, 2018 at 4:27
  • This worked for me, had a refs issue, which this fixed. Sep 6, 2020 at 12:05
  • error: cannot spawn sh: No such file or directory fatal: unable to fork Dec 20, 2020 at 12:10
  • 1
    Thank you! Step 6 fixed it! Apr 20, 2022 at 18:21
22

I found the solution to this problem in GitHub help (Dealing with non-fast-forward errors):

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
1
  • 6
    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, 2017 at 16:51
20

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
2
  • 4
    this was the only solution that worked for me.
    – madacoda
    Feb 16, 2021 at 16:21
  • This is the final solution for beginners Oct 26, 2021 at 12:49
16

I had faced the same problem and fixed it with the below steps.

  1. git init

  2. git add .

  3. git commit -m 'Add your commit message'

  4. git remote add origin https://[email protected]/User_name/sample.git

    (The above URL, https://[email protected]/User_name/sample.git, refers to your Bitbucket project URL)

  5. git push -u origin master

Hint

Check if your GitHub account links with your local Git repository by using:

git config --global user.email "[email protected]"
git config --global user.name "Your Name"
15

If you were using git push origin master, change it to git push origin main and vice versa.

3
13

I got into this error today. The problem was that GitHub had an outage, and I could not push any changes, so before you start to mess up with the config, maybe check https://www.githubstatus.com.

remote: Resolving deltas: 100% (5/5), completed with 4 local objects.
remote: fatal error in commit_refs
To github.com:REDACTED.git
 ! [remote rejected] main -> main (failure)
error: failed to push some refs to 'github.com:REDACTED.git'
1
  • This was it for me
    – HeyImArt
    May 9 at 11:49
8

Remember to commit your changes before pushing to the GitHub repository. This might fix your problem.

1
8

I created an empty repository 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

The 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 executed the wrong sequence first, I just executed the below commands:

git add .
git commit -m 'Initial Commit'
git push -u origin master
8

Because maybe it has nothing to push (really, nothing to push). Do it 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's URL in your case. You can skip command git remote -v, just for checking.

8

I got this error because I tried to push without committing So tried

git add .

git commit -m "message"

git push -f

Then it worked well for me

1
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.

6

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

git push origin main

This is a common mistake beginners can make.

GitHub article Renaming the default branch from master.

5

Not committing initial changes before pushing also causes the problem.

5

Use:

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.

5

Try this Git command,

git push origin master –f
git push origin master --force
2
  • 1
    error: cannot spawn sh: No such file or directory fatal: unable to fork Dec 20, 2020 at 16:30
  • "–" in "–f" is an em dash, not ASCII 45 ("-"). I don't think it will work. Apr 10, 2022 at 12:46
5

Using a Git repository in Azure DevOps, the problem was a branch policy requiring that all changes to the branch must be made via a pull request (PR). 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.

4

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

4

Just run these two commands if you are deploying your site on GitHub pages for the first time.

git commit -m "initial commit"
git push origin +HEAD
4

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.

4

Due to the recent "replacing master with main in GitHub" action, you may notice that there is a refs/heads/main if you do git show-ref. As a result, the following command may change from

git push heroku master

to

git push heroku main

That will solve your issue.

3

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
  • Why did you need to rename a local branch? Apr 10, 2022 at 13:12
3

In my case, it was my husky package that disallowed 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://[email protected]/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 [email protected] does not satisfy loopback-utils@^0.9.0

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

1
  • I had a similar issue, except the pre-push hook ran a linter, and that was failing because I hadn't installed the packages. VSCode just gave me the final git error, but when I ran the git command directly it told me about the install issue. Running yarn install sorted it.
    – 404
    Apr 5, 2022 at 13:58
3

In my case, the 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 the branch solved my problem.

3

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
1
  • 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, 2020 at 20:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.