You’re the Expert!

git

Cheatsheets

Initialize a New Git Repository
# Initialize a new repository
git init

# Check the status of the repository
git status
copy to clipboard
Checking the Status of Your Files
# Check the status of the repository
git status

# Show changes in the working directory
git diff
copy to clipboard
Adding Files to the Staging Area
# Add a single file to the staging area
git add filename.txt

# Add all files to the staging area
git add .
copy to clipboard
Committing Changes
# Commit staged changes
git commit -m "Initial commit"

# View commit history
git log
copy to clipboard
Cloning a Repository
# Clone an existing repository
git clone https://github.com/username/repository.git
copy to clipboard
Checking the Commit History
# View the commit history in the repository
git log

# View the commit history with a specific format
git log --oneline
copy to clipboard

Creating and Switching Branches
# Create a new branch
git branch new-branch

# Switch to the new branch
git checkout new-branch

# Alternatively, create and switch in one command
git checkout -b new-branch
copy to clipboard
Merging Branches
# Switch to the branch you want to merge into
git checkout main

# Merge another branch into the current branch
git merge new-branch

# Resolve conflicts, if any
copy to clipboard
Deleting Branches
# Delete a local branch
git branch -d new-branch

# Delete a remote branch
git push origin --delete new-branch
copy to clipboard
Stashing Changes
# Stash changes that are not yet committed
git stash

# List all stashes
git stash list

# Apply the latest stash
git stash apply
copy to clipboard
Working with Remote Repositories
# Add a remote repository
git remote add origin https://github.com/username/repository.git

# Push changes to the remote repository
git push origin main

# Pull changes from the remote repository
git pull origin main

# Check remote repository URLs
git remote -v
copy to clipboard
Undoing Changes
# Undo changes in the working directory (before commit)
git checkout -- filename.txt

# Undo the last commit (keep the changes staged)
git reset --soft HEAD~1

# Undo the last commit (remove changes)
git reset --hard HEAD~1
copy to clipboard

Rebasing Branches
# Rebase a branch onto another branch
git checkout feature-branch
git rebase main

# Resolve conflicts if they occur during the rebase
copy to clipboard
Cherry-picking Commits
# Apply a specific commit from another branch
git cherry-pick <commit-hash>

# After cherry-pick, commit if necessary
copy to clipboard
Tagging Releases
# Create a new tag
git tag v1.0

# Push tags to the remote repository
git push origin v1.0

# List all tags
git tag

# Checkout a specific tag
git checkout v1.0
copy to clipboard
Resetting and Reverting Commits
# Reset to a specific commit (and delete changes)
git reset --hard <commit-hash>

# Revert a commit (create a new commit that undoes the changes)
git revert <commit-hash>
copy to clipboard
Resolving Merge Conflicts
# After a merge conflict, Git will mark the conflicting files
git status

# Open the conflicting file and manually resolve conflicts
# After resolving, stage the file and commit the merge
git add filename.txt
git commit
copy to clipboard
Squashing Commits
# Combine multiple commits into one (during rebase)
git rebase -i HEAD~3

# Mark the commits you want to squash
# Change 'pick' to 'squash' for the commits to combine

# After finishing, you'll have one commit containing all the changes
copy to clipboard