997

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://REDACTED@github.com':
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

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
leipzy
  • 11,676
  • 6
  • 19
  • 24
  • 19
    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
  • 9
    Do check this similar question - http://stackoverflow.com/questions/18588974/git-prevents-pushing-after-amending-a-commit – R11G Jun 09 '14 at 06:33
  • 5
    @R11G thank you sir! this link helped me http://stackoverflow.com/a/18589043/3626672 – leipzy Jun 09 '14 at 06:42
  • 5
    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
  • 21
    hey, if you find this on Google at this time, check if GitHub is down -> https://www.githubstatus.com/ – eri0o Aug 10 '21 at 15:59
  • 1
    for me, I had to add and commit – TechDog Nov 30 '21 at 06:19
  • Here is Step By Step Solution >> https://stackoverflow.com/questions/24114676/git-error-failed-to-push-some-refs-to-remote/70585050#70585050 – Rizwan Jan 04 '22 at 21:05
  • Definitely check githubstatus.com ! Start there. – SmileBot Mar 17 '22 at 16:08
  • Related: *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way***, *such as to provide screenshots of a user interface.*). – Peter Mortensen Apr 10 '22 at 13:37
  • Check if your `branch name` is the same in the local and remote repo. You might have named your main branch `master` in the remote repo and `Main` in the local repo. – ARiyou Jahan Feb 20 '23 at 12:07

65 Answers65

1242

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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 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
  • 49
    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
  • 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 – Hari Narayanan Dec 05 '17 at 05:28
  • 2
    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
  • 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
  • it happens when you don't specify branch name before pushing upto repo, simply use "git branch -M main" & it should work – Seeschon Aug 12 '21 at 07:52
  • @TylerC same here... wonder howd that happen. Ive done it countless times.. perhaps the commit wasnt registered the first time it was entered... – Gel Feb 06 '23 at 15:02
250

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cagcak
  • 3,894
  • 2
  • 21
  • 22
  • 92
    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
  • 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. – Joshua Goldberg Jan 23 '19 at 19:13
  • I don't recommend it. – Menai Ala Eddine - Aladdin Jul 12 '21 at 09:54
  • this one worked for me on Nginx Producntion Environment. – Asad Ali Feb 15 '22 at 14:59
144

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,576
  • 1
  • 9
  • 6
  • 2
    just ran into this. Commit command didn't work during the git add. good call. Thanks – jgritten Aug 02 '18 at 02:44
  • 7
    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
  • 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 – Ricky Manalo Apr 04 '23 at 04: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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pramodpxi
  • 977
  • 8
  • 13
  • 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 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
  • I have done all things properly but getting an error after sowing your message I typed git status and it shows no commit. so I go through your process and was finally done. – Chetan Nada Mar 12 '23 at 17:01
62

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 5
    This sounds like dangerous advice. All implications and caveats ought to be explained in the answer. – Peter Mortensen Apr 10 '22 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](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/69852137/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Apr 10 '22 at 13:23
  • thi s was the right one – kaybrian Aug 06 '23 at 21:10
47

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

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

This worked for me.

clemens
  • 16,716
  • 11
  • 50
  • 65
p8ul
  • 2,212
  • 19
  • 19
  • 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 '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
  • 1
    I simply renamed my branch to something else (which contained a hyphen) and it also worked. Don't know why though. – PROgram52bc Mar 22 '22 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

papanito
  • 2,349
  • 2
  • 32
  • 60
James Siva
  • 1,243
  • 1
  • 14
  • 19
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
Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
Sealter
  • 301
  • 3
  • 7
  • 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 '17 at 16:51
