首页 > 解决方案 > 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了以防万一,但它报告我已经在主人身上了。

然后我将test.txt文件更改为$HOME/work/gittest. git status显示它已修改,所以我这样做git commit -a -m "testing"了,这一切似乎都很好。

git status现在$HOME/work/gittest说我领先origin/master1 次提交。再一次,一切都和预期的一样好。

然后我git push origin master在目录中执行$HOME/work/gittest- 它报告事情发生了变化,现在git status说我是最新的origin master.

当我回到. $HOME/gittest_ 但是,如果我这样做,它表明有一个未提交的更改。 但是没有显示出差异。 提交,但仍然没有改变。test.txttest.txt$HOME/work/gittestgit statustest.txtgit diffgit 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

解决方案


看起来您正在尝试推送到非裸存储库。当我重现您的步骤时, 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按照上面的建议在第一个存储库中尝试。


推荐阅读