Personal Git cheat sheet
Setting up git credentials on new machine.
Click to expand
Reference: GitHub Docs for SSH keys
-
Use SSH URL for
git clone
(e.g.ssh://[email protected]/username/repo.git
). Edit in.git/config
if previously cloned usinghttps
orgit
URL. See reference here.
Click to expand
To quickly clone a public repo into a private repo. Caveat: clone of public repo on local machine is not usable for development.
-
Clone the public repo onto local machine. This includes all branches and commit history, but without any link back to the Github repo, i.e. you will not able to
git fetch
again.git clone --bare <public repo url> <local folder name>
-
Create private repo on Github.
-
Enter local folder.
cd <local folder name>
-
Push the local version of the contents of public repo to the private repo.
git push --mirror <private repo url>
-
Delete local folder.
cd ..
rm -rf <local folder name>
-
Clone private repo.
git clone <private repo url> <local folder name>
-
To setup the new local folder to track changes in the public repo, see steps 7 onward in the next section to create a tracking remote.
Click to expand
To clone a public repo into a private repo and track changes of the public repo.
-
Clone the public repo onto local machine.
git clone <public repo url> <local folder name>
-
Create private repo on Github.
-
Enter local folder.
cd <local folder name>
-
Change remote for
origin
for local folder to private repo.origin
is the default pull/push Git repo for local folder.git remote set-url origin <private repo url>
-
Check that the origin remote has been changed.
git remote -v
> origin <private repo url> (fetch) > origin <private repo url> (push)
Alternatively, check that the remote "origin" is set to private repo url in the git config file.
cat .git/config
-
Push cloned files to
main
branch on private repo. This also sets the upstream formain
to theorigin
remote.git push origin main
-
Create branch to track public repo.
git branch <tracking branch name>
-
Create new remote to track public repo.
git remote add <new remote name> <public repo url>
-
Check that the new tracking remote has been created.
git remote -v
-
Fetch the branch info from the new tracking remote (aka the public repo).
git fetch <new remote name>
-
Change the upstream for the new tracking branch to the new tracking remote.
git branch <tracking branch name> --set-upstream-to <tracking remote name>/<branch on public repo>
-
Check that the main development branch is tracking the private repo and the tracking branch is tracking the public repo.
cat .git/config
Click to expand
Create new feature on new working branch.
-
Create new working branch from existing branch (e.g. 'development').
git checkout -b <new branch name> <existing branch name>
-
Push the new branch to Github (or remote).
git push --set-upstream <remote name> <new branch name>
Click to expand
-
View commit history.
git log --oneline
-
Squash x past commits.
git rebase -i HEAD~<x>
- An editor will open.
- Use
pick
orp
to retain specific commits. Usesquash
ors
to combine specific commits. - Once done, save the file and close.
- Another editor will open with the new commit messages for further editing if required.
- Once done, save the file and close.
To clean up commits on one branch (e.g. dev branch) when merging it into another (e.g. main branch).
-
Switch to main branch.
git switch <main branch>
-
Use
--squash
flag when merging incoming branch (e.g. dev branch).git merge --squash <dev branch>
-
Commit the changed files with a single commit message.
-
Push to recipient branch (e.g. main branch), create Pull Request on GitHub, and merge Pull Request.
-
Check past commits.
git reflog
-
Reset current branch to desired commit (
HEAD@{x}
).git reset --hard HEAD@{x}
For Windows:
git reset --hard "HEAD@{x}"
Click to expand
-
Fetch changes made to the upstream since the last
git fetch
, merge to local branch, and apply existing local commits to the top of the updated local branch (aka "rebasing the local branch on top of the upstream branch after fetching"). (Explainer)git pull --rebase
Click to expand
git config --global core.autocrlf input
git config --global core.autocrlf true
Click to expand
-
Clone submodule into repo.
git submodule add <submodule URL>
-
Commit and push changes to origin (new submodule folder and new .gitmodules file).
-
Clone the repo.
-
Initialise submodule.
git submodule init
-
Fetch data from submodule project.
git submodule update