DEV Community

tanish vashisth
tanish vashisth

Posted on

EASY GIT

Git Cheat Sheet: A Quick Reference Guide

$ git config --global user.name "Your Name"
Enter fullscreen mode Exit fullscreen mode

Assign Your Email

$ git config --global user.email xyz@domain.com
Enter fullscreen mode Exit fullscreen mode

Initialization

Initialize Git in Your Folder

$ git init
Enter fullscreen mode Exit fullscreen mode

Show Git Status

$ git status
Enter fullscreen mode Exit fullscreen mode

Basic Commands

Add File to Staging Area

$ git add index.html
Enter fullscreen mode Exit fullscreen mode

Commit File

$ git commit
Enter fullscreen mode Exit fullscreen mode

Commit with Message

$ git commit -m "Added more HTMLs"
Enter fullscreen mode Exit fullscreen mode

Send All Files to Staging Area

$ git add -A
Enter fullscreen mode Exit fullscreen mode

Recover File or Match Last Commit

$ git checkout index.html
Enter fullscreen mode Exit fullscreen mode

Recover All Files

$ git checkout -f
Enter fullscreen mode Exit fullscreen mode

Viewing Commits

View Commit Log

$ git log
Enter fullscreen mode Exit fullscreen mode

Filter Commit Log

$ git log -p (number of commits)
Enter fullscreen mode Exit fullscreen mode

Comparisons

Working Tree and Staging Area

$ git diff
Enter fullscreen mode Exit fullscreen mode

Compare Last Commit to Staging Area

$ git diff --staged
Enter fullscreen mode Exit fullscreen mode

Advanced Committing

Commit All Files Directly

$ git commit -a -m "Skipped staging area and fixed"
Enter fullscreen mode Exit fullscreen mode

Remove File from Directory and Staging Area

$ git rm about.html
Enter fullscreen mode Exit fullscreen mode

Remove File from Staging Area

$ git rm --cached about.html
Enter fullscreen mode Exit fullscreen mode

Show Staging and Working Tree Status

$ git status -s
Enter fullscreen mode Exit fullscreen mode

Remote Repositories

Add Remote Repo (Origin)

$ git remote add origin https://github.com/Prome-theus/CSES.git
Enter fullscreen mode Exit fullscreen mode

Get Remote Repo Links

$ git remote -v
Enter fullscreen mode Exit fullscreen mode

Push to Remote Repo (Origin - Master Branch)

$ git push origin master
Enter fullscreen mode Exit fullscreen mode

Change Remote Repo URL

$ git remote set-url origin git@github.com:Prome-theus/CSES.git
Enter fullscreen mode Exit fullscreen mode

Push to Remote Repo with Default Branch Set

$ git push -u origin master
Enter fullscreen mode Exit fullscreen mode

Clone a Repository

$ git clone {url}
Enter fullscreen mode Exit fullscreen mode

Branching

Create a New Branch

$ git branch feature1
Enter fullscreen mode Exit fullscreen mode

Show All Branches

$ git branch
Enter fullscreen mode Exit fullscreen mode

Switch Branch

$ git checkout feature1
Enter fullscreen mode Exit fullscreen mode

Merge Branch to Master

$ git merge feature1
Enter fullscreen mode Exit fullscreen mode

Create and Switch to a New Branch

$ git checkout -b htmlcodes
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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)