Git Version Control
Introduction
Hello. This is my git cheat sheet for me to easily refer to.
Over time I will append links to official documentation. If something does not look right, please reach out.
Git clone
Used when beginning work on a new project and you need a local copy of the repo. Keep in mind this preserves previous commits made by others.
git clone https://<username>/<repository>.git
For a fresh start, download the files as a zip and
git init
Git push
Update remote branch or repository with local changes. Omit local_branch to make changes to all branches with upstream connection
git push origin <local_branch>
If working on a new branch created locally, you must first set upstream
git push origin <new_or_old_remote_branch> –-set-upstream
Update new branch name in remote repository, And then set upstream connection
git push <remote> :<old_branch_name> <new_branch_name>
Remove a branch from the remote repository
git push <remote> :<branch_name>
Git pull
Performs a git fetch and git merge in a single operation. If there are merge conflicts, they must be resolved by passing various arguments.
git pull origin
Add –rebase flag to append changes as commit ontop of local HEAD
git pull origin --rebase
Git branch
List all the local branches
git branch
To list remote branches, include the -a flag
git branch -a
Change the branch name with the -m flag. You should checkout the branch you want to rename
git branch -m <new_branch_name>
Delete a branch by adding -d flag and ensure you are not checked into said branch
git branch -d <branch>
Use -D flag to force delete. Ensure you are okay with losing unmerged changes
git branch -D <branch>
Git checkout
Switches to an existing branch
git checkout <branch name>
Creates a new branch and switches to it
git checkout -b <branch name>
Git remote
Check url set for push and pull to remote repository
git remote -v
Set remote repository url
git remote add origin https://github.com/<username>/<repository>.git
Change remote repository url
git remote set-url origin https://github.com/<username>/<repository>.git
Shows entire remote configuration including all references to branches
git remote show origin
Git fetch
Prunes local references to branches that are no longer on the remote repository
git fetch --prune or git fetch -p
Git config
Change global settings for git locally
git config --global --edit
Git rebase
Begin rebase in interactive mode from the very first commit
git rebase -i --root
Used to navigate when checking out a commit during rebase
git rebase --continue #Proceed with changes to next commit
git rebase --skip #Ignore current commit and continue to next
git rebase --abort #Entirely stop rebase and revert to previous state
Git commit
Commit staged changes with a message
git commit -m "Enter message here"
Make changes to previous commit during rebase, add –no-edit flag to skip interactive
git commit --amend --author="User name <user@domain.com>" --no-edit
git commit --amend --date="Mon Jun 12 12:05:24 2023 +0200"
Git reset
If you messed up and need to hard revert to previous point in history, run
git reflog #To get the head offset value
git reset --hard HEAD@{5}
Git merge
When you have pushed changes to remote repository and need to update local branch with origin/main ‘s changes.
git checkout <branch>
git merge origin/main
Others
git log --oneline
git reflog
git status #Check if changes are staged and status of branches