The Git Cheat Sheet for most Common Operations

Add, commit and push files to Git
Add :
git add <file_path>
Commit :
git commit -m "<commit_message>"
Push :
git push origin <branch_name>
Fetch and apply changes from the upstream branch
Fetch :
git fetch upstream
Apply : Make sure you are in the branch that the fetched changes should be applied to.
git rebase upstream/<upstream_branch_name>
Change the commit message of the last commit
git commit --amend
Change the commit message of multiple commits
git rebase -i HEAD~3
Then change the word “pick” to “reword” for commit messages you want to change. 3 is the number of commits to go back.
Squash commits
git rebase -i HEAD~2
Then change the word “pick” to “squash” for new commits you need to squash with the previous commit. 2 is the number of commits to go back.
Checkout a specific tag
List the available tags :
git tag -l
Checkout :
git checkout tags/<tag_name> -b <new_branch_name>
Locally merge an upstream pull request
git pull upstream pull/<PR_ID>/head
Ex: git pull upstream pull/200/head
Undo an accidental git merge that has not been pushed to GitHub yet
Method 1 : Check git log to find the commit_hash for the command below.
git reset --hard <commit_hash>
Method 2 : 5 here is the number of commits that you need to get back.
git reset --hard HEAD~5
Method 3 : ORIG_HEAD will point to a commit directly before merge has occurred. You don't need to find for the commit_hash.
git reset --hard ORIG_HEAD
Reset already added files
git reset <path-to-file>
Delete multiple files from Git at once
Track the deleted files :
git ls-files --deleted -z | xargs -0 git rm
The deleted files will be in green now, meaning they are ready to be committed. After listing the files from the above command, you can commit and push them to GitHub.
Delete one or specific files from Git
git rm <file1> <file2> <file3>
Force push to GitHub
git push -f origin <branch_name>
Set upstream URL for the local repo
git remote add upstream <url_to_upstream_repo_in_git>
Ex : git remote add upstream https://github.com/perlCompany/newrepo.git
Change URL already set in local repo
git remote set-url <name> <url_to_name_repo_in_git>
Ex : git remote set-url origin master
Apply a specific commit from one branch to another
Checkout to the branch you need to apply the commit to. Then execute,
git cherry-pick <commit_hash_from_other_branch>
Create a new branch
git checkout -b <new_branch_name> <existing_branch_name>
Push a local branch to a remote repository
git push origin <new_branch_name>
Delete an existing branch
Before executing the command below, make sure you are in a different branch than the branch that you want to delete.
If there are no uncommitted changes in the branch to delete, -d option can be used.
git branch -d <branch_to_delete>
If there are uncommitted changes but you anyway want to delete the branch, use the -D option as follows.
git branch -D <branch_to_delete>
Revert back to a particular commit
Note : The hard option in the command below deletes all other commits done after the specific commit that you are reverting back to.
git reset --hard <commit_hash>
Stash the changes
When you want to update your working branch with the changes in the upstream branch, this comes in handy. If you already have uncommitted changes in the local working branch, you can stash them first, merge the changes from the upstream branch and then apply the stashed changes on top of the updated branch.
Stash the uncommitted changes with
git stash
After updating apply the stashed changes back
git stash pop
Thank you for reading the article!
If you find any outdated content or issues with this article, please feel free to create an issue at Developer Corner Git repository here. Let’s grow together and help others in their journey too!
If you like this article please give it a clap. 🙂
Cheers!