git error: failed to push some refs to remote
Asked Answered
F

69

1044

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

Fideism answered 9/6, 2014 at 6:21 Comment(12)
looks like your local repo is not in sync with the git repo. did you try to do git pull ?Pompom
yes, but I have no idea with the following syntax after git pull it says git pull <remote> <branch>, can you let me see an example syntax for git pull?Fideism
Do check this similar question - stackoverflow.com/questions/18588974/…Pompom
@Pompom thank you sir! this link helped me https://mcmap.net/q/12628/-git-prevents-pushing-after-amending-a-commitFideism
I got that error on a new repo. This helped: https://mcmap.net/q/13534/-push-origin-master-error-on-new-repositoryBirdhouse
Kindly check if you are in master branch then run git checkout -b main and then git push origin mainGrosmark
hey, if you find this on Google at this time, check if GitHub is down -> githubstatus.comEditor
for me, I had to add and commitSayyid
Here is Step By Step Solution >> stackoverflow.com/questions/24114676/…Alter
Definitely check githubstatus.com ! Start there.Alleviative
Related: Why not upload images of code/errors when asking a question? (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.).Czarevitch
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.Mix
J
1279

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

Jimmy answered 9/6, 2014 at 6:28 Comment(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 sirFideism
For me, I just needed to run "git commit". :(Toddler
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.Fuentes
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.Guiana
@Guiana Thenj it is best to ask a new question, with full details (OS, version of IntelliJ, Git, GitHub Desktop, URL,...)Jimmy
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 @JimmyLaminar
Turns out github had problems.Dejected
@DavidJ Yes: twitter.com/coreyhaines/status/1153335824563539968 and twitter.com/JustinBeckwith/status/1153333169011118080Jimmy
I also needed to do git prune in before pushing.Alicia
Warning, this will delete any existing local files. Before doing it take a copy of your local folder that contains your project files.Lemcke
My whole project crashed, nice oneAlverta
@James666 I am sorry to hear that. To avoid others having the same issue, what part of this answer was problematic in your case?Jimmy
it happens when you don't specify branch name before pushing upto repo, simply use "git branch -M main" & it should workMotherless
@TylerC same here... wonder howd that happen. Ive done it countless times.. perhaps the commit wasnt registered the first time it was entered...Densimeter
B
257

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.

Brandeebranden answered 18/3, 2017 at 13:41 Comment(4)
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)Pedaiah
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.Lamonica
I don't recommend it.Inappreciative
this one worked for me on Nginx Producntion Environment.Trigger
B
151

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.

Battleplane answered 22/5, 2015 at 18:57 Comment(3)
just ran into this. Commit command didn't work during the git add. good call. ThanksFur
Thanks man! That's it. I thought I committed my changes. Now git push -u origin master works fine.Semiotics
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 issuePacheco
C
77

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.

Cyrillic answered 4/1, 2017 at 13:16 Comment(10)
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!Maisonette
Oh I also forget to commit :pJonquil
This is also possible it won't allow you to push with an empty commitShaw
An initial commit fixed it.Converted
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.Brayton
I just had this problem and I forgot to commit. Error message should be more clearTulley
Why people are upvoting this answer :P This answer does not apply to question asked.Armure
It does apply to me since I was getting that exact error message and this solution fixed my problem.Rumormonger
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..Enteric
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.Fluffy
A
66

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.

Atchley answered 5/11, 2021 at 10:54 Comment(3)
This sounds like dangerous advice. All implications and caveats ought to be explained in the answer.Czarevitch
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).Czarevitch
thi s was the right oneCompanion
W
49

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

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

This worked for me.

