Is there a good way to explain how to resolve \"! [rejected] master -> master (fetch first)\'
\" in Git?
When I use this command $ git push or
It's likely that someone else (e.g. your colleague) has put commits onto origin/master
that aren't in your local master
branch, and you are trying to push some commits from your local branch to the server. In 99% of cases, assuming you don't want to erase their work from origin
, you have two options:
2) Merge their changes into your local branch, and then push the merged result.
git checkout master
git pull # resolve conflicts here
git push
(Note that git pull
is essentially just a git fetch
and a git merge
in this case.)
1) Rebase your local branch, so that it looks like your colleague made their commits first, and then you made your commits. This keeps the commit history nice and linear - and avoids a "merge commit". However, if you have conflicts with your colleague's changes, you may have to resolve those conflicts for each of your commits (rather than just once) in the worst case. Essentially this is nicer for everyone else but more effort for you.
git pull --rebase # resolve conflicts here
git push
(Note that git pull --rebase
is essentially a git fetch
and a git rebase origin/master
.)
Problem Solved
Problem I had
! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/repo_name/repo-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.
I also had the same problem. The problem is that your commits
in other repositories were not successfully pushed
so you need to run the following commands:
git fetch origin master
output: From https://github.com/username/repo-name
* branch master -> FETCH_HEAD
git merge origin master
output: Merge made by the 'recursive' strategy.
repo-name/ReadMe.md | 1 -
1 file changed, 1 deletion(-)
git push
output: Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 1.00 KiB | 1.00 MiB/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To https://github.com/user_name/repo-name.git
0852d5vf..70v56d9 master -> master
Thank You so much
First, You should use git pull
, then command do a git fetch
and next do the git merge.
If you use a git push origin master --force
command, you may have problems in the future.
I overcame this by checking-out a new branch like this :
# git checkout -b newbranch <SHA of master>
# git branch
* newbranch
master
# git push -u <repo_url_alias> newbranch
You are left with 2 branch : Master and newbranch , that you can manage to merge later.
try:
git fetch origin master
git merge origin master
After to wrote this code I received other error: (non-fast-forward)
I write this code:
git fetch origin master:tmp
git rebase tmp
git push origin HEAD:master
git branch -D tmp
And resolved my problem
You should use git pull
, that´s command do a git fetch
and next do the git merge
.
If you use a git push origin master --force
command, you may have problems in the future.