For some reason, I can't push now, whereas I could do it yesterday. Maybe I messed up with configs or something.
This is what happens:
When I use the git push origin master
What my working directory and remote repository looks like:
57 Answers
12 Next(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 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 pushThe full syntax is:
git pull --rebase origin main
git push origin mainWith Git 2.6+ (Sept. 2015), after having done (once)
git config --global pull.rebase true
git config --global rebase.autoStash trueA 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 mainThat 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 pushSee "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 mainThere 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.
Try:
git push -f origin masterThat 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.
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.
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.
Rename your branch and then push, e.g.:
git branch -m new-name
git push -u new-nameThis worked for me.
3It 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 --forceBe careful. If they have a Readme file, the git reset deletes them.
git initgit remote add origingit remote -v(for checking current repository)git add -A(add all files)git commit -m 'Added my project'git pull --rebase origin mastergit push origin master
I found the solution to this problem in GitHub help (Dealing with non-fast-forward errors):
1You 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 workOr, 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
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 I had faced the same problem and fixed it with the below steps.
git initgit add .git commit -m 'Add your commit message'git remote add origin(The above URL, , refers to your Bitbucket project URL)
git push -u origin master
Hint
Check if your GitHub account links with your local Git repository by using:
git config --global user.email ""
git config --global user.name "Your Name" If you were using git push origin master, change it to git push origin main and vice versa.
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
git add .
git push -u origin masterThe 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
git push -u origin masterSince I executed the wrong sequence first, I just executed the below commands:
git add .
git commit -m 'Initial Commit'
git push -u origin master Because maybe it has nothing to push (really, nothing to push). Do it like this:
git remote add origin
git remote -v
git add .
git commit -m"upload"
git push --set-upstream origin masterChange the remote repository's URL in your case. You can skip command git remote -v, just for checking.
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.
Remember to commit your changes before pushing to the GitHub repository. This might fix your problem.
Not committing initial changes before pushing also causes the problem.
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.
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 mainThis is a common mistake beginners can make.
GitHub article Renaming the default branch from master.
Before push, you have to add and commit the changes or do git push -f origin master.
Try this Git command,
git push origin master –f
git push origin master --force 2 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.
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 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.
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.
These steps worked for me:
Switch to current branch & pull latest code
Rename local branch
git branch -m [new-name]Push local branch to server
git push origin [new-name]Remove branch from server
git push origin --delete [old-name]
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 '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.0Ran npm install and commit it, and the problem is fixed.
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 Best use rm -rf .git/hooks and then try git push
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.
1It 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
12 Next