Willardwillcox answered 1/2, 2018 at 5:47 Comment(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.Levorotation
Thanks. As @Levorotation mentioned, the hyphen was the culprit. Changed it to underscore and my push went through.Macrocosm
I simply renamed my branch to something else (which contained a hyphen) and it also worked. Don't know why though.Masurium
A
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

Adama answered 11/9, 2018 at 4:25 Comment(6)
before pushing the code you need to pull from repositoryAdama
you can simply write like git pull --rebase origin masterAdama
This worked for me, had a refs issue, which this fixed.Sunderance
error: cannot spawn sh: No such file or directory fatal: unable to forkLaughable
Thank you! Step 6 fixed it!Undertaker
git pull --rebase origin master solved my issue. Thanks a lot dear.Negate
L
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
Layfield answered 21/11, 2014 at 6:28 Comment(1)
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.Tattletale
H
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
Holmgren answered 28/1, 2020 at 18:49 Comment(2)
this was the only solution that worked for me.Martyrology
This is the final solution for beginnersSunflower
S
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"
Sayce answered 23/4, 2019 at 9:21 Comment(0)
U
15

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

Undersheriff answered 15/1, 2022 at 11:5 Comment(3)
https://mcmap.net/q/13242/-git-error-failed-to-push-some-refs-to-remoteBortz
Presuming GitHub'ish?Czarevitch
This worked for me. To change master to main I used: $ git branch -m master mainBoesch
P
14

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'
Pamalapamela answered 27/3, 2023 at 13:45 Comment(1)
This was it for meMaxwell
E
9

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

Enucleate answered 18/10, 2022 at 22:3 Comment(1)
this answer is similar to this answer, this answer, and this answerSorosis
P
8

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

Ploy answered 13/12, 2016 at 18:13 Comment(1)
this answer is expanded upon in this answer, this answer, and this answer (all later than this one was posted. moral of story: examples are valuable)Sorosis
R
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
Ronni answered 7/5, 2020 at 13:16 Comment(0)
T
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.

Tannie answered 31/7, 2020 at 13:45 Comment(0)
R
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.

Rucker answered 5/7, 2016 at 9:22 Comment(0)
R
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.

Richards answered 30/10, 2020 at 6:27 Comment(0)
C
5

Not committing initial changes before pushing also causes the problem.

Compute answered 6/5, 2018 at 7:26 Comment(0)
D
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.

Domicile answered 4/4, 2019 at 6:32 Comment(0)
M
5

Try this Git command,

git push origin master –f
git push origin master --force
Mayfly answered 10/12, 2019 at 19:25 Comment(2)
error: cannot spawn sh: No such file or directory fatal: unable to forkLaughable
"–" in "–f" is an em dash, not ASCII 45 ("-"). I don't think it will work.Czarevitch
N
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.

Nesmith answered 3/9, 2020 at 8:54 Comment(0)
L
4

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

Lacy answered 14/12, 2017 at 14:5 Comment(0)
A
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
Architectonics answered 18/10, 2020 at 19:51 Comment(0)
A
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.

Adenoidectomy answered 19/10, 2020 at 10:18 Comment(0)
T
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.

Timi answered 23/9, 2021 at 16:59 Comment(0)
L
4

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!

Libeler answered 20/5, 2022 at 12:36 Comment(0)
G
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.

Glycine answered 3/5, 2019 at 3:34 Comment(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.Chenopod
I
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.

Indecision answered 6/11, 2019 at 9:56 Comment(0)
K
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
Kierkegaardian answered 29/6, 2020 at 19:41 Comment(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)Jimmy
S
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]

Selfexamination answered 26/4, 2021 at 4:45 Comment(1)
Why did you need to rename a local branch?Czarevitch
P
3

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>
Parricide answered 11/7, 2022 at 7:35 Comment(0)
A
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!

Alix answered 3/4, 2017 at 17:25 Comment(1)
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: "...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).Czarevitch
O
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.

Osteomyelitis answered 15/11, 2018 at 21:41 Comment(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 pushKorea
C
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
Commons answered 2/6, 2019 at 11:33 Comment(0)
P
2

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

git add .

git commit -m "your msg"

Precondemn answered 15/8, 2019 at 9:53 Comment(0)
T
2

Do these:

git rm --cached *
git add .
git commit -m"upload"
git push --set-upstream origin master
Tannie answered 20/9, 2019 at 9:31 Comment(1)
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: "...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).Czarevitch
B
2

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

Bose answered 20/11, 2019 at 16:35 Comment(3)
Interesting, it helped me in a case, where there were clearly no commits on origin (no need to rebase).Delectable
Why would you toss out all your hooks? maybe make a backup first?Dissimilate
Deleting all the Git hooks just because they prevent you from committing? Better find the real reason.Czarevitch
L
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.

Laity answered 27/8, 2020 at 10:56 Comment(0)
C
2

Use

git push -f origin master

This one is write.

Cornerstone answered 4/2, 2021 at 9:14 Comment(1)
What do you mean by "This one is write."? Do you mean "This one is right."? Or something else?Czarevitch
S
2

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.

Sclerotic answered 27/3, 2023 at 19:56 Comment(1)
This is a duplicate answer. Before posting make sure you check to see if someone else has already provided the same solution.Oyler
C
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.

Clyve answered 17/3, 2018 at 17:33 Comment(0)
P
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.

Pubilis answered 25/3, 2019 at 2:56 Comment(0)
X
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.

Xiphoid answered 10/10, 2019 at 13:53 Comment(0)
B
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
Buskirk answered 16/5, 2020 at 20:19 Comment(0)
B
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.

Bulletin answered 2/10, 2020 at 18:51 Comment(0)
B
1

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

Baudelaire answered 4/4, 2021 at 12:53 Comment(0)
S
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.

Spritsail answered 25/4, 2021 at 6:50 Comment(0)
D
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

Danziger answered 10/8, 2021 at 16:31 Comment(0)
F
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 :)

Fritzie answered 1/10, 2021 at 12:40 Comment(1)
Still, 3G should be more than sufficient. Perhaps extreme congestion (long latency)? What are the 'ping' times?Czarevitch
C
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.

Cleek answered 17/3, 2022 at 16:6 Comment(3)
How did you "clear the cache of GitHub? What did you do?Czarevitch
Thanks, @PeterMortensen, I have just edited my previous answer.Cleek
Sounds strange, but it worked for me. Thanks!Athiste
R
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
Rambow answered 12/7, 2022 at 11:7 Comment(0)
C
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
Cattail answered 10/9, 2022 at 11:9 Comment(0)
K
1

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.

Kirshbaum answered 27/4, 2023 at 5:34 Comment(0)
O
1

git gc has worked for me.

Extra characters b/c 30 is the minimum.

Obi answered 4/5, 2023 at 17:47 Comment(1)
didn't work for meOldham
O
1

if you are using powershell i will recommend use git bash it is available in vs code

  1. switch to git bash
  2. check ls -a (it will show hidden file)
  3. if you find .git file remove using rm -rf .git
  4. then git init
  5. git add .
  6. git commit -m "message"
  7. git remote add origin "https://.............."
  8. git branch -M main
  9. git push -u origin main
Ornstead answered 15/11, 2023 at 14:57 Comment(0)
M
0

You need to give some force

Just do push --force.

Myrmecophagous answered 2/4, 2019 at 7:30 Comment(1)
That sounds dangerous. Can you explain why that is necessary and what the implications are? 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).Czarevitch
H
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.

