A while ago, I uploaded some of my projects to GitHub to serve as a backup. Now, I want to link my local files to their respective GitHub repos to make use of Git's version history in VS Code.
The issue is, all of the tutorials I have found online rely on either the remote repository or local repository being empty. However, my GitHub repo is different from the local files on my machine. I also want to be able to view the differences between the two before pushing my changes to the remote repo (i.e. git pull
and then git diff
before git push
).
So, how can I pull from a remote GitHub repo without overwriting my local files, and then afterwards push my local changes to GitHub?
Now, I want to link my local files to their respective GitHub repos
This is not how Git works. You do not link files to a remote. Git reasons in terms of commits, not files. Therefore, if you have a certain history in your local repo and a different history on your remote, you first need to reconcile the two histories.
Since in your question you've stated "A while ago I uploaded some of my projects to GitHub", I assume that your remote contains an older version of your files, while your local repo contains the updated versions.
In this scenario, you could:
git remote add origin https://github.com/user/my-repo.git
master
).git fetch origin master
git rebase origin/master master
git push origin master
Instead, if you want to overwrite the history and the content of your remote with your local changes, you can force push to the remote.
git push --force origin master
If you wish to examine the remote's changes before proceeding with the rebase, you could perform a git log origin/master
to check the history of changes on the remote branch.
Alternatively, you could run git checkout origin/master
to inspect the content on the remote branch. Beware that checking out a remote tracking branch will bring you into the detached HEAD state. In this state, you will still be able to make changes and perform commits, but in order for you to retain the new changes, you need to perform a git checkout -b <new-branch>
to save them under a new branch. The detached HEAD state is perfect to examine and test things. If you want to exit this state without maintaining any eventual change, just checkout any branch, for example git checkout master
.