Git Cheat Sheet: A Quick Reference Guide
$ git config --global user.name "Your Name"
Assign Your Email
$ git config --global user.email xyz@domain.com
Initialization
Initialize Git in Your Folder
$ git init
Show Git Status
$ git status
Basic Commands
Add File to Staging Area
$ git add index.html
Commit File
$ git commit
Commit with Message
$ git commit -m "Added more HTMLs"
Send All Files to Staging Area
$ git add -A
Recover File or Match Last Commit
$ git checkout index.html
Recover All Files
$ git checkout -f
Viewing Commits
View Commit Log
$ git log
Filter Commit Log
$ git log -p (number of commits)
Comparisons
Working Tree and Staging Area
$ git diff
Compare Last Commit to Staging Area
$ git diff --staged
Advanced Committing
Commit All Files Directly
$ git commit -a -m "Skipped staging area and fixed"
Remove File from Directory and Staging Area
$ git rm about.html
Remove File from Staging Area
$ git rm --cached about.html
Show Staging and Working Tree Status
$ git status -s
Remote Repositories
Add Remote Repo (Origin)
$ git remote add origin https://github.com/Prome-theus/CSES.git
Get Remote Repo Links
$ git remote -v
Push to Remote Repo (Origin - Master Branch)
$ git push origin master
Change Remote Repo URL
$ git remote set-url origin git@github.com:Prome-theus/CSES.git
Push to Remote Repo with Default Branch Set
$ git push -u origin master
Clone a Repository
$ git clone {url}
Branching
Create a New Branch
$ git branch feature1
Show All Branches
$ git branch
Switch Branch
$ git checkout feature1
Merge Branch to Master
$ git merge feature1
Create and Switch to a New Branch
$ git checkout -b htmlcodes
Git Ignore
Create a .gitignore
file in your directory to specify files you want to ignore in the repository.
For example:
mylogs.log
*.log
/mylogs.log
ignore/
Remember to add and commit the .gitignore
file to your repository.
Branching Note
The master
branch is typically considered the main branch.
This cheat sheet covers essential Git commands, but feel free to explore more features and options in the official Git cheatsheet. Happy coding!
Top comments (0)