git: always need pull before pushing code, even I'm the only one working on this repository

huangapple go评论65阅读模式
英文:

git: always need pull before pushing code, even I'm the only one working on this repository

问题

作为一个Git的新手,我最近开始使用它已经有几天了,但我遇到了一些问题,尽管我已经阅读了许多在线资源,但仍然很难理解。

为了给你一些背景,我在Rstudio中设置了一个Git仓库,并将其连接到GitHub上的一个远程仓库。我将本地仓库保存在OneDrive上。
截至目前,我是唯一在这个仓库上工作的人,我的主要目标是使用Git记录我的工作,并确保我始终可以访问我的代码的最新版本。

我的问题是,每当我尝试推送代码时,我总是遇到不同的分支和拒绝消息。

尽管我已经同步了本地和远程仓库(即,我可以成功运行pull和push),当我编辑一个文件并将其拉到我的本地仓库,然后推送到远程仓库时,我仍然遇到不同的分支和拒绝消息。

在这一点上,如果我先拉取,拉取是成功的,我可以在拉取后正常进行。
然而,在此之后,任何新的更改仍然需要我先拉取才能推送。
只有我使用这个仓库,所以我不知道为什么本地和远程仓库之间会有冲突。

我担心的是,如果我从远程仓库拉取代码,它可能会覆盖我的本地代码,导致我丢失已经做过的更改。
这让我不敢拉取代码。然而,如果我不拉取代码,我也无法推送我的更改。

问题1

这个冲突不是由本地和远程代码之间的差异引起的。如果本地和远程代码之间有差异,将会出现合并提示。
我的情况与此不同,我不明白为什么会发生这种情况。

问题2

我读过一些文章,建议如果我提交本地更改,拉取远程代码不会覆盖本地代码。
这是真的吗?

问题3

此外,如果本地和远程代码之间存在差异,将出现合并提示。
当冲突发生时,我不想从本地或远程仓库中删除任何代码。
我想保留两者并选择本地代码作为最终版本。

<<<<<<<======= >>>>>>>提示出现时,我可以删除或修改提示内的内容,然后使用"git add ."保存本地和远程代码的更改,然后将它们上传到GitHub。
由于我是唯一使用Git的人,并且我使用OneDrive在我的工作和个人计算机之间同步了本地仓库,我不担心覆盖远程代码。
但有时我可能会直接在GitHub上进行更改,因此我需要确保冲突发生时保留本地和远程代码。

以下是我git中显示的树形结构图。
我不明白为什么它这么复杂。

git: always need pull before pushing code, even I'm the only one working on this repository

英文:

As a newcomer to Git, I've recently started using it for a few days now, but I've encountered some issues that I'm struggling to understand despite reading numerous online resources.

To give you a bit of context, I've set up a Git repository in Rstudio and connected it to a remote repository on GitHub. I've kept the local repository on my OneDrive.
As of now, I'm the only one working on this repository, and my primary goal is to use Git to keep a record of my work and ensure that I always have access to the latest version of my code.

My problem is that I always encounter different branches and rejection messages when I try to push my code.

Even though I have synchronized my local and remote repositories (i.e., I can successfully run pull and push), when I edit a file and pull it to my local repository and then push it to the remote repository, I still encounter different branches and rejection messages.

At this point, if I pull first, the pull is successful, and I can proceed normally after the pull.
However, after this, any new changes still require me to pull first before pushing.
Only I use this repository, so I do not know why there are conflicts between the local and remote repository.

My concern is that if I pull code from the remote repository, it might overwrite my local code, causing me to lose the changes I've made.
This makes me hesitant to pull the code. However, if I don't pull the code, I won't be able to push my changes either.

Question 1

This conflict isn't caused by a difference between the local and remote code. If there were differences between the local and remote code, a merge prompt would appear.
My situation is different from this, and I don't understand why this is happening.

Question 2

I've read some articles that suggest if I submit my local changes, pulling remote code won't overwrite local code.
Is this true?

Question 3

Additionally, if there are differences between the local and remote code, a merge prompt will appear.
I don't want to delete any code from either the local or remote repository when conflicts occur.
I want to keep both and choose the local code as the final version.

When the &lt;&lt;&lt;&lt;&lt;&lt;&lt;======= &gt;&gt;&gt;&gt;&gt;&gt;&gt; prompt appears, I can delete or modify the content within the prompt, and then use "git add ." to save the local and remote code changes before uploading them to GitHub.
Since I'm the only one using Git, and I've synced the local repository using OneDrive across my work and personal computers, I'm not worried about overwriting the remote code.
However, sometimes I may make changes directly on GitHub, so I need to ensure that the local and remote code are preserved when conflicts occur.

The following image is the tree showed in my git.
I don't understand why it's so complicated.

git: always need pull before pushing code, even I'm the only one working on this repository

答案1

得分: 1

> 我的担忧是,如果我从远程存储库中拉取代码,它可能会覆盖我的本地代码,导致我丢失已经做出的更改。

这就是为什么我更喜欢默认设置stash + pull --rebaseGit 2.6+

git config --global pull.rebase true
git config --global rebase.autoStash true

这样,简单的git pull会在刷新后的origin/myCurrentBranch之上重放(rebase)我的本地提交(尚未推送的提交),确保不会覆盖我的本地工作。

另外,如果我有“未提交”的工作(尚未添加到索引并提交的本地修改),"autostash" 部分会确保正在进行的工作在拉取之前保存,并在拉取后重新应用。
1: https://stackoverflow.com/a/30209750/6309

英文:

> My concern is that if I pull code from the remote repository, it might overwrite my local code, causing me to lose the changes I've made

That is why I prefer setting a stash + pull --rebase by default (Git 2.6+)

git config --global pull.rebase true
git config --global rebase.autoStash true

That way, a simple git pull will replay (rebase) my local commits (the one not yet pushed) on top of the refreshed origin/myCurrentBranch, making sure my local work is not overridden.

Additionally, if I have "unsubmitted" work (local modification not yet added to the index and committed), the "autostash" part make sure the work in progress is saved before the pull, and re-applied after the pull.
1: https://stackoverflow.com/a/30209750/6309

huangapple
  • 本文由 发表于 2023年4月4日 15:25:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定