2

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 <<<<<<<======= >>>>>>> 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.

enter image description here

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
li jiaqi
  • 89
  • 7

1 Answers1

1

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+][1])

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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250