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

12

69 Answers 69

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
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>
2

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

3
  • Interesting, it helped me in a case, where there were clearly no commits on origin (no need to rebase).
    – pevik
    Feb 27, 2020 at 15:51
  • 1
    Why would you toss out all your hooks? maybe make a backup first?
    – MSpreij
    Mar 11, 2020 at 11:58
  • Deleting all the Git hooks just because they prevent you from committing? Better find the real reason. Apr 10, 2022 at 12:42
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.

1
  • 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, 2019 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
2

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

git add .

git commit -m "your msg"

2

Do these:

git rm --cached *
git add .
git commit -m"upload"
git push --set-upstream origin master
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). Apr 10, 2022 at 12:34
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.

2

Use

git push -f origin master

This one is write.

1
  • What do you mean by "This one is write."? Do you mean "This one is right."? Or something else? Apr 10, 2022 at 13:07
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!

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). Apr 10, 2022 at 12:06
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.

1
  • This is a duplicate answer. Before posting make sure you check to see if someone else has already provided the same solution.
    – Nol4635
    Apr 1, 2023 at 17:52
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.

1

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

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.

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.

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.

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

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

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 :)

1
  • Still, 3G should be more than sufficient. Perhaps extreme congestion (long latency)? What are the 'ping' times? Apr 10, 2022 at 13:17
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.

3
  • How did you "clear the cache of GitHub? What did you do? Apr 10, 2022 at 13:31
  • Thanks, @PeterMortensen, I have just edited my previous answer.
    – nurmdrafi
    Apr 10, 2022 at 15:48
  • Sounds strange, but it worked for me. Thanks! May 4, 2023 at 16:02
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
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
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.

0
1

git gc has worked for me.

Extra characters b/c 30 is the minimum.

1
  • didn't work for me
    – PKS
    Nov 29, 2023 at 4:46
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
0

You need to give some force

Just do push --force.

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). Apr 10, 2022 at 12:14
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

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.

0

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

1

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