I wrote a simple bash script which is initialize 2 bare repos (king
and slave
). This script you can copy to terminal and just execute to reproduce issue.
Within king
repo I've added remote branch to slave
repo.
The problem is when I try to push new commit into the slave
repo with cmd git push slave master
some strange push fail occurs... Like I did not fetch new changes from remote and this changes prevents my try to push new commit...
But this is not so...
ygyerts@ygyerts-Aspire-V3-771:~/repos/wd/king$ git push slave master
To /home/ygyerts/repos/bare/slave.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to '/home/ygyerts/repos/bare/slave.git' hint: Updates were rejected because a pushed branch tip is behind its remote hint: counterpart. Check out this branch and integrate the remote changes hint: (e.g. 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
I can push new changes just with -f
flag, which adds this "init commit in king"
from king/origin/master
to history of slave/master
. Which is absolutely unacceptable!
tput reset
#======================================
# Clean workspace
#======================================
rm -rf ~/repos/bare
rm -rf ~/repos/wd
#======================================
# Create required folders
#======================================
mkdir -p ~/repos/bare
mkdir -p ~/repos/wd
#======================================
# Create Bare Repo KING
#======================================
cd ~/repos/bare
git init --bare king.git
#======================================
# Create Bare Repo SLAVE
#======================================
cd ~/repos/bare
git init --bare slave.git
#======================================
# Clone and Initialize KING repository
#======================================
cd ~/repos/wd
git clone ~/repos/bare/king.git
cd ./king
echo "KING" > king.txt
git add . && git commit -m "init commit in king"
git push origin master
#======================================
# Clone and Initialize SLAVE repository
#======================================
cd ~/repos/wd
git clone ~/repos/bare/slave.git
cd ./slave
echo "SLAVE" > slave.txt
git add . && git commit -m "init commit in slave"
git push origin master
#======================================
# Add remotes to KING repo
#======================================
cd ~/repos/wd/king
git remote add slave ~/repos/bare/slave.git
git fetch slave
git branch -a
master
remotes/origin/master
remotes/slave/master
#======================================
# Set upstream branches to within KING repo
#======================================
cd ~/repos/wd/king
git checkout slave/master
git checkout -b slave_master
git branch --set-upstream-to=slave/master slave_master
#======================================
# Add changes to slave/master through the king repo
#======================================
cd ~/repos/wd/king
git branch -vv
master fbdc59a [origin/master] init commit in king
- slave_master 6370a7d [slave/master] init commit in slave
git checkout slave_master
echo "king > slave/master" > king_to_slave.txt
git add . && git commit -m "add changes from king repo"
git push slave master
To /home/ygyerts/repos/bare/slave.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to '/home/ygyerts/repos/bare/slave.git' hint: Updates were rejected because a pushed branch tip is behind its remote hint: counterpart. Check out this branch and integrate the remote changes hint: (e.g. 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
git pull
doesn't help!
答案 0 :(得分:0)
I just found and error in my script. But I didn't understand the problem.
Error was in push cmd git push slave master
Correct cmd is git push slave slave_master:master
But I still not understand why git push -f slave master
cmd takes all commits from origin master
and try put them into history of slave master