Skip to content

Instantly share code, notes, and snippets.

@alenabdula
Last active November 20, 2022 16:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alenabdula/5bf5dda100d884346711 to your computer and use it in GitHub Desktop.
Save alenabdula/5bf5dda100d884346711 to your computer and use it in GitHub Desktop.
Git Cheat Sheet
# Create git repository
git init
# Add all files to staging area
git add .
# Check current status
git status
# Commit staged changes for tracking
git commit -m "Initial Commit"
# Hard reset code the way it was at <commit_number>, destroying changes.
git reset --hard <commit_number>
# Soft reset code the way it was at <commit_number>, preserving changes
git reset --soft <commit_number>
# Add repository for sharing code with others
git remote add origin <repository_url>
# Set push defaults, only push changes to current active branch
git config --global push.default simple
# Add upstream reference to "master" branch or any new branch
git push -u origin master
# Push changes to currently active branch
git push
# Pushing tags
git push --tags
# Create new branch called "feature_x"
git checkout -b feature_x
# Delete local branch "feature_x"
git branch -d feature_x
# List all branches, -v | --verbose shows sha1 and commit subject line
git branch -v
# Delete upstream branch "feature_x"
git push origin --delete feature_x
# Switching branches
git checkout master
# Pull changes from upstream reference
git pull
# Merge feature_x branch to currently active branch
git merge feature_x
# Git will try to auto-merge, if you get conflicts
# edit files shown by Git and manually merge them
# then add edited files to stagging area
git add <filename>
# Preview differences between branches
# git diff <source_branch> <target_branch>
git diff master feature_x
# Adding tags
git tag 1.0.0 <commit_number>
# Log, view log of commits
git log
git log --pretty=oneline
git log --graph --oneline --decorate --all
# Local changes, reverting back files to current HEAD
git checkout -- <filename>
# Drop all your local changes and commits
git fetch origin
git reset --hard origin/master
# When contributing, or pushing to master branch consider rebasing your commits
git checkout feature_x
git rebase master
git checkout master
git merge feature_x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment