51 git commands that you'll ever need to get started with Git 🔥
Tags: github, git, webdev, javascript
Published: 2021-10-20 09:39:50

## 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

```bash
git config -l

```

## Setting up your git username

```bash
git config --global user.name "pramit"

```

## Setting up email

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

```

## Caching credential

```bash
git config --global credential.helper cache

```

## Initialize repository

```bash
git init

```

## Adding filename to staging area

```bash
git add file_name

```
## Adding all the files to the staging area

```bash
git add .

```

## Add only certain files to the staging area

example add all files starting with "comp"

```bash
git add comp*

```

## Checking repo Status

```bash
git status

```

## Commit changes

```bash
git commit

```

## Commit changes with a message in it

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

```

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

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

```

## To see the commit history

```bash
git log

```

## Commit history and the following file changes

```bash
git log -p

```

## Show specific commit in git

```bash
git show commit_id

```

## Statistics about changes

```bash
git log --stat

```

## Changes made before committing them using diff

```bash
git diff
git diff some_file.js
git diff --staged

```

## Removing tracked files

```bash
git rm filename

```

## Rename files in git

```bash
git mv oldfilename newfilename

```

## Revert unstaged changes

```bash
git checkout file_name

```

## Revert staged changes

```bash
git reset HEAD filename
git reset HEAD -p

```

## Modify and add changes to the most recent commit

```bash
git commit --amend

```

## Rollback the last commit

```bash
git revert HEAD

```

## Revert a previous commit

```bash
git revert comit_id_here

```

## Create a new branch

```bash
git branch branch_name

```

## List branch in git

```bash
git branch

```

## Create a branch and switch it Immediately

```bash
git checkout -b branch_name

```

## Delete a branch in git

```bash
git branch -d branch_name

```

## Merge

```bash
git merge branch_name

```

## Commit log as a graph in git

```bash
git log --graph --oneline

```

## Commit log as a graph in git of all branches

```bash
git log --graph --oneline --all

```

## Abort a conflicting merge

```bash
git merge --abort

```

## Adding a remote repository

```bash
git add remote https://repository_name.com

```

## View the remote repo URL

```bash
git remote -v

```

## Get more info about remote repo

```bash
git remote show origin

```

## Push changes to the remote repository

```bash
git push

```

## Pull changes from remote repo

```bash
git pull

```

## Check remote branches that git is currently tracking

```bash
git branch -r

```

## Fetch remote repo changes

```bash
git fetch

```

## Current commit logs of the remote repo

```bash
git log origin/main

```

## Merge remote repo with the local repo

```bash
git merge origin/main

```

## Get the contents of remote branches in Git without automatically merging

```bash
git remote update

```

## Push a new branch to the remote repository

```bash
git push -u origin branch_name

```

## Remove a remote branch in git

```bash
git push --delete origin branch_name

```

## GIT rebase

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

```bash
git rebase branch_name

```

## Force a push request in git:(VERY DANGEROUS)

```bash
git push -f

```

---

## Git tips and tricks

### Blank commits

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

```

### Prettify Logs

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

```

### Clean up local branches

```bash
git config --global fetch.prune true

```

- you can clean up local branches that have been merged

```bash
git branch --merged master | grep -v "master" | xargs -n 1 git branch -d

```

### File that specifies intentionally untracked files that Git should ignore

```bash
.gitignore

```

Happy coding!!

Follow [@aviyelHQ](https://twitter.com/AviyelHq) or [sign-up](https://aviyel.com/discussions) 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](https://discord.gg/TbfZmbvnN5)

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