21

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 R
  • 4,043
  • 1
  • 20
  • 16
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://User_name@bitbucket.org/User_name/sample.git

    (The above URL, https://User_name@bitbucket.org/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 "you@example.com"
git config --global user.name "Your Name"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chitu
  • 353
  • 4
  • 14
15

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Babatunde Mustapha
  • 2,131
  • 20
  • 21
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'
vikiival
  • 363
  • 3
  • 7
8

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alf Moh
  • 7,159
  • 5
  • 41
  • 50
  • this answer is expanded upon in [this answer](https://stackoverflow.com/a/41464745/11107541), [this answer](https://stackoverflow.com/a/63193062/11107541), and [this answer](https://stackoverflow.com/a/74117926/11107541) (all later than this one was posted. moral of story: examples are valuable) – starball May 04 '23 at 18:15
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kavin Raju S
  • 1,214
  • 2
  • 17
  • 25
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vy Do
  • 46,709
  • 59
  • 215
  • 313
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

KORLA GOUTHAM
  • 141
  • 2
  • 4
  • this answer is similar to [this answer](https://stackoverflow.com/a/41464745/11107541), [this answer](https://stackoverflow.com/a/63193062/11107541), and [this answer](https://stackoverflow.com/a/41127879/11107541) – starball May 04 '23 at 18:17
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jim Sime
  • 131
  • 2
  • 1
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shivampip
  • 2,004
  • 19
  • 19
5

Not committing initial changes before pushing also causes the problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Claw
  • 51
  • 1
  • 2
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yitong Feng
  • 243
  • 3
  • 9
5

Try this Git command,

git push origin master –f
git push origin master --force
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zin Myo Swe
  • 778
  • 2
  • 10
  • 24
  • 1
    error: cannot spawn sh: No such file or directory fatal: unable to fork – Deepak Keynes Dec 20 '20 at 16:30
  • "–" in "–f" is an [em dash](https://en.wiktionary.org/wiki/em_dash#Noun), not [ASCII 45](https://en.wikipedia.org/wiki/ASCII#Printable_characters) ("-"). I don't think it will work. – Peter Mortensen Apr 10 '22 at 12:46
4

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Markus
  • 341
  • 1
  • 5
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
azeem
  • 341
  • 3
  • 4
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jochen Holzer
  • 1,598
  • 19
  • 25
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LOTUSMS
  • 10,317
  • 15
  • 71
  • 140
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://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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jee Mok
  • 6,157
  • 8
  • 47
  • 80
  • 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 05 '22 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Roland Lariotte
  • 2,606
  • 1
  • 16
  • 40
  • 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
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]

3

In my case these two lines solved the problem.

git add .
git commit -m "Changes"

Actually, I forgot to add and commit to my changes and was just trying to push it for the first time.

git init
git remote add origin https://github.com/anything/repo-name.git
git add .
git commit -m "Changes"
git branch -M main
git push -u origin main

Hope this helps!

Shubham Agrawal
  • 1,252
  • 12
  • 29
2

If you are using git-with-ssh, one reason why it's not working is it maybe pointing to wrong ssh-private-key file or correct-file-wrong-private-key. If I remember correctly, I had recently added the ssh-private-key with some difficulty. So I cleared the sshe-agents by

ssh-add -D

Then everything worked!

Abhisek
  • 4,610
  • 3
  • 17
  • 27
  • An explanation would be in order. E.g., what is the idea/gist? What is it supposed to do? Why is it necessary? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/43190763/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Apr 10 '22 at 12:06
2

Creating a new branch solved it for me:

git checkout -b <nameOfNewBranch>

As expected, there isn’t any need to merge since the previous branch was fully contained in the new one.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Santiago M. Quintero
  • 1,237
  • 15
  • 22
  • 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 – mfaani Jun 24 '19 at 18:37
2

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

git add .
git commit -m "first commit"
git push --set-upstream origin master
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sushil suthar
  • 639
  • 9
  • 12
2

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

git add .

git commit -m "your msg"

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rohitsam
  • 91
  • 8
2

Do these:

git rm --cached *
git add .
git commit -m"upload"
git push --set-upstream origin master
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • An explanation would be in order. E.g., what is the idea/gist? What is it supposed to do? Why are all those steps and command-line options necessary? Why does it work? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/58025637/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Apr 10 '22 at 12:34
2

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

Binod Singh
  • 682
  • 4
  • 11
  • 22
2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike
  • 59
  • 1
  • 4
2

Use

git push -f origin master

This one is write.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

I am 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1889992
  • 355
  • 3
  • 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andreas Bigger
  • 5,264
  • 1
  • 13
  • 18
1

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

It was test/testing_routes, and I needed to change it to testing_route in which the forward slash (/) is not allowed by remote.

You should ensure that the branch name format is correct.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
1

This issue comes when the 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 the latest code from the remote server to local and then push, do

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

    git push --force
    

If #1 will not work, then use the #2 option.

Use the below command to get all the options that are related to push:

git push --help
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sheo Dayal Singh
  • 1,591
  • 19
  • 11
1

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
  • 43,228
  • 68
  • 238
  • 348
1

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

Onat Korucu
  • 992
  • 11
  • 13
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ashvini Maurya
  • 493
  • 1
  • 7
  • 13
1

I tried the 'git push origin main' and then got the message. Then I tried 'git push' alone, but it was not working.

I checked if I had committed to be sure (yes). I tried the 'pull' and then 'push' again, but nope.

And before starting some stunts, I just closed and opened a new terminal and then 'push' again and it worked :p

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jerem
  • 47
  • 4
1

Check if your Internet connection is working fine and has got good speed.

I was trying to push with my 4G mobile hotspot and getting this error for nearly 10 minutes.

P.S. Here in India, we get 3G speed for a 4G network, so before doing something fancy; just see if there is reasonable speed available :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vikramvi
  • 3,312
  • 10
  • 45
  • 68
1

I have also faced this issue when using the command.

git push -u origin main

So I cleared all cache of npm using npm cache clean --force and tried to push again. That's worked for me.

nurmdrafi
  • 419
  • 4
  • 11
1

By following the documentation, you just need to pull and push it to your Github project.

git init -b main

git add . && git commit -m "Commit Here"

git remote add origin  <REMOTE_URL>

git remote -v

git pull origin main

git push origin main

Here are some of my solutions dealing with this kind of problems:

fatal: unable to access <REMOTE_URL>: The requested URL returned error: 400

git remote set-url origin <REMOTE_URL>

fatal: refusing to merge unrelated histories

git pull origin main --allow-unrelated-histories

error: pathspec did not match any file(s) known to git

git fetch origin <branch_name>
KNTY
  • 351
  • 3
  • 11
1

Steps to push :

git init
git add README.md      -------- or --------   git add .
git commit -m "first commit"
git remote add origin 'http github link'
git push -u origin main       ------ or -------    git push -f origin master

If branch is on main, hence:

git push -u origin main

for master :

git push -f origin master
ADITYA AHLAWAT
  • 140
  • 1
  • 13
1

My answer is not for this specific question but may help someone with the same error. If you are doing add . , commit and push on an empty directory, you will probably get the same error. After building the Github repository, run these codes in the local directory. The point is that you need to add/create a file and commit this change before pushing the repository to the remote.

echo "# something" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/[address of your repository].git
git push -u origin main
Mehrdad Salimi
  • 1,400
  • 4
  • 16
  • 31
1

The issue at times may be caused by a system outage on Github. Always check on the GitHub status page https://www.githubstatus.com/ to verify if all systems are operational.

  • This is a duplicate answer. Before posting make sure you check to see if someone else has already provided the same solution. – Nol4635 Apr 01 '23 at 17:52
1

git gc has worked for me.

Extra characters b/c 30 is the minimum.

humanity
  • 1,010
  • 9
  • 14
0

You need to give some force

Just do push --force.

Ankit
  • 4,755
  • 2
  • 22
  • 14
  • That sounds dangerous. Can you explain why that is necessary and what the implications are? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/55469180/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Apr 10 '22 at 12:14
0

For Sourcetree users

First do an initial commit or make sure you don't have any uncommitted changes. Then at the side of Sourcetree there is a "REMOTES". Right-click on it, and then click 'Push to origin'. There you go.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bowie Chang
  • 126
  • 12
0

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
D063520
  • 113
  • 6
0

I was pushing an existing branch with a typo, 'evelop', which I did not have checked out yet, and instead, I wanted to push a branch called 'envelope'.

So the branch must exist and checked out at the local working copy in order to be able to be pushed, of course. Therefore the solution to that error is to not make a typo.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
FantomX1
  • 1,577
  • 2
  • 15
  • 23
0

You will also get this error if you created an empty repo and forgot to use

git init

first before pushing your first commit.

0

is your push linked with your jira story. if yes check the jira work flow condtion. the admin may configure to allow only push only based on some work flow.

0

Try With

git status

It will show like this Your branch is based on 'origin/main', but the upstream is gone.

Then follow below steps

git branch --unset-upstream
git push --set-upstream origin main
Aniket Chauhan
  • 386
  • 2
  • 6
0

For me I just had to checkout into another branch and then comeback, and then it worked, I could push with no issues.

Please try the simple things before using --force and such

sir-haver
  • 3,096
  • 7
  • 41
  • 85
0

In my case, I found out I was pushing the wrong branch name

The branch I'm pushing is release/2.2.1 but my branch name is release/2.2.l so I change it using this command git branch -m old-name new-name. Now it's working.

starball
  • 20,030
  • 7
  • 43
  • 238
Axel Mhar
  • 120
  • 2
  • 9
-1

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 it incorrectly and you want to remove the local repository, use the following steps.

Remove the local repository on 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-3

Getting an error on git push -u origin main? Try this solution. It will work 100%.

git push origin master, change it to git push origin main

How to change Main?

git branch -M main

Rizwan
  • 3,741
  • 2
  • 25
  • 22