Git Commands Cheatsheet

Please Read This 👇🏾


Definition: Git is a version tacking system which is use to track the changes/versions in a source code while development of any software and Github is the cloud based website where all the source codes repository can be maintained.

In this post I will share some important commands which might be helpful while using git from CLI.

1) Installation : In most updated current versions of Linux systems included git by default. But if you are not sure use below command to install git in your system.

For rpm based system : sudo dnf install git-all or sudo yum install git-all

For debian based system : sudo apt-get install git-all 

For arch based system : sudo pacman -Sy git

To check installed version : git --version

For help : git --help

2) Configuration :  If you are new and you want to configure git for yourself before commit git , run the below commands to configure it for yourself. All the system wide configuration will under /etc/gitconfig & user configuration can be find under ~./config/git/config .

a) Configuring git for user :

 git config --global  "your_name"

git config --global "your_mail_id"

b) View setting for a user :

git config --list --show-origin

3) Start Using Git : After successfully register yourself for git, its time to start using git.

a) To initialize a project git init "project_name"

b) To clone a project : git clone "repository_path"

4) Changes in Git  : Git projects have a staging area, which is an index file in your Git directory, that stores the changes that will go into your next commit. To record a modified file you therefore firstly need to add it to the index (stage it). The git commit command then stores the current index in a new commit.

a) To add file contents to the index :  git add "file_path"

b) To Remove files from the working tree and from the index : git rm "file_path"
c) To revert your repository to specific state : git reset "file_path"

d) To Show the working tree status : git status "project_path"
e) To Record changes to the repository : git commit

f) To Restore working tree files :  git restore "file_path"

g) To move or rename a file, a directory, or a symlink :  git mv "source" "destination"

5) Branching , Merging , Checkout Commands : Branching commands are used to list, create, or delete branches. Merging commands are used to join two or more development histories together. And Switch command are used to switch branches or restore working tree files

a) To create an new branch : git branch "branch_name"
b) To rename a branch name : git branch -m | -M "branch1" "branch2"

c) To remove a branch :  git branch -D "branch_name"

d) To List all remote and local branches : git branch -a
e) To List all remote : git branch -r "remote path"

f) Merge branches on top of the current branch :   git merge "branch1" "branch2"

g) Merge  branch1 into the current branch without making a new commit automatically :

 git merge --no-commit "branch1"

h)  Merge branch1  into the current branch with auto remove conflicts with current branch by using ours strategy:

git merge -s ours "branch1"

i) Checks out the branch : git checkout "branch1"

j)  Revert file with N number back in a branch : git checkout "branch_name"~2 "file_name"  

k)  Restore a deleted file :   git checkout "file_name"

l) Swtich current branch to specified branch : git checkout -b "branch_name"

6) Pull & Push Commands : Pull command is use to fetch from and integrate with another repository or a local branch and push command is use to update remote refs along with associated objects.

a) Update the remote-tracking branches for the repository you cloned from, then merge one of them into your current branch:  git pull    or  git pull origin

 b)  Merge into the current branch1 the remote branch2 git pull branch1 branch2

c)  Push local changes to remote : git push [options] "remote_path"

d) Fetch changes from the remote : git fetch "remote_path"

Sharing is Caring 😀


Post a Comment