Your origin master
has to be either a bare-repository or the branch may not be checked out.
If it is not a bare-repository and the branch which will be pushed is checked out, you will get the following error:
git push origin master --force
Counting objects: 3, done.
Writing objects: 100% (3/3), 214 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set 'receive.denyCurrentBranch' configuration variable to
remote: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To /Users/Roman/gittest/master
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/Users/Roman/gittest/master'
Solution
If you do not want to use a bare-repository, you have to checkout a branch that differs between your bkpfolder
and your master
-directory.
Out of the box example
For better undestanding i renamed the remote master
to origin
.
mkdir main_backup
cd main_backup && git init --bare && cd ..
git clone main_backup bkpfolder
cd bkpfolder && echo "This is a test" >> test.txt
git add .
git commit -m 'Automatic backup'
git push origin master