Git Workbook
Last updated: 16 Sept 2026
Git commands I that are useful to me regularly
Initial Setup
Set username and email
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
Basic
Initialise
git init
Add/Stage
git add <file_name> # Stages a single file|
git aad .
Commit
git commit -m “Message” # Commits with a one-line message
| Git Commands | Action Performed |
|---|---|
| git add —all | Stages all files in the repository, including new, modified, and deleted files. |
| git add . | Stages all changes in the current directory. |
| git add -u | Stages only modified and deleted tracked files, ignoring new untracked files. |
Branches
git branch # view local branches
git branch -a # all branches
git branch -r # remote branches
New Branch
git checkout -b <branch_name> # create new branch
Deleting
git branch -D <local branch name>
git push origin -D <remote branch name> # remote branch
Status
git status # full status
git status -s # short status
Intermediate
Push
git push origin <banch_name>
Rename Branch
Local
in the branch
git branch -m <new_name>
From another branch
git branch -m <old_name> <new_name>
Remote
Delete remote branch
git push origin --delete <old_branch>
Push renamed branch
git push origin -u <new_branch>
Logging
git log # Full history
git log --oneline # Summary
git log --reverse
Advanced
Revert to a previous commit
git log # find the specific commit
git revert --no-commit 0d1d7fc3..HEAD
git commit
Rebase
git checkout my-feature-branch
Checkout a new branch from it:
git checkout -b my-feature-branch-backup
Go back to your original branch:
git checkout master
git pull
git checkout my-feature-branch
git rebase origin/master
# Rebase current branch onto another
git rebase <branch-name>
# Abort a rebase in progress
git rebase --abort
# Continue a rebase after resolving conflicts
git rebase --continue
# Force push safely after a rebase
git push --force-with-lease origin <branch-name>
Orphan Branch
# Create a branch with no history (no parent commit)
git checkout --orphan <branch-name>
# Delete all files from the index after creating orphan branch
git rm -rf .
Merging Unrelated Branches
# Merge branches that have no common ancestor
git merge <branch-name> --allow-unrelated-histories
# Accept all incoming changes during a merge conflict
git checkout --theirs .
# Accept all current branch changes during a merge conflict
git checkout --ours .
Git Dubious
git dubious thing solved by git config --global --add safe.directory '*'
Back to log