我确信我做错了一些非常基本的事情,但我在这里搜索了其他答案,仍然无法得到正确的答案。我的操作系统是Ubuntu 18.04 LTS。我无法从克隆中获得git push
来更新原始存储库中已更改的文件。
我已经设置了一个非常基本的GIT配置供我学习。在我的主目录中,我创建了一个目录gittest
,然后在该目录中创建一个简单的test.txt
文件,执行git init
,然后执行git add
和git commit
。到目前为止还不错。
然后我创建一个目录$HOME/work
,然后在该目录中创建一个git pull $HOME/gittest
。同样,一切都很好,创建了gittest
目录,test.txt
文件也在那里。
在$HOME/work/gittest
目录中,我做了git checkout master
以备不时之需,但它报告我已经在主机上了。
然后我更改$HOME/work/gittest
中的test.txt
文件。git status
显示它被修改了,所以我做了git commit -a -m "testing"
,这似乎都没问题
$HOME/work/gittest
中的git status
现在表示我领先origin/master
1个commit。同样,一切都很好。
然后,我在目录$HOME/work/gittest
中执行git push origin master
——它报告发生了变化,现在git status
说我是最新的origin master
。
当我返回到$HOME/gittest
时,test.txt
尚未更改为与$HOME/work/gittest
中的test.txt
文件匹配。但是,如果我执行git status
,则表明test.txt
有一个未提交的更改。CCD_ 30无明显差异。git commit -a -m "testing 2"
执行提交,但test.txt
仍然没有更改。
我整天都在努力让这个简单的过程发挥作用,任何帮助都将不胜感激。
Ubuntu1804:~$ mkdir gittest
Ubuntu1804:~$ cd gittest
Ubuntu1804:~/gittest$ pico test.txt
Ubuntu1804:~/gittest$ cat test.txt
testing git
Ubuntu1804:~/gittest$ git init
Initialized empty Git repository in /home/marksires/gittest/.git/
Ubuntu1804:~/gittest$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
Ubuntu1804:~/gittest$ git add test.txt
Ubuntu1804:~/gittest$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test.txt
Ubuntu1804:~/gittest$ git commit -a -m "test 1"
[master (root-commit) dfe13e2] test 1
1 file changed, 2 insertions(+)
create mode 100644 test.txt
Ubuntu1804:~/gittest$ git status
On branch master
nothing to commit, working directory clean
Ubuntu1804:~/gittest$ cd ..
Ubuntu1804:~$ mkdir work
Ubuntu1804:~$ cd work
Ubuntu1804:~/work$ git clone $HOME/gittest
Cloning into 'gittest'...
done.
Ubuntu1804:~/work$ ls
gittest
Ubuntu1804:~/work$ ls gittest
test.txt
Ubuntu1804:~/work$ cd gittest
Ubuntu1804:~/work/gittest$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
Ubuntu1804:~/work/gittest$ git checkout master
Already on 'master'
Your branch is up-to-date with 'origin/master'.
Ubuntu1804:~/work/gittest$ pico test.txt
Ubuntu1804::~/work/gittest$ cat test.txt
testing git
line 2 for testing
Ubuntu1804:~/work/gittest$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
Ubuntu1804:~/work/gittest$ git commit -a -m "testing 2"
[master 51ddc40] testing 2
1 file changed, 2 insertions(+)
Ubuntu1804:~/work/gittest$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
Ubuntu1804:~/work/gittest$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 277 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /home/marksires/gittest
dfe13e2..51ddc40 master -> master
Ubuntu1804:~/work/gittest$ cd $HOME/gittest
Ubuntu1804:~/gittest$ ls
test.txt
Ubuntu1804:~/gittest$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: test.txt
Ubuntu1804:~/gittest$ git diff test.txt
Ubuntu1804:~/gittest$ cat test.txt
testing git
Ubuntu1804:~/gittest$ cat $HOME/work/gittest/test.txt
testing git
line 2 for testing
看起来您正试图推送到非裸存储库。当我重现你的步骤时,git-push命令被拒绝:
$ git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 254 bytes | 254.00 KiB/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 the 'receive.denyCurrentBranch' configuration variable
remote: to '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 /tmp/gittest
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/tmp/gittest'
在您的情况下,您可以在第一个存储库中尝试git reset --hard
,如上所述。