可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I\'m new to GitHub. Today I met some issue when I was trying to push my code to GitHub.
Pushing to git@github.com:519ebayproject/519ebayproject.git
To git@github.com:519ebayproject/519ebayproject.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to \'git@github.com:519ebayproject/519ebayproject.git\'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. \'git pull\')
hint: before pushing again.
hint: See the \'Note about fast-forwards\' in \'git push --help\' for details.
I have not pushed anything in the repository yet, so why do I need to pull something?
回答1:
This can cause the remote repository to lose commits; use it with care.
If you do not wish to merge the remote branch into your local branch (see differences with git diff), and want to do a force push, use the push command with -f
git push -f origin <branch>
where origin
is the name of your remote repo.
Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. This flag disables the check. This can cause the remote repository to lose commits; use it with care.
回答2:
As the message tells you,
Merge the remote changes (e.g. \'git pull\')
Use git pull
to pull the latest changes from the remote repository to your local repository. In this case, pulling changes will require a merge because you have made changes to your local repository.
I\'ll provide an example and a picture to explain. Let\'s assume your last pull from origin/branch was at Commit B. You have completed and committed some work (Commit C). At the same time, someone else has completed their work and pushed it to origin/branch (Commit D). There will need to be a merge between these two branches.
local branch: --- Commit C
/
/
/
origin/branch: Commit A ------ Commit B ---- Commit D
Because you are the one that wants to push, Git forces you to perform the merge. To do so, you must first pull the changes from origin/branch.
local branch: --- Commit C -- Commit E
/ /
/ /
/ /
origin/branch: Commit A ------ Commit B ---- Commit D
After completing the merge, you will now be allowed to fast-forward origin/branch to Commit E by pushing your changes.
Git requires that you handle merges yourself, because a merge may lead to conflicts.
回答3:
Have you updated your code before pushing?
Use git pull origin master
before you push anything.
I assume that you are using origin
as a name for your remote.
You need to pull before push, to make your local repository up-to-date before you push something (just in case someone else has already updated code on github.com
). This helps in resolving conflicts locally.
回答4:
This normally happens when you git commit
and try to git push
changes before git pulling
on that branch x
where someone else has already made changes.
The normal flow would be as below,
STEP 1: git stash
your local uncommitted changes on that branch.
STEP 2: git pull origin branch_name -v
to pull and merge
to locally committed changes on that branch (give this merge some message, and fix conflicts if any.)
STEP 3: git stash pop
the stash
ed changes (Then you can make commits on popped files if you want or push already committed changes (STEP4) first and make new commit to files later.)
STEP 4: git push origin branch_name -v
the merged changes.
Replace branch_name
with master
(for master
branch).
回答5:
Sometimes we forgot the pulling and did lots of works in the local environment.
If someone want to push without pull,
git push --force
is working. This is not recommended when working with other people, but when your work is a simple thing or a personal toy project, it will be a quick solution.
回答6:
Some of you may be getting this error because Git doesn\'t know which branch you\'re trying to push.
If your error message also includes
error: failed to push some refs to \'git@github.com:jkubicek/my_proj.git\'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. If you did not intend to push that branch, you may want to
hint: specify branches to push or set the \'push.default\' configuration
hint: variable to \'current\' or \'upstream\' to push only the current branch.
then you may want to follow the handy tips from Jim Kubicek, Configure Git to Only Push Current Branch, to set the default branch to current.
git config --global push.default current
回答7:
git pull origin branch_name --rebase
This worked for me -- the command git pull origin branch_name --rebase
will pull changes from remote branch_name at first, then rebase
current branch on the top of it.
回答8:
I had the same problem , what I did was I first pushed it by force by using this
git push --force
I did this after I commited the files and was getting an error as you got.It did commit all the files and it pushed them.
Then the next time I was pushing to the github .I did what it asked me to and it was alright then. Hope this works for you too :)
回答9:
First and simple solution
- Try this command
git push -f origin master
.
- This command will forcefully do all the changes needed to your repository.
Recommended solution
OR
git push -f origin master
Only use git push -f origin master
if -u
dont work for you.
This will solve almost any kind of errors occurring while pushing your files.
回答10:
In addition to the answers above, the following worked for me : -
Scenario -
- I pushed my_branch to origin successfully.
- I made few more changes.
- When I tried to push again, (after doing add, commit of course), I got the above mentioned error.
Solution -
1. git checkout **my_branch**
2. git add, commit your changes.
3. git pull origin **my_branch** (not origin, master, or develop)
4. git push origin **my_branch**
![\"Proof\"](\"https://i.stack.imgur.com/H8vNX.png\")
回答11:
I mentioned this in my tutorial, How To Use GitHub: A tutorial for beginners.
When you create a new repository on GitHub, GitHub may ask you to create a readme file. If you create a readme file directly on GitHub, then you will need to first make a ‘pull’ request before the ‘push’ request will be successful.
These commands will ‘pull’ the remote repository, merge it with your current files, and then ‘push’ all the files back to GitHub:
git pull https://github.com/thomas07vt/MyFirstRepo.git master
git push https://github.com/thomas07vt/MyFirstRepo.git master
回答12:
git push -f origin branchname
Use the above command only if you are sure that you don\'t need remote branch code otherwise do merge first and then push the code
回答13:
I was getting the above mentioned error message when I tried to push my current branch foobar
:
git checkout foobar
git push origin foo
It turns out I had two local branches tracking the same remote branch:
foo -> origin/foo (some old branch)
foobar -> origin/foo (my current working branch)
It worked for me to push my current branch by using:
git push origin foobar:foo
... and to cleanup with git branch -d
回答14:
If you don\'t want to pull into your current project (and potentially face merge conflicts you don\'t care to solve) and you don\'t want to create another branch (This will be tedious to manage another branch), and you don\'t want to do any risky and permanent git force
commands (which even after reading what they do I\'m often surprised about the implications of doing it).
Solution: You could simply, drag your folder\'s contents into another folder, pull the project into your now empty folder, drag the pulled contents to the trash, then drag your correct project back into the folder. You should be able to push properly and get the desired results. It literally takes me less than 10 seconds to do this.
To the people that would tell me this is not proper without citing any consequences, or people telling me to use a command that causes me future annoyances, I say: \"This method literally takes me less than 10 seconds.\" If I encounter a git command that takes less than 10 seconds to implement and has the exact same effect, I will adopt that. Until then, I\'m using this method.
One downside of this method is that your commit history will appear linear when you actually merged in a branch without the merge being documented. This might not be the best method when working with groups. Work on branches in those cases!
回答15:
Just had the same issue but in my case I had typed the wrong branch on the remote. So, it seems that is another source of this issue... double check you\'re pushing to the correct branch.
回答16:
I had a similar issue and it turned out that my workflow for keeping my branch up to date was at fault. I was doing the following:
In my local \'master\'
git fetch upstream
git merge upstream/master --ff-only
then back in my local branch
git rebase master
This worked well for a previous git flow but not with github. The git rebase
was the problem here causing issues with syncing (and I\'ll admit that\'s something I\'ve had to accept without fully understanding) and unfortunately put me in a position where git push -f
became probably the easiest option. Not good.
My new flow is to update the branch directly using git merge
as follows:
In my local branch
git fetch upstream
git merge upstream/master
No fast forward, as I will have made changes of course in the local branch.
As you can probably tell, I\'m no git expert but I\'m reliably informed that this workflow will probably avoid the specific problems that I had.
回答17:
In my case, I had \"mybranch\" checked out, and had done git pull
, so I couldn\'t figure out why the push wasn\'t working. Eventually, I realized that I was pushing the wrong branch. I was typing git push origin master
instead of git push origin mybranch
.
So if you\'ve already done git pull
and still getting this message, make sure you\'re pushing the correct branch.
回答18:
I experienced the very same problem and it turned out I was on a different (local) branch than I thought I was AND the correct local branch was behind in commits from remote.
My solution: checkout the correct branch, cherry-pick the commit from the other local branch, git pull and git push
回答19:
Is your branch name the same as the remote branch name?
If no, you should checkout a new branch with the same name as the remote branch and try push it again.
Assume the remote branch you want to push is [testing] and your local branch is named as [test].
If you`re not in test branch, first switch to it.
git checkout test
Then open a new branch and name it testing.
git checkout -b testing
Now, it`s time to push it:
git push [remote repo] testing
回答20:
I have resolve this issue at my GIT repository. No need to rebase
or force
commit in this case. Use below steps to resolve this -
local_barnch> git branch --set-upstream to=origin/<local_branch_name>
local_barnch>git pull origin <local_branch_name>
local_barnch> git branch --set-upstream to=origin/master
local_barnch>git push origin <local_branch_name>
hope it will help.
回答21:
Another solution is to advance the head of the remote by making another commit if you can. After you pull this advanced head into the local subtree then you will be able to push from it again.
回答22:
I was getting a similar error while pushing the latest changes to a bare Git repository which I use for gitweb. In my case I didn\'t make any changes in the bare repository, so I simply deleted my bare repository and cloned again:
git clone --bare <source repo path> <target bare repo path>
回答23:
If you are certain that no one made changes to your git repository and that you are working on the latest version, git pull
doesn\'t make sense as a solution in your heart...
Then this is probably what happened, you used git commit --amend
It lets you combine staged changes with the previous commit instead of committing it as an entirely new snapshot. It can also be used to simply edit the previous commit message without changing its snapshot.
ATLASSIAN tutorial: rewriting history
However, it is not recommended to perform git commit --amend
if you have already pushed the commit to GitHub, this is because \"amending doesn’t just alter the most recent commit—it replaces it entirely. To Git, it will look like a brand new commit\" which means to other developer on your GitHub, history looks like A->B->C but to you it looks like A->B->D, if GitHub let you push
, everyone else will have to manually fix their history
This is the reason why you get the error message ! [rejected] master -> master (non-fast-forward)
, if you know that no one has pulled your latest change, you can do git push --force
, this will alter the git history in your public repo. Otherwise...you can perform git pull
, but I believe this will have the same result as you didn\'t go through git commit --amend
,it will create a new commit (ie: git history after git pull: A->B->C->D)
for more detail: How to change your latest commit
回答24:
Another option: locally rename your branch to something new.
You will then be able to push it to the remote repository, for example if that is your way of keeping a copy (backup) and making sure nothing gets lost.
You can fetch the remote branch to have a local copy and examine the differences between (i) what the remote had (with the old branch name) and (ii) what you have (with the new branch name), and decide what to do. Since you weren\'t aware of the remote\'s differences in the first place (hence the problem), simply merging or forcing changes somewhere is far too brutal.
Look at the differences, pick which branch you want to work on, cherry pick changes you want from the other branch, or revert changes you don\'t want on the branch you\'ve got etc.
Then you should be in a position to decide whether you want to force your clean version onto the remote, or add new changes, or whatever.
回答25:
The problem with push command is that you your local and remote repository doesn\'t match. IF you initialize readme by default when creating new repository from git hub, then, master branch is automatically created. However, when you try to push that has no any branch. you cannot push... So, the best practice is to create repo without default readme initialization.
回答26:
Another cause of this problem (apparently not so common)...
My server was behind ~12 hours when I did a push
I configured NTP on the server SYNC my clock.
I executed a new git push which led the error discussed in this post.
回答27:
If by any chance git pull
prints Already up-to-date
then you might want to check the global git push.default
param (In ~/.gitconfig
). Set it to simple
if it was in matching
. The below answer explains why:
Git - What is the difference between push.default "matching" and "simple"
Also, it is worth checking if your local branch is out of date using git remote show origin
and do a pull if needed
回答28:
use git pull https://github.com/username/repository
It\'s because the Github and remote repositories aren\'t in sync. If you pull
the repo and then Push
everything will be in sync and error will go away.
`
回答29:
git pull
prints Already up-to-date
solution:
you might be created a repository/project in remote(server) and added some file there, Then again created a Folder in your local and initialised git git init
- this is the mistake, you should not create git init
in local, instead clone the project to your local using git clone
then pull
回答30:
Provided that nobody else is working with a branch, my guess is that you did the following:
1. $git commit #you committed your changes.
2. $git push origin <branch> #you pushed your commit to remote origin (yeah, success!)
3. $git commit --amend #you did some changes locally and committed with --amend
4. $git push origin <branch> #you pushed the \"old\", just amended commit to the remote origin and you thought \"WTF?\".
The step 4. is the cause of your problem, imho...