Mastering Git with GitHub: A Comprehensive Cheat Sheet

Aspiring DevOps Engineer | Linux | Git & Github | Shell Scripting | Docker | CI/CD Jenkins | Kubernetes | AWS | Terraform | JIRA | Python |
Initialize a repository:
git initClone a repository:
git clone <repository_url>Check the status:
git statusAdd 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 logCreate 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 -vAdd 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 stashApply stashed changes:
git stash applyTo see a list of stashed changes.
git stash listCreate 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 --allShow the last commit:
git show HEADShow the difference between the working directory and the staging area:
git diffShow the difference between the staging area and the last commit:
git diff --stagedShow 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 --abortAbort a rebase:
git rebase --abortContinue a merge after resolving conflicts:
git merge --continueContinue a rebase after resolving conflicts:
git rebase --continueConfigure default editor:
git config --global core.editor <editor_name>Show commits that changed a file:
git log -- <file_path>Ignore files globally: Create a
.gitignorefile 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 -vChange 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.




