Helpful Git Routines and Commands
Copy Repo to Private Repo
Often you need to copy a repo to your own private repo. To do this, follow the steps below.
-
In the command line, CD into the main directory where your git repos are.
-
Remember, before you can push the original repository to your new copy, or mirror, of the repository, you must create the new repository on GitHub.com.
-
Then follow the directions here: https://docs.github.com/en/repositories/creating-and-managing-repositories/duplicating-a-repository.
-
You can now clone your new repository on your machine:
git clone https://github.com/yourname/private-repo.git
cd private-repo
make some changes
git commit
git push origin master (or main - or just use the desktop git tool now, it's easier)
-
Now if you want to pull changes from the original repo you copied, you can add new remotes.
-
add back the old remote from step 1, but NOT as the origin, because we want to be able to pull changes from that remote, in this example we would do:
git remote add YOURNEWNAME <THEPUBLIC REP You copied>
so sort of like thisgit remote add public https://github.com/exampleuser/public-repo.git
-
git remote -v
to check to make sure your new remote is there. -
Then we are done and if we want to pull changes, we can create a NEW branch, switch to that branch, and then run
git pull <your_remote_name> <branch_name>
to pull changes from the original repo. We can then merge any changes into the main branch, if we want. So sort of like this
git pull public master # Creates a merge commit
git push origin master
-
You can list all your remotes with
git remote -v
. You should see: -
In theory, we should disable push on the new remote, as we can't push to the original repo, but it's not necessary really. You can disable it by running
git remote set-url --push <upstream = name of your remote> DISABLE
Removing Git History
Sometimes you want to remove the history of the branch you copied, so you can start fresh, if you are sure you want to remove all commit history, after you followed all the steps above, simply delete the .git directory in your project root (note that it's hidden). Then initialize a new repository in the same folder and link it to the GitHub repository:
cd into the directory that has the git and run: rm rf .git
(do a backup first!)
git init
git remote add origin git@github.com:user/repo (first create this origin in github and follow the commands there)
now commit your current version of code
git add *
git commit -am 'message'
and finally force the update to GitHub:
git push -f origin master
Then just following current git terminology, make master the main branch on git hub, delete main on github. Then locally create a new branch main, push to github and then make main branch the main again and delete master.
Merging remote
First make sure you have the remote in your git - git remote -v
then you can checkout to the branch you want to pull into, so: git checkout "BRANCH"
Then git pull origin master
will pull changes from the origin remote, master branch and merge them to the local checked-out branch. So you can also change the name to ```git pull REMOTENAME REMOTEBRANCHNAME
Problem logging out github desktop (windows) with Visual Studio
In Visual Studio on the left side bottom, there is a people icon. Click on that. Log out of your user and then log back in.