Git for beginners

What is Git?
Git is a free, open-source, widely used, and distributed version control system that helps to track changes of any set of files. When comes to Git there are two ways to work with, Command, and Graphical User Interface – GUI. This article explains to you how Git works and the top most commonly used command use all day every day.
How does Git keep track of your records?
There are basically four parts of Git flow you need to remember while working on Git. When you push your code to a remote repository, your code changes pass through these steps.
If you look at the below diagram you can see some familiarize commands/ terms.

When every developer clones a repository or executes the git initialize command, Git will create a Local repository equivalent to a remote repository. Once your code changes good to share with others, always remember to push to the remote repository.
Most commonly use Git commands
HEAD
While working with Git, only one branch can be checked out at a time or another way you can work on one branch at a time, this is what’s called the “HEAD” branch. Also referred to as the “active” or “current” branch.
git config
This command help to set author name and email which to be used in every commit.
git config --global user.name "<Your Name>"
git config --global user.email "<Your Email>"
You can list all the global configuration using below command
git config -l
git init
This command is used to initialized a new repository in your working directory.
git init
git clone
This command is used to obtain a repository from an existing URL.
URL should be HTTPS or SSH. Base on the URL you may have to configure or provide additional credential detail to access the repository.
URL should be in this formathttps => https://github.com/<account-name>/<repository-name>.git
SSH => git@github.com:<account-name>/<repository-name>.git
git clone "<URL>"
git branch
This command lists all the local branches in the current repository
git branch
Also you can create new branch adding branch name at the end of same command.
git branch <branch-name>
To delete a branch you need to add -d as a parameter. Also, you can add -D as force delete
git branch -d <branch-name>
git checkout
This command is used to switch between branches
git checkout <branch-name>
Adding –b parameter will create a new branch and same time switch to new branch.
git checkout -b <branch-name>
git checkout with commit id will move local branch HEAD to given commit id while master still point to latest commit.
git checkout <commit-id>

git reset
This command is used to reset files/commit.
To remove local changes
git reset <file-name>
To remove all local changes
git reset .
adding commit id to command will move HEAD and master to that specific commit and remove all other subsequent commits after onward.
git reset --soft|mixed|hard <commit-id>
You can use HEAD to mention how many commits you want to reset. With ~ symbol you can mention number of commits that need to be considered from HEAD.
git reset HEAD~2

This command is similar to git checkout <commit-id> but HEAD and master will be point to the same commit id given in the command.
git status
This command list all the files that have to be committed
git status
git clean
This command is used to clean local folder.
git clean -xdf
x – all ignored files d – including directories f – force
git log
This command is used to list all the commits made in current branch
git log
git diff
This command is used to show file differences which are still in local folder.
git diff
If you want to compare a file with Stage you can use the same command adding the following parameter.
git diff --staged
To compare with two different branches
git diff <branch-1> <branch-2>
git remote
This command is used to connect your local repository to the remote server. If you start with git init
, then you need this command to connect to the remote repository. Otherwise, git clone
command will create this entry for you.
git remote add <variable name> <remote URL>
git add
This command adds a file from your local folder to the staging area. If you look at the diagram on top, you can see this command is used for what. YOu can use the file name or * to represent all the files.
git add <file-name>
git add *
git commit
This command is used to create a snapshot of files changes in the stage area permanently in the version history.
git commit -m <commit message>
You can skip the stage area while adding -a into this command, But recommend what is to use Stage areas before adding into the version history. You can execute the below command in your local folder
git commit -a -m <commit message>
git push
This command sends the committed changes of your local branch to your remote repository
git push <variable-name> <branch-name>
git pull
This command fetches and merges changes on the remote server to your local branch and working directory
git pull <variable-name> <branch-name>
git pull <repository-url>
git fetch
This command fetches changes on the remote server to your local repository without changing your local folder. Later you can use git merge to update the local folder.
git fetch <variable-name> <branch-name>
git stash
This command helps you to temporary store your modified track file.
You can use following commands to work with stash area.
git stash save
git stash pop
git stash list
git stash drop
git merge
This command merges the specified branch’s history into the current branch
git merge <branch name>

git rebase
This command is used to integrate changes from one branch to another. But the difference from merge command is rebasing flattens the history.
git rebase <branch-name>
