Git 在两个独立的系统中初始化同一个项目,并同步更改

问题描述 投票:0回答:1

背景

我有一段时间没有使用 git 进行编程,只是编写代码并希望当我出错时 ctrl-z 能够起作用。
上周,我制作了一台具有冗余功能的小型存储服务器,因为我在一台笔记本电脑上有多年的项目,并且我传输了所有项目文件。
从那时起,我对笔记本电脑中的项目进行了进一步的更改。

我终于决定学习git了,我再次为之前的决定感到非常难过。
我现在想为我的每个项目创建一个存储库,托管在服务器上。

问题

现在我的笔记本电脑中有较新的项目文件,服务器中有较旧的项目文件(两者都没有被 git 初始化),我如何初始化两侧的项目并将我的笔记本电脑上所做的更改推送到服务器上服务器?


我尝试过的事情

在服务器中,我用那里的东西做了一个裸仓库。我这样做是用:

git init
+
git add .
+
git commit -m "created on server"

然后使用如何将普通 Git 存储库转换为裸存储库? 将其设为裸存储库。 不完全确定这些步骤是否足够,但它们似乎有效。

然后在客户端我尝试了相同的 init+add+commit,但后来我也做了

remote add origin git@server:project.git
并尝试过
git push --set-upstream origin master

然而,失败并出现以下错误:

To server:project.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'server:project.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

这似乎表明 git 认为上游的更改比我本地的更改更新。
然后,我尝试使用

git branch -m master thechanges
重命名本地主分支,然后拉取服务器的主分支
git pull origin master
,但失败并显示
fatal: refusing to merge unrelated histories
,我不太确定该怎么办。我计划将这些更改合并到 master 上并推送它,但我不确定我是否正朝着正确的方向前进,所以这就是我们现在的位置。

git version-control
1个回答
0
投票

在客户端,在 init+add+commit 步骤之后,有一种方法可以将该提交放置在“服务器端”提交的顶部

git remote add origin <url>
git fetch

# emphasis on the --soft:
git reset --soft origin/master

git commit

git push origin master
© www.soinside.com 2019 - 2024. All rights reserved.