6

51 git commands that you'll ever need to get started with Git

 2 years ago
source link: https://dev.to/aviyel/51-git-commands-that-youll-ever-need-to-get-started-with-git-34d5
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 modeExit fullscreen mode

Setting up your git username

git config --global user.name "pramit"

Enter fullscreen modeExit fullscreen mode

Setting up email

git config --global user.email "[email protected]"

Enter fullscreen modeExit fullscreen mode

Caching credential

git config --global credential.helper cache

Enter fullscreen modeExit fullscreen mode

Initialize repository

git init

Enter fullscreen modeExit fullscreen mode

Adding filename to staging area

git add file_name

Enter fullscreen modeExit fullscreen mode

Adding all the files to the staging area

git add .

Enter fullscreen modeExit fullscreen mode

Add only certain files to the staging area

example add all files starting with "comp"

git add comp*

Enter fullscreen modeExit fullscreen mode

Checking repo Status

git status

Enter fullscreen modeExit fullscreen mode

Commit changes

git commit

Enter fullscreen modeExit fullscreen mode

Commit changes with a message in it

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

Enter fullscreen modeExit 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 modeExit fullscreen mode

To see the commit history

git log

Enter fullscreen modeExit fullscreen mode

Commit history and the following file changes

git log -p

Enter fullscreen modeExit fullscreen mode

Show specific commit in git

git show commit_id

Enter fullscreen modeExit fullscreen mode

Statistics about changes

git log --stat

Enter fullscreen modeExit fullscreen mode

Changes made before committing them using diff

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

Enter fullscreen modeExit fullscreen mode

Removing tracked files

git rm filename

Enter fullscreen modeExit fullscreen mode

Rename files in git

git mv oldfilename newfilename

Enter fullscreen modeExit fullscreen mode

Revert unstaged changes

git checkout file_name

Enter fullscreen modeExit fullscreen mode

Revert staged changes

git reset HEAD filename
git reset HEAD -p

Enter fullscreen modeExit fullscreen mode

Modify and add changes to the most recent commit

git commit --amend

Enter fullscreen modeExit fullscreen mode

Rollback the last commit

git revert HEAD

Enter fullscreen modeExit fullscreen mode

Revert a previous commit

git revert comit_id_here

Enter fullscreen modeExit fullscreen mode

Create a new branch

git branch branch_name

Enter fullscreen modeExit fullscreen mode

List branch in git

git branch

Enter fullscreen modeExit fullscreen mode

Create a branch and switch it Immediately

git checkout -b branch_name

Enter fullscreen modeExit fullscreen mode

Delete a branch in git

git branch -d branch_name

Enter fullscreen modeExit fullscreen mode

Merge

git merge branch_name

Enter fullscreen modeExit fullscreen mode

Commit log as a graph in git

git log --graph --oneline

Enter fullscreen modeExit fullscreen mode

Commit log as a graph in git of all branches

git log --graph --oneline --all

Enter fullscreen modeExit fullscreen mode

Abort a conflicting merge

git merge --abort

Enter fullscreen modeExit fullscreen mode

Adding a remote repository

git add remote https://repository_name.com

Enter fullscreen modeExit fullscreen mode

View the remote repo URL

git remote -v

Enter fullscreen modeExit fullscreen mode

Get more info about remote repo

git remote show origin

Enter fullscreen modeExit fullscreen mode

Push changes to the remote repository

git push

Enter fullscreen modeExit fullscreen mode

Pull changes from remote repo

git pull

Enter fullscreen modeExit fullscreen mode

Check remote branches that git is currently tracking

git branch -r

Enter fullscreen modeExit fullscreen mode

Fetch remote repo changes

git fetch

Enter fullscreen modeExit fullscreen mode

Current commit logs of the remote repo

git log origin/main

Enter fullscreen modeExit fullscreen mode

Merge remote repo with the local repo

git merge origin/main

Enter fullscreen modeExit fullscreen mode

Get the contents of remote branches in Git without automatically merging

git remote update

Enter fullscreen modeExit fullscreen mode

Push a new branch to the remote repository

git push -u origin branch_name

Enter fullscreen modeExit fullscreen mode

Remove a remote branch in git

git push --delete origin branch_name

Enter fullscreen modeExit fullscreen mode

GIT rebase

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

git rebase branch_name

Enter fullscreen modeExit fullscreen mode

Force a push request in git:(VERY DANGEROUS)

git push -f

Enter fullscreen modeExit fullscreen mode

Git tips and tricks

Blank commits

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

Enter fullscreen modeExit fullscreen mode

Prettify Logs

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

Enter fullscreen modeExit fullscreen mode

Clean up local branches

git config --global fetch.prune true

Enter fullscreen modeExit 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 modeExit fullscreen mode

File that specifies intentionally untracked files that Git should ignore

.gitignore

Enter fullscreen modeExit 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]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK