git - Git 没有在推送时更新远程存储库 - 我知道我做错了,但不知道是什么
问题描述
我确定我在做一些非常基本的错误,但是我在这里搜索了其他答案,但仍然无法正确。我的操作系统是 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
了以防万一,但它报告我已经在主人身上了。
然后我将test.txt
文件更改为$HOME/work/gittest
. git status
显示它已修改,所以我这样做git commit -a -m "testing"
了,这一切似乎都很好。
git status
现在$HOME/work/gittest
说我领先origin/master
1 次提交。再一次,一切都和预期的一样好。
然后我git push origin master
在目录中执行$HOME/work/gittest
- 它报告事情发生了变化,现在git status
说我是最新的origin master
.
当我回到. $HOME/gittest
_ 但是,如果我这样做,它表明有一个未提交的更改。 但是没有显示出差异。 提交,但仍然没有改变。test.txt
test.txt
$HOME/work/gittest
git status
test.txt
git diff
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
按照上面的建议在第一个存储库中尝试。
推荐阅读
- python - 死锁主线程而不是忙着等待更好吗?
- c# - 使用面板的选项卡系统
- java - AWS Java SDK v2 应为 Route 53 使用哪个区域终端节点?
- java - 如何在 Intellij 中将 SQLJDBC 正确导出到 JAR?
- r - 复制 is.element
- python - DLL 加载失败:找不到指定的模块 Windows 10 shell
- laravel - 如何使用 Laravel 和 JQuery Masonry 更改 Bootstrap 网格中的列数
- c++ - 如何让系统在十六进制数字之前打印 0x 和在八进制数字之前打印 0?
- javascript - Recorder.js 在本地主机上工作正常,但在服务器上不工作
- ios - 在情节提要中使用材质样式时,iOS 13.0 警告之前的系统模糊样式