仓库改名方法(待改进)

目录

PS:全是自己瞎搞出来的。中间很多步骤可改进。
这玩意搞了我最少5个小时。。。
当你发现你能找的所有资料都不行的无奈。。23/12/27
我先按我的步骤写吧,明天再搞一个总结准确方法。

我真的。。不知怀着怎样的心情,在0:21写下这个东西。马上睡觉。🌚🌚🌚

1. 仓库改名

首先,直接仓库改名,不用我说,(就是这玩意开启了我悲惨生涯 )

2. 删旧的remote

一步一步来。

在此之前可以先看看url (肯定得变)

git remote -v
git remote rm origin                   # remove a first remote
git remote -v

# if you see your second origin
git remote rm origin                   # remove the second origin
git remote add origin <repo-url>       # add new origin
git remote -v                          # see all the remotes you have  

3.git.git/..下修改

到当前上传文件.git 目录下进行操作。

3.1 先打开config文件,加入以下代码 (一般是origin分支,不是的话另算,若有类似代码可忽略本步骤)
[branch "main"]
	remote = origin
	merge = refs/heads/main

当看到config下有类似代码即为成功;

[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = git@gitea.bestrui.top:yu/YU_THREAD.git
	fetch = +refs/heads/*:refs/remotes/origin/*

[branch "main"]
	remote = origin
	merge = refs/heads/main

git remote -v一下,出现类似

root@Cofallen:~/code/thread/.git# git remote -v
origin  git@gitea.bestrui.top:yu/YU_THREAD.git (fetch)
origin  git@gitea.bestrui.top:yu/YU_THREAD.git (push)

注意! 这里冒号后的要是远程仓库改名后的名称,其实这一步是替换成你的地址。

3.2 输入git push报错

这是分析我出现的问题,2,3步骤没有解决问题,若不感兴趣可跳到第4步骤。

在此之前你要输入一个代码,pull回来。

git pull origin <your_branch_name>

注意: 这里分支名为origin/main,填main就行,要是改了就填你的分支名。

肯定有报错,具体看第4步骤。

跳到第4步骤

要想再看看我的历程,报错经历以及处理办法,可以继续看看。

root@Cofallen:~/code/thread/.git# git push
To gitea.bestrui.top:yu/YU_THREAD.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'gitea.bestrui.top:yu/YU_THREAD.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

分析这个问题

你遇到的问题是"Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes (e.g.‘git pull …’) before pushing again." 这个错误通常发生在你试图将本地的提交推送到远程仓库时,而此时远程仓库已经包含了你尚未合并的更新。

为了解决这个问题,你需要执行以下步骤:

  1. 确保你在正确的本地分支上。你可以使用 git branch 命令来查看当前所在的分支。

  2. 运行 git pull 命令来将远程仓库的更改合并到你的本地分支上:

    git pull origin <your_branch_name>
    
  3. 如果在执行 git pull 命令时遇到了冲突,需要解决这些冲突。Git 会提示你手动解决冲突,然后你需要运行 git add 将解决后的文件标记为已解决。

  4. 最后,再次尝试将你的更改推送到远程仓库:

    git push origin <your_branch_name>
    

实际还有问题。。

3.3 报错 this operation must be run in a work tree

输入git checkout main会报错

this operation must be run in a work tree

分析这个问题。

你收到的错误消息 “this operation must be run in a work tree” 意味着你正在尝试在不正确的位置执行 Git 命令。这通常是因为你可能没有在正确的 Git 仓库目录中执行命令。

要解决这个问题,你需要确保您在正确的 Git 仓库目录中执行命令。以下是一些可能的解决方法:

  1. 切换到正确的目录:首先,确认您当前的工作目录是否是 Git 仓库的根目录。您可以使用 ls 命令查看当前目录中是否包含 .git 文件夹,这是 Git 仓库的标志。如果不在正确的目录中,请使用 cd 命令切换到包含 .git 文件夹的目录。

  2. 重新初始化 Git 仓库:如果您确定当前目录应该是一个 Git 仓库,但仍然遇到问题,您可以尝试重新初始化 Git 仓库。在正确的目录中执行以下命令:

git init

这将重新初始化 Git 仓库并允许您在该目录下执行 Git 命令。

  1. 确认远程仓库:如果您正在尝试执行与远程仓库相关的操作,确保您已经将远程仓库正确地添加到本地仓库中。您可以使用 git remote -v 命令来查看当前配置的远程仓库。

一旦您确认了正确的工作目录并且已经初始化了 Git 仓库,您应该能够执行 Git 命令而不再收到 “this operation must be run in a work tree” 的错误消息。

所以大体方案就是回到上一总目录,初始化仓库

4.总体解决方案

在第2步骤下,你输入 git pull origin <your_branch_name>出现refusing to merge unrelated histories问题~~(实际上报错很多hint,我就写关键吧)~~

分析这个问题。

这个错误通常发生在你尝试合并两个不相关的 Git 历史时。这通常发生在你尝试将两个不同的仓库或分支进行合并,而 Git 认为它们的历史是不相关的。

为了解决这个问题,你可以在执行 git pull 命令时加上 --allow-unrelated-histories 参数,这样可以允许合并不相关的历史。

尝试使用以下命令来解决这个问题:

git pull origin <your_branch_name> --allow-unrelated-histories

执行这个命令后,Git 将允许合并不相关的历史,这样你就可以继续进行后续的操作了。

请确保在执行这些命令之前,你已经切换到了正确的本地分支。
(其实就是上传文件的目录下,不是.git哦)

下附我成功代码

root@Cofallen:~/code/thread# git pull origin main --allow-unrelate
d-histories
hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint: 
hint:   git config pull.rebase false  # merge (the default strategy)
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.

From gitea.bestrui.top:yu/YU_THREAD
 * branch            main       -> FETCH_HEAD
Merge made by the 'recursive' strategy.

大功告成!!!

去尝试git add .,git commit -m "",git push吧。

点击跳转到目录

5.感想及附录

星期日晚上看到RUI的仓库名字,就手贱改了改我的仓库名。
yu/Thread -> yu/YU_THREAD ,改成这个之后发现怎么也交不上,还有工图考试,当晚肝到1点,星期一干了几个小时,晚上肝到12点。星期二复习工图,晚上干到2点,终于完成。。

算是一个不错的回忆。。🌚🌚🌚

下面是我部分终端内容,因网不好丢了不少数据。23/12/27/10:32

突然断网,先保存明天写。。丢了不少。。 23/12/27/01:40

root@Cofallen:~/code/thread# git pull origin main --allow-unrelate
d-histories
hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint: 
hint:   git config pull.rebase false  # merge (the default strategy)
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.

From gitea.bestrui.top:yu/YU_THREAD
 * branch            main       -> FETCH_HEAD
Merge made by the 'recursive' strategy.
root@Cofallen:~/code/thread# git pull origin main --allow-unrelated-histories
hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint: 
hint:   git config pull.rebase false  # merge (the default strategy)
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
From gitea.bestrui.top:yu/YU_THREAD
 * branch            main       -> FETCH_HEAD
Already up to date.
root@Cofallen:~/code/thread# git push
Enumerating objects: 2, done.
Counting objects: 100% (2/2), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 339 bytes | 339.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: . Processing 1 references
remote: Processed 1 references in total
To gitea.bestrui.top:yu/YU_THREAD.git
   2ef6ccb..40b1594  main -> main

点击跳转到目录

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