Git Cheat Sheet
The Git commands used day to day, grouped by task: setting your identity, starting or cloning a repository, staging and committing, branching, merging and rebasing, undoing changes, reviewing history, stashing and syncing with remotes — plus three recipes for untracking a committed node_modules or .env, restoring a file from an older commit and changing a remote URL.


Setup
Set the name and email that will be attached to your commits and tags:
git config --global user.name "your-username"
git config --global user.email "youremail@gmail.com"Start a Project
Create a local repo (initialize the current directory as a git repo):
git init <directory>Download a remote 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:
git commit -m "commit message"Add changes to tracked files to staging:
git commit -am "commit message"Remove node_module / .env file from GitHub
To remove a file from your GitHub repository, follow these steps:
- Remove the file from your local repository using the
git rmcommand:
git rm <file_path>- Commit the changes to your local repository:
git commit -m "Removed <file_path>"- Push the changes to your remote GitHub repository:
git push origin <branch_name>If you want to remove the file from version control but keep it in your local file system, use:
git rm --cached <file_path>Retrieve a File from a Previous Commit
To retrieve a file from a previous commit in your Git repository, follow these steps:
- Find the commit hash that contains the version of the file you want. Use:
git log --oneline- Checkout the file from the specific commit using:
git checkout <commit_hash> -- <file_path>- Optionally, commit the changes if you want to keep the retrieved version:
git add <file_path>
git commit -m "Reverted <file_path> to version from <commit_hash>"
git push origin <branch_name>Change the Remote Repository URL
To change the remote repository URL in your Git repository, follow these steps:
- View the current remote URL (optional):
git remote -v- Change the remote URL using the
set-urlcommand:
git remote set-url origin <new_url>- Verify the change (optional):
git remote -vBasic Concepts
master: Default development branchorigin: Default remote nameHEAD: Current branchHEAD^: Parent of HEADHEAD~4: 4th commit back from HEAD
Branches
List all local branches (add -r flag to show all remote branches, -a to show all branches):
git branchCreate a new branch:
git branch <new-branch>Switch to a 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 not:
git branch -D <branch>Add a tag to current commit (use -a to tag new releases):
git tag <tag-name>Merging
Merge a branch into a branch B (add --no-ff to avoid fast-forward merge):
git merge <branch>Merge and rebase all commits at once (single commit):
git merge --squash <branch>Rebasing
Rebase feature branch onto main to incorporate new changes made to main (prevents unnecessary merge commits into feature, keeping history linear):
git checkout feature
git rebase mainIteratively clean up a branch's commits before rebasing onto main:
git rebase -i mainIteratively rebase the last 3 commits in the current branch:
git rebase -i Head~3Undoing Things
Move (and/or rename) a file and stage it:
git mv <existing-path> <new-path>Remove a file from the working directory and from staging area, then stage the deletion:
git rm <file>Reset a file or a commit:
- Reset a file from staging area only:
git reset <file>- Reset the index and working directory to a specific commit (read-only):
git reset --hard <commit_ID>Revert a commit by creating a new commit:
git revert <commit_ID>Restore file to a specific commit (leaves staging alone):
git checkout <commit_ID> <file>Review your Repo
List new or modified files not yet committed:
git statusList commit history, with respective IDs:
git log --onelineShow changes to unstaged files (to changes of staged files, add --cached option):
git diffShow changes between two commits:
git diff commit1_ID commit2_IDStashing
Store modified and staged changes (to include untracked files, add -u flag. For untracked & ignored files, add -a):
git stashAs above, but add a comment:
git stash save "comment"Partial stash (stash just single file):
git stash push -pList all stashes:
git stash listApply stash:
git stash apply stash@{1}Delete stash at index 1 (omit stash@{n} to delete the last stash):
git stash drop stash@{1}Clear all stashes:
git stash clearSynchronizing
Add a remote repo:
git remote add <alias> <url>View all remote connections (add -v to view full URLs):
git remoteRemove a connection:
git remote remove <alias>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>Merge fetched changes into current branch:
git pullPush all branches to remote repo:
git push <alias> <branch>This cheat sheet summarizes the most common Git commands and operations. For more detailed information, refer to the official Git documentation.
Common Questions
- How do I undo the last Git commit but keep my changes?
- Run
git reset --soft HEAD~1: the commit is removed and its changes stay staged.git reset HEAD~1keeps them unstaged instead. If the commit is already pushed, usegit revert <commit>, which adds a new commit that undoes it without rewriting shared history. - What is the difference between git merge and git rebase?
git mergejoins two branches with a merge commit and keeps both histories as they happened.git rebasereplays your commits on top of the other branch, giving a straight, linear history. Never rebase commits that others have already pulled.- How do I remove node_modules or .env from GitHub after committing them?
- Add them to
.gitignore, rungit rm -r --cached node_modules(orgit rm --cached .env), then commit and push. The files stay on your machine. If.envheld secrets, change them: they are still in the repository’s history. - How do I change a Git remote URL?
- Run
git remote set-url origin <new-url>, thengit remote -vto check it. This is the fix when a repository moves, is renamed, or when you switch from HTTPS to SSH.