Mastering Git with GitHub: A Comprehensive Cheat Sheet
Initialize a repository:
git init
Clone a repository:
git clone <repository_url>
Check the status:
git status
Add changes to the staging area: ( git add . --> to stage all files)
git add <file_name>
Commit changes:
git commit -m "Commit message"
View commit history:
git log
Create a new branch:
git branch <branch_name>
Switch to a branch:
git checkout <branch_name>
Merge branches:
git merge <branch_name>
Delete a branch: ( -d to delete merged branch and -D to delete un-merged branch)
git branch -d <branch_name>
Undo changes in the working directory:
git restore <file_name>
Undo changes in the staging area:
git reset HEAD <file>
View changes between commits:
git diff <commit1> <commit2>
Show details of a commit:
git show <commit>
Set username and email:
git config --global user.name "Your Name" git config --global user.email "youremail@example.com"
View remote repositories:
git remote -v
Add a remote repository:
git remote add <remote_name> <repository_url>
Push changes to a remote repository:
git push <remote_name> <branch_name>
Pull changes from a remote repository:
git pull <remote_name> <branch_name>
Fetch changes from a remote repository:
git fetch <remote_name>
Stash changes:
git stash
Apply stashed changes:
git stash apply
To see a list of stashed changes.
git stash list
Create a new branch and switch to it:
git checkout -b <new_branch_name>
Rename a branch:
git branch -m <old_branch_name> <new_branch_name>
Configure a remote repository URL:
git remote set-url origin <new_repository_url>
Delete a remote branch:
git push <remote_name> --delete <branch_name>
Squash commits:
git rebase -i HEAD~<number_of_commits>
View commit history graphically:
git log --graph --oneline --decorate --all
Show the last commit:
git show HEAD
Show the difference between the working directory and the staging area:
git diff
Show the difference between the staging area and the last commit:
git diff --staged
Show the difference between two branches:
git diff <branch1> <branch2>
Show the difference between two commits:
git diff <commit1> <commit2>
Show commits that will be merged or rebased:
git cherry-pick -n <commit>
Abort a merge:
git merge --abort
Abort a rebase:
git rebase --abort
Continue a merge after resolving conflicts:
git merge --continue
Continue a rebase after resolving conflicts:
git rebase --continue
Configure default editor:
git config --global core.editor <editor_name>
Show commits that changed a file:
git log -- <file_path>
Ignore files globally: Create a
.gitignore
file and add file patterns to ignore.Check which files are ignored:
git check-ignore -v <file>
Show the last commit on each branch:
git branch -v
Change the commit message of the last commit:
git commit --amend -m "New commit message"
List all tags:
git tag
These commands cover various aspects of Git version control and should serve as a handy reference for both beginners and experienced users.
Subscribe to my newsletter
Read articles from Vaishnavi Shivde directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vaishnavi Shivde
Vaishnavi Shivde
Aspiring DevOps Engineer | Linux | Git & Github | Shell Scripting | Docker | CI/CD Jenkins | Kubernetes | AWS | Terraform | JIRA | Python |