Git Cheatsheet Every Dev. MUST know
INSTALLATION & GUIS
With platform-specific installers for Git, GitHub also provides the ease of staying up-to-date with the latest releases of the command-line tool while providing a graphical user interface for day-to-day interaction, review, and repository synchronization.
GitHub for Windows
GitHub for Mac
For Linux and Solaris platforms, the latest release is available on the official Git website.
Git for All Platforms
What is Git
Git is the free and open-source distributed version control system that’s responsible for everything GitHub-related that happens locally on your computer. This cheat sheet features the most important and commonly used Git commands for easy reference.
Basic Concepts
main: default development branch
origin: default upstream repository
HEAD: current branch
HEAD^: parent of HEAD
HEAD~4: great-great-grandparent of HEAD and so on
Setup
git config --global user.name "Mhassanbughio"
git config --global user.email "muhammadhassan.sw18@muetkhp.edu.pk"
- Start a project:
git init <directory>
- Clone a Repo:
git clone <url>
Make a Change
- Add a file to staging:
git add <file>
- Stage all files:
git add .
- Commit all staged files to git with a message:
git commit -m "Message goes here"
- Commit all changes made to track files and commit:
git commit -am "Message Goes Here"
Branches
- Add -r flag to show all removed branches. Add -a flag for all branches:
git branch
- Create a new branch:
git branch <branch-name>
- Switch to a new branch and update the working directory:
git checkout <branch>
- Create a new branch and switch to it:
git checkout -b <new-branch>
- Delete a merged branch:
git branch -d <branch>
- Delete a branch, whether merged or now:
git branch -D <branch>
- Add a tag to the current commit (often used for new version releases):
git tag <tag-name>
TRACKING PATH CHANGES
Versioning file removes and path changes
git rm [file] delete the file from project and stage the removal for commit
git mv [existing-path] [new-path] change an existing file path and stage the move
git log — stat -M show all commit logs with indication of any paths that moved
Merging
Merge branch a into branch b. Add -no-ff option for no-fast-forward-merge
Merge
git checkout b
git merge a
- Merge and squash all commits into one new commit:
git merge -- squash a
Rebasing
- Rebase feature branch onto main (to incorporate new changes made to main). Prevents unnecessary merge commits into the feature, keeping history clean:
git checkout feature
git rebase main- Interactively clean up a branch’s commits before rebasing onto main:
git rebase -I main
- Interactively rebase the last 3 commits on the current branch:
git rebase -I Head~3
Undoing Things
- Move (&/or rename) a file & stage move:
git mv <existing_path> <new_path>
- Remove a file from the working directory & staging area, then stage the removal:
git rm <file>
- Remove from staging area only:
git rm --cached <file>
- View a Previous Commit (READ ONLY):
git checkout <commit_ID>
- Create a new commit, reverting the changes from a specified commit:
git revert <commit_ID>
- Go back to a previous commit & delete all commits ahead of it (revert is safer). Add — hard flag to also delete workspace changes (BE VERY CAREFUL):
git reset <commit_ID>
Review your Repository
- List new or modified files not yet committed:
git status
- List commit history, with respective IDs:
git log --online
- Show changes to unstaged files, add — cached option:
git diff
- Show changes between two commits:
git diff commit1_ID commit2_ID
Stashing
- Store modified & staged changes. To include untracked files, add -u flag. For untracked & ignored files, add the -a flag:
git stash
- As above, but add a comment:
git stash save "comment"
- Partial stash. Stash just a single file, a collection of files, or individual changes from within files:
git stash -p
- List all stashes:
git stash list
- Re-Apply the stash without deleting it:
git stash apply
- Re-Apply the stash at index 2, then delete it from the stash list. Omit stash@(n) to pop the most recent stash:
git stash pop stash@(2)
- Show the difference summary of stash 1. Pass the -p flag to see the full difference:
git stash show stash@(1)
- Delete stash at index 1. Omit stash@(n) to delete the last stash made:
git stash drop stash@(1)
- Delete all stashes:
git stash clear
Synchronizing
- Add a remote Repo:
git remote add <alias> <url>
- View all remote connections. Add -v flag to view URLs:
git remote
- Rename a Connection:
git remote rename <old> <new>
- Fetch all branches from remote repo (no merge):
git fetch <alias>
- Fetch a specific branch:
git fetch <alias> <branch>
- Fetch a remote repo’s copy of the current branch, then merge:
git pull
- Move (rebase) your local changes onto the top of new changes made to the remote repo (for clean, linear history):
git pull --rebase <alias>
- Upload local content to the remote repo:
git push <alias>
- Upload to a branch (can then pull request):
git push <alias> <branch>
written:
BY Muhammad Hassan Advocate Open Source 🤩
Don’t forget to visit my Github profile