Hamman answered 2/5, 2019 at 9:31 Comment(0)
E
0

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

Engedus answered 5/9, 2019 at 7:56 Comment(1)
What was the solution?Czarevitch
S
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.

Salutary answered 10/9, 2019 at 22:39 Comment(0)
S
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.

Subminiaturize answered 29/6, 2021 at 20:14 Comment(0)
F
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.

Fealty answered 20/5, 2022 at 7:34 Comment(0)
D
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
Duval answered 15/6, 2022 at 6:0 Comment(0)
W
0

In my case: a tag was named the same as an existing branch. Renaming the branch (git checkout with an other name) and removing the clashing named branch (git branch -d $branchname) solved the issue: git pushing the tag went ok.

Whitecollar answered 3/11, 2023 at 10:43 Comment(0)
S
-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
Shrewd answered 16/3, 2018 at 2:27 Comment(0)
P
-1

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

Pantoja answered 27/3, 2023 at 13:20 Comment(0)
D
-1

Works in Github

I'm assuming that you're utilizing GitHub for your repo.

A potential, albeit rare, cause of the error you're encountering might be due to protective rules set on the branch to which you are pushing, which you may have inadvertently forgotten about.

If this is the case, to resolve it, it's necessary to completely remove these rules. Merely editing the rule or deselecting options within the rule page is insufficient to address the issue.

Step 1:

Go to the "Setting" of the repo

enter image description here

Step 2:

In "Code and automation" menu find the "Branches" item

enter image description here

step 3:

You should verify whether there's a match between the existing rule patterns and your branch name. If you find a match, delete that rule as demonstrated in the picture above.

Remember to capture a screenshot of the configured rules before you delete them.

step 4:

Since you're removing the protection rule temporarily, it implies you understand your actions and might need to reestablish that rule following your push operation.

Note

To resolve the error, you might consider editing the rule by unchecking the active boxes; however, this approach is ineffective. Only complete delete works!

enter image description here

Defendant answered 13/12, 2023 at 12:1 Comment(0)
V
-1

My case here is that I am trying to upload files that are too large at the same time.

So the problem is solved when they are split and uploaded separately.


Update

Hi @JeremyCaney, thank you for your kind reminder. Please find further elaboration below.

1. Certainty of approach uniqueness: I can confirm that the approach I presented earlier has not been previously provided for this specific question.

2. Distinction of approach: While the uniqueness of my approach may not be the primary focus, it is worth noting that the reason behind the 'git error: failed to push some refs to remote' can vary, and the solution I offered addresses one possible cause.

3. Preferred circumstances for the approach: This approach is particularly effective for handling large files. Although I cannot provide an exact file size threshold, based on my experience, I have encountered difficulties when attempting to upload files of approximately 4GB at once.

4. Insufficiency of previous answers: The previous answers provided effective solutions for most issues, but unfortunately, they did not address the specific problem I encountered.

Vanthe answered 14/1 at 14:22 Comment(2)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Precursor
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewZoosporangium
A
-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

Alter answered 4/1, 2022 at 21:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.