gpt4 book ai didi

git - 从本地存储库克隆,然后推回更改

转载 作者:行者123 更新时间:2023-12-02 16:04:20 25 4
gpt4 key购买 nike

我的本​​地系统上有一个 git 存储库,位于 /original/.git 中。现在,我已经在 /cloned_one/.git 中克隆了这个存储库。一切看起来都很好。在这里,我拥有与 /original 中相同的文件,这非常好。我创建一个新文件并提交它。现在,我的克隆 repo 是提前提交的。我想推送更改,但不知道如何操作!

它变得更加棘手,因为我对以下命令的多样性和确切的用例有点困惑。我知道其中的一些,并且实际上以某种方式与他们合作,但我看到很多用户在错误的地方使用它们,这让我感到困惑。不确定何时使用以下命令。

  • git fetch
  • git rebase
  • git push
  • merge
  • git --force push

谢谢。

最佳答案

Git的远程仓库可以来自ssh、https、目录。

在你的例子中,

  • /original/.git 具有指向 github 的远程源(通过 ssh 或 https,例如:https://github.com/user/example.git)
  • /cloned_one/.git 具有指向目录的远程源(例如:/original/.git

这看起来像某种链表:

[/cloned_one/.git] --origin--> [/original/.git] --origin--> [github]

下面是重现此类设置的示例命令:

$ cd /tmp
$ git clone https://github.com/schacon/example.git original
Cloning into 'original'...
remote: Enumerating objects: 4, done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 4
Receiving objects: 100% (4/4), 18.52 KiB | 3.70 MiB/s, done.
$ mkdir cloned_one
$ git clone /tmp/original cloned_one
Cloning into 'cloned_one'...
done.
$ cd cloned_one/
$ echo newfile > newfile.txt
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
(use "git add <file>..." to include in what will be committed)
newfile.txt

nothing added to commit but untracked files present (use "git add" to track)
$ git add newfile.txt
$ git commit -m 'add new file'
[master e45e780] add new file
1 file changed, 1 insertion(+)
create mode 100644 newfile.txt
$ git remote -v
origin /tmp/original (fetch)
origin /tmp/original (push)
$ 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 tree clean

现在,cloned_oneoriginal 提前 1 次提交

您可以将更改从 cloned_one 推送到 original,方法是(记住先 cd/tmp/cloned_one):

  • git push
  • git push origin master
  • git push/tmp/original master

这里push的语法是指定你要push到哪里(比如:到origin,或者到/tmp/original目录),以及你要push到什么分支

$ cd /tmp/cloned_one/
$ 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 tree clean
$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 333 bytes | 333.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 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/original
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/tmp/original'

现在它说你无法推送到/tmp/original,因为它不是一个裸仓库。您可以通过将/tmp/original 更改为裸仓库来解决此问题,或者将其配置为在被推送到如下所示时更新其工作树:

$ cd /tmp/original/
$ git config receive.denyCurrentBranch updateInstead
$ cd /tmp/cloned_one/
$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 333 bytes | 333.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To /tmp/original
c3d5e92..e45e780 master -> master

现在,如果你想将你的更改推送回 github(/tmp/original 的来源),你可以从 /tmp/original/tmp/cloned_one:

  • cd/tmp/original ; git push origin master
  • cd/tmp/cloned_one ; git push https://github.com/username/example.git

请注意,从 /tmp/original 推送时,您可以将目标指定为 origin(因为/tmp/original 的来源来自 github)。当从 /tmp/cloned_one 推送时,您必须将目标指定为完整 URL。

你也可以改变cloned_one的远程指向github(搜索git remote manual)

进一步阅读:

关于git - 从本地存储库克隆,然后推回更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69846209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com