DEV Community

Cover image for 51 git commands that you'll ever need to get started with Git πŸ”₯
Pramit Marattha for Aviyel Inc

Posted on • Updated on

51 git commands that you'll ever need to get started with Git πŸ”₯

Version Control (Git) Basics

Hi !! In this blog tutorial, I will be listing out all the necessary and only command that you will ever need to start your GIT journey. You can bookmark this blog and come back to it, whenever it is necessary.

Checking the git configuration

git config -l

Enter fullscreen mode Exit fullscreen mode

Setting up your git username

git config --global user.name "pramit"

Enter fullscreen mode Exit fullscreen mode

Setting up email

git config --global user.email "pramit@aviyel.com"

Enter fullscreen mode Exit fullscreen mode

Caching credential

git config --global credential.helper cache

Enter fullscreen mode Exit fullscreen mode

Initialize repository

git init

Enter fullscreen mode Exit fullscreen mode

Adding filename to staging area

git add file_name

Enter fullscreen mode Exit fullscreen mode

Adding all the files to the staging area

git add .

Enter fullscreen mode Exit fullscreen mode

Add only certain files to the staging area

example add all files starting with "comp"

git add comp*

Enter fullscreen mode Exit fullscreen mode

Checking repo Status

git status

Enter fullscreen mode Exit fullscreen mode

Commit changes

git commit

Enter fullscreen mode Exit fullscreen mode

Commit changes with a message in it

git commit -m "YOOOO!!! This is a message"

Enter fullscreen mode Exit fullscreen mode

Add to staging area and commit changes with a message in it

git commit -a -m "YOOOO!!! This is another message"

Enter fullscreen mode Exit fullscreen mode

To see the commit history

git log

Enter fullscreen mode Exit fullscreen mode

Commit history and the following file changes

git log -p

Enter fullscreen mode Exit fullscreen mode

Show specific commit in git

git show commit_id

Enter fullscreen mode Exit fullscreen mode

Statistics about changes

git log --stat

Enter fullscreen mode Exit fullscreen mode

Changes made before committing them using diff

git diff
git diff some_file.js
git diff --staged

Enter fullscreen mode Exit fullscreen mode

Removing tracked files

git rm filename

Enter fullscreen mode Exit fullscreen mode

Rename files in git

git mv oldfilename newfilename

Enter fullscreen mode Exit fullscreen mode

Revert unstaged changes

git checkout file_name

Enter fullscreen mode Exit fullscreen mode

Revert staged changes

git reset HEAD filename
git reset HEAD -p

Enter fullscreen mode Exit fullscreen mode

Modify and add changes to the most recent commit

git commit --amend

Enter fullscreen mode Exit fullscreen mode

Rollback the last commit

git revert HEAD

Enter fullscreen mode Exit fullscreen mode

Revert a previous commit

git revert comit_id_here

Enter fullscreen mode Exit fullscreen mode

Create a new branch

git branch branch_name

Enter fullscreen mode Exit fullscreen mode

List branch in git

git branch

Enter fullscreen mode Exit fullscreen mode

Create a branch and switch it Immediately

git checkout -b branch_name

Enter fullscreen mode Exit fullscreen mode

Delete a branch in git

git branch -d branch_name

Enter fullscreen mode Exit fullscreen mode

Merge

git merge branch_name

Enter fullscreen mode Exit fullscreen mode

Commit log as a graph in git

git log --graph --oneline

Enter fullscreen mode Exit fullscreen mode

Commit log as a graph in git of all branches

git log --graph --oneline --all

Enter fullscreen mode Exit fullscreen mode

Abort a conflicting merge

git merge --abort

Enter fullscreen mode Exit fullscreen mode

Adding a remote repository

git add remote https://repository_name.com

Enter fullscreen mode Exit fullscreen mode

View the remote repo URL

git remote -v

Enter fullscreen mode Exit fullscreen mode

Get more info about remote repo

git remote show origin

Enter fullscreen mode Exit fullscreen mode

Push changes to the remote repository

git push

Enter fullscreen mode Exit fullscreen mode

Pull changes from remote repo

git pull

Enter fullscreen mode Exit fullscreen mode

Check remote branches that git is currently tracking

git branch -r

Enter fullscreen mode Exit fullscreen mode

Fetch remote repo changes

git fetch

Enter fullscreen mode Exit fullscreen mode

Current commit logs of the remote repo

git log origin/main

Enter fullscreen mode Exit fullscreen mode

Merge remote repo with the local repo

git merge origin/main

Enter fullscreen mode Exit fullscreen mode

Get the contents of remote branches in Git without automatically merging

git remote update

Enter fullscreen mode Exit fullscreen mode

Push a new branch to the remote repository

git push -u origin branch_name

Enter fullscreen mode Exit fullscreen mode

Remove a remote branch in git

git push --delete origin branch_name

Enter fullscreen mode Exit fullscreen mode

GIT rebase

(transfer completed work from one branch to another using git rebase)

git rebase branch_name

Enter fullscreen mode Exit fullscreen mode

Force a push request in git:(VERY DANGEROUS)

git push -f

Enter fullscreen mode Exit fullscreen mode

Git tips and tricks

Blank commits

git commit --allow-empty -m "yooo"

Enter fullscreen mode Exit fullscreen mode

Prettify Logs

git log --pretty=oneline --graph --decorate

Enter fullscreen mode Exit fullscreen mode

Clean up local branches

git config --global fetch.prune true

Enter fullscreen mode Exit fullscreen mode
  • you can clean up local branches that have been merged
git branch --merged master | grep -v "master" | xargs -n 1 git branch -d

Enter fullscreen mode Exit fullscreen mode

File that specifies intentionally untracked files that Git should ignore

.gitignore

Enter fullscreen mode Exit fullscreen mode

Happy coding!!

Follow @aviyelHQ or sign-up on Aviyel for early access if you are a project maintainer, contributor, or just an Open Source enthusiast.

Join Aviyel's Discord => Aviyel's world

Twitter =>[https://twitter.com/AviyelHq]

Discussion (6)

Collapse
kdemetter profile image
De Metter Kenny • Edited on

One I recently discovered and really like is git switch.
git switch
and in particular
git switch -c

It allows you to move all the uncommited changes you've done so far to a new branch.
This is really useful when you are working on something and then realize you really should have created a seperate branch for your changes

Collapse
vinayhegde1990 profile image
Vinay Hegde • Edited on

Good cheatsheet of commands but one can accidentally delete the live branch if it's not named master but main or production.

An easy way is to exclude it like:

egrep -iv '(master|main|production)'
Enter fullscreen mode Exit fullscreen mode

In the cleanup local branches section

Collapse
ramkumar_rk_r profile image
Ram Kumar

Awesome one.

Collapse
onlyzeus profile image
Zeus

Woah

Collapse
akhik profile image
Akhik Dutta

Thanks, i think this will be of so much help to meπŸ™‚

Collapse
russelmelroydsouza8 profile image
RusselMelroydsouza8

Very Useful