Git 在推送时不更新远程存储库 - 我知道我做错了,但无法弄清楚是什么



我确信我做错了一些非常基本的事情,但我在这里搜索了其他答案,仍然无法得到正确的答案。我的操作系统是Ubuntu 18.04 LTS。我无法从克隆中获得git push来更新原始存储库中已更改的文件。

我已经设置了一个非常基本的GIT配置供我学习。在我的主目录中,我创建了一个目录gittest,然后在该目录中创建一个简单的test.txt文件,执行git init,然后执行git addgit 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/master1个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,如上所述。

我找到了答案。正如我所怀疑的,这只是一个不理解git的"新手"问题。"推送"文件不会在"主"存储库中被推送修改,因此直接在$HOME/gittest中查看它们仍然显示原始文件。如果我通过将$HOME/gittest克隆到$HOME/work2/gittest来创建另一个远程目录,那么从$HOME/gittest中提取的文件将反映从$HOME/wwork/gittest推送到$HOME/gittest的更改。我相信有一种方法可以直接在$HOME/gittest中查看文件的"当前"版本,因为gitlab和github可以做到这一点。我还没有找到它,对于我目前的练习来说,它是不需要的。知道一个pull将始终反映出所有来自远程的push的当前状态就足够了。感谢所有其他的答案,其中也有一些好的信息!

最新更新