Learn Git Commands on the go ! Git Cheat Sheet for Beginners

Git is an efficient version control system that is used to track changes in any set of files generally used by programmers to collaborate with each other. Here are some git commands every programmer must know. As some of these commands are used only once some of us might forget it sometimes. Therefore, I have compiled a GIT Cheatsheet that can help you on-the-go. From GIT installation to GIT synchronization all the commands are at your disposal. 

Learn Git Commands on the go ! Git Cheat Sheet for Beginners

Git is an efficient version control system that is used to track changes in any set of files generally used by programmers to collaborate with each other. Here are some git commands every programmer must know. As some of these commands are used only once some of us might forget it sometimes. Therefore, I have compiled a GIT Cheatsheet that can help you on the go. From GIT installation to GIT synchronization all the commands are at your disposal. You'll also find a cheat sheet pdf at the end of this article, which can be very helpful if you don't want to go to the internet every time. So.. let's start.

GIT INSTALLATION


You can download or install Git or GitHub or any other version control system that uses the git ecosystem. 

SETTING UP SSH CONNECTION FROM YOUR LOCAL MACHINE TO REMOTE 

To create a ssh key use this in your terminal.

ssh-keygen -t rsa -b 4096 -C your@email.com

Check if your Key Pair is generated

ls -al ~/.ssh

It is very important that you never reveal your private key. Always use the ".pub" public key for example id_rsa.pub.

Now Copy the public ssh key and paste it to your GitHub or git lab ssh key using this in Windows

clip < ~/.ssh/id_rsa.pub # For Windows

or

cat ~/.ssh/id_rsa.pub # For Linux

Now you are connected to the remote server from the local workstation

You can test your connection using 

ssh -T git@github.com

GIT SETUP AND CONFIGURATION

To Set up your user name :

git config --global user.name "YOUR_NAME"

Setting up user email ID :

git config --global user.email "YOUR_EMAIL_ID"

INITIALIZING GIT

If you want to Initialize Existing Directory as Git Repository 
Go to that directory and Run the terminal/or command line there

git init

To clone Existing Git Repository using the URL.

git clone [url]

GIT STAGING & OPERATIONS [Most of Daily Use GIT COMMANDS]

It shows which files have been changed since your last commit, in your working directory.

git status

Staging or adding all changes made in a file for future commit.

git add [file]

Staging or adding all changes made in a folder for future commit.

git add [folder]

Unstage or remove a file from staging or adding for future commit

git reset [file]

If you want to find out unstaged changes  between your index and working directory

git diff [file]

If you want to find out the difference of what is changed but not committed

git diff --staged

commit your staged or added content as a new commit or version then the previous version

git commit -m “[descriptive message]”

Remove file from the staging area and  the working directory

git rm [file]

if you are working on some feature but you are bored and you want to work on a different feature then you can keep the changes in the stash for future use.

git stash

If you want yo get the previous work you have added on stash then use

git stash pop

If you want to list all the states

git stash list

if you discard the specific stash

git stash drop

UNDOING CHANGES IN GIT

Create a new commit that undoes all of the changes made in the previous commit, then apply it to the current branch.

git revert [commit sha]​

You can find all your commit SHA's using

git log

GIT BRANCHING AND MERGING

To list all the local branches and see the active branch. The currently active branch shall have " * " appended next to the branch's name.

git branch

'-a' to the above code shall list all the banches in the remote as well as local repository

git branch [-a]

To create a new branch which is referenced to the current HEAD 

 git branch [branch_name]

If you want to switch to a certain branch and check it our in your working directory use

git checkout [branch_name]

Use git "merge branch" If you want to merge the specified branch into the current branch 

git merge [branch]

If you want to remove a branch if it's already merged with another branch

git branch -d [name]

REVIEWING YOUR GIT FLOW

You already know that "git log " is used to list all the commits, what [-n 10] does is its gonna show you last 10 commits.

git log [-n count]

If you want to see commits on one branch but not in another branch, If A and B are two branches

git log branchB..branchA

It gives you git commit review with reference labels and a history graph and one commit per line

git log --graph --decorate

If you want  to list all the git commit messages to condensed in one line then use

git log --oneline

Synchronizing your Local GIT repositories with the Remote Repository

Fetch changes from the remote, but not update tracking branches.

git fetch [remote]

Fetch changes from the remote and merge the current branch with its upstream.

git pull [remote]

Push local changes to the remote. Use --tags to push tags.

 git push [--tags] [remote]

Push local branch to remote repository. Set its copy as an upstream.

 git push -u [remote] [branch] 

Files