git
Master Git with this comprehensive cheatsheet. Learn essential Git commands for configuration, branching, merging, cherry-picking, tagging, and more. Boost your version control workflow.
Git Cheatsheet
Git Configuration
Set global Git configurations:
git config --global --edit
Your global configuration file (~/.gitconfig) should look similar to this:
[user]
name = Ruan Bekker
email = <user>@<domain>
Configure Git settings for a specific repository:
git config user.name "Ruan"
git config user.email "me@example.com"
git config commit.gpgsign true
Fetching Remote Branches
Fetch all branches from all remote repositories:
git fetch --all
Fetch a specific remote branch and create a local branch:
git fetch origin <local-branch-name>:<remote-branch-name>
Displaying Branch Information
Show branches containing a specific commit:
git --no-pager branch -a --contains 0000000000000000000000000000000000000
remotes/origin/dev
Display only the branch name from the above output:
git --no-pager branch -a --contains 0000000000000000000000000000000000000 | rev | cut -d '/' -f1 | rev
dev
List all local and remote branches:
$ git branch -a
Getting the Current Working Directory
Display the root directory of the current Git repository:
$ git rev-parse --show-toplevel
Merging Master into Your Branch
Synchronize your current branch with the latest changes from the master branch:
$ git checkout master
$ git pull origin master
$ git checkout feature/one
$ git merge master
$ git push origin feature/one
Deleting Branches
Delete a local branch:
$ git branch -D branch-name
Using Cherry Picks
Scenario: You are on a new branch feature/change-website-color
, created from main
, and want to incorporate a specific commit from feature/fixed-css
.
Create your new branch and make initial commits:
git checkout main
git pull origin main
git checkout -b "feature/change-website-color"
# Make your changes
git commit -m "Changed website color to blue"
Cherry-pick the desired commit:
git checkout main
git fetch --all
git checkout feature/fixed-css
git log
# Copy the commit ID you want to cherry-pick
git checkout feature/change-website-color
git cherry-pick -e <commit-id-that-you-copied>
Commit the cherry-picked changes and push to your branch:
git commit -m "cherry picked css fix"
git push origin feature/change-website-color
Managing Git Tags
Prepare to create a tag by syncing with the main branch:
git checkout main
git pull origin main
Create a tag named snapshot-20230525
:
git tag snapshot-20230525
Push the newly created tag to the remote repository:
git push origin snapshot-20230525
List all tags, sorted by version in descending order, and filter by a prefix:
git tag --sort=-version:refname | grep "release-*"
Find the previous release tag (assuming the current is 0.55.0):
git tag --sort=-version:refname | grep "release-*" | sed -n '2p'
# Expected output: release-0.54.0
Unstaging Changes
If you have accidentally staged changes that you do not want to commit, you can unstage them:
git restore --staged modules/asg/variables.tf