Git Basic to Advance Commands

Parmeet SinghParmeet Singh
11 min read

Basic Before Git Commands

Git-GIT, which stands for Global Information Tracker, is a powerful and widely-used version control system commonly used for software development and other collaborative projects.

Git hub-GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere.

Git 3 Stage Archiecture

Getting Started With GIT - Studytonight

Git Basics Commands

-git init : This is the first command that you used to make a new Empty Git Directory

For Example: Here we created a directory Git_Repo after creating Git_Repo we changed the directory to Git_Repo by using the cd command.Inside Git_Repo Directory we executed the command git init to make it empty git repository

-git add <filename>: git add command is used to add file to staging area.Every time we add or update any file in our project, it is required to forward updates to the staging area. In Parallel we can use git add . to add all file content at once in the staging area.

For Example: First, we created the file File1.txt in the git repo Git_Repo by Vim editor after creating the file we added File1.txt to the staging area by the git add command.

-git commit : git commit is used to record the changes in the repository. When we want to share these changes to the version control system, committing allows us to record these changes.

For Example: After adding our file File1.txt to the staging area by the git add command we use git commit to record these changes in a git repo. Here -m is used to write the commit message on the command line.

-git log :

The git log is a record of commits. A git log contains the following data:

  • A commit hash, which is a 40 character checksum data generated by SHA (Secure Hash Algorithm) algorithm. It is a unique number.

  • Commit Author metadata: The information of authors such as author name and email.

  • Commit Date metadata: It's a date timestamp for the time of the commit.

  • Commit title/message: It is the overview of the commit given in the commit message.

-git branch : This command is used to demonstrate available branches in repository

For Example:

-git checkout: The git checkout commands let you create and switch to a new branch. You can not only create a new branch but also switch it simultaneously with a single command.

For Example :

Here in this example, we are going to create a new branch in Git_Repo

Here git checkout -b option is a convenience flag that performs run git branch <new-branch>operation before running git checkout <new-branch>.

For Switching an existing Branch we simply use the git checkout command.

-git branch -d <branch name>: The git branch d command can be used in two formats. Another format of this command is git branch D. The 'git branch D' command is used to delete the specified branch.

Adding Remote URL to GIT to Perform Basic Push and Pull Operations

Working Flow

Git Remote - javatpoint

-git remote

lets you create, view, and delete connections to other repositories.

-git remote -v: Git remote support a specific option -v to show the URLs that Git has stored as a short name.-v stands for verbose. We can use --verbose in place of -v. Here no URL is Assigned now

Adding Remote URL by Token

Step 1: How to Generate Token

-Access GIT HUB go to Settings

-Go to Developer Settings

-Go to Tokens(classic)->Generate new token(classic), on Next Screen Enter Your GITHUB Password

-Add Note, Enter Token Expiration, Select Scopes, according to Scope You can give access to remote user what remote user can access by using token.

-Copy and Save You Token

Adding Remote Connection By -git remote add

-git remote add <variable> https://<token>@github.com/<Your Github repo>

After Establising connection check connection is established or not by command -git remote -v

-git push/pull :

-git push: The git push command is used to upload local repository content to a remote repository.

For Example:

File1.txt,File2.txt we want to push from our local Git Repo to Git-Hub Repo we use the command git push origin branch

Here we need to Push File1, and File2 from our directory

Git-Hub Repo before Push

Git-Hub After Push

-git pull :The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content.

For Example:

Here we need to Pull File3,File4.txt from Git-Hub Repository

Before Pull Our Local Git Repo LookLike this

After Pull Our local Git Repo LookLike this

Alternatively, we can use the git fetch command to fetch all the remote branches

Git Advance Commands

-git merge <branch name>: It joins two or more development histories together. The git merge command facilitates you to take the data created by the git branch and integrate them into a single branch. Git merge will associate a series of commits into one unified history. Generally, git merge is used to combine two branches.

Git Merge and Merge Conflict

For Example: Here we are in the Branch_1 branch we want to merge File5 into the master branch

Before Merge

Branch_1 branch bc64ee4 node is to be merged into the master branch.

Use Command git checkout <branch> branch where you want to merge a new node In this case we are going to merge a new node in the master branch from the Branch_1 branch.

New Node is added to the master Branch, Head is Changed.

Merge Conflict

When two branches are trying to merge, and both are edited at the same time and in the same file, Git won't be able to identify which version to take for changes. Such a situation is called a merge conflict.

-git blame <filename>: If you need to examine the content of any file line by line, you need to use git blame. It helps you to determine who made the changes to a file.

-git cherry-pick: Suppose you are working with a team of developers on a medium to large-sized project. Some changes were proposed by another team member and you want to apply some of them to your main project, but not all. Managing the changes between several Git branches can become a complex task, and you don't want to merge a whole branch into another branch. You only need to pick one or two specific commits. To pick some changes to your main project branch from other branches is called cherry-picking.

Git Cherry-pick

For Example :

Here in branch Branch_1, u want to add Feature1.txt in your master branch by the use of cherry-picking u can directly add that Feature1.txt(Feature1.txt, Feature2.txt, Feature3.txt included in one feature) node to your master node.

Using Command -git cherry-pick<commit-id>

-git rebase: It is an alternative to git merge command. Merge is always a forward-changing record. Comparatively, rebase is a compelling history-rewriting tool in git. It merges the different commits one by one.

Instead of doing a git merge, I’ll do a git rebase. What rebase will do is take all of the commits on your feature branch and move them on top of the master commits. Behind the scenes, git is blowing away the feature branch commits and duplicating them as new commits on top of the master branch (remember, under the hood, commit objects are immutable and immovable). What you get with this approach is a nice clean tree with all your commits laid out nicely in a row, like a timeline. Easy to trace.

For Example:

As you can see we created a branch my_new_cool_feature and we want to rebase it to our master branch. In my_new_cool_feature there are two features.

After Rebasing

Branch my_new_cool_feature successfully rebased to the master branch.

Git Cheat Sheet

1. Git configuration

  • Git config
    Get and set configuration variables that control all facets of how Git looks and operates.
    Set the name:
    $ git config --global user.name "User name"
    Set the email:
    $ git config --global user.email "youremail@gmail.com"
    Set the default editor:
    $ git config --global core.editor Vim
    Check the setting:
    $ git config -list

  • Git alias
    Set up an alias for each command:
    $ git config --global alias.co checkout
    $ git config --global alias.br branch
    $ git config --global alias.ci commit
    $ git config --global alias.st status

2. Starting a project

  • Git init
    Create a local repository:
    $ git init

  • Git clone
    Make a local copy of the server repository.
    $ git clone

3. Local changes

  • Git add
    Add a file to staging (Index) area:
    $ git add Filename
    Add all files of a repo to staging (Index) area:
    $ git add*

  • Git commit
    Record or snapshots the file permanently in the version history with a message.
    $ git commit -m " Commit Message"

4. Track changes

  • Git diff
    Track the changes that have not been staged: $ git diff
    Track the changes that have staged but not committed:
    $ git diff --staged
    Track the changes after committing a file:
    $ git diff HEAD
    Track the changes between two commits:
    $ git diff Git Diff Branches:
    $ git diff < branch 2>

  • Git status
    Display the state of the working directory and the staging area.
    $ git status

  • Git show Shows objects:
    $ git show

5. Commit History

  • Git log
    Display the most recent commits and the status of the head:
    $ git log
    Display the output as one commit per line:
    $ git log -oneline
    Displays the files that have been modified:
    $ git log -stat
    Display the modified files with location:
    $ git log -p

  • Git blame
    Display the modification on each line of a file:
    $ git blame <file name>

6. Ignoring files

  • .gitignore
    Specify intentionally untracked files that Git should ignore. Create .gitignore:
    $ touch .gitignore List the ignored files:
    $ git ls-files -i --exclude-standard

7. Branching

  • Git branch Create branch:
    $ git branch List Branch:
    $ git branch --list Delete a Branch:
    $ git branch -d Delete a remote Branch:
    $ git push origin -delete Rename Branch:
    $ git branch -m

  • Git checkout
    Switch between branches in a repository.
    Switch to a particular branch:
    $ git checkout
    Create a new branch and switch to it:
    $ git checkout -b Checkout a Remote branch:
    $ git checkout

  • Git stash
    Switch branches without committing the current branch. Stash current work:
    $ git stash
    Saving stashes with a message:
    $ git stash save ""
    Check the stored stashes:
    $ git stash list
    Re-apply the changes that you just stashed:
    $ git stash apply
    Track the stashes and their changes:
    $ git stash show
    Re-apply the previous commits:
    $ git stash pop
    Delete a most recent stash from the queue:
    $ git stash drop
    Delete all the available stashes at once:
    $ git stash clear
    Stash work on a separate branch:
    $ git stash branch

  • Git cherry pic
    Apply the changes introduced by some existing commit:
    $ git cherry-pick

8. Merging

  • Git merge
    Merge the branches:
    $ git merge
    Merge the specified commit to currently active branch:
    $ git merge

  • Git rebase
    Apply a sequence of commits from distinct branches into a final commit.
    $ git rebase
    Continue the rebasing process:
    $ git rebase -continue Abort the rebasing process:
    $ git rebase --skip

  • Git interactive rebase
    Allow various operations like edit, rewrite, reorder, and more on existing commits.
    $ git rebase -i

9. Remote

  • Git remote
    Check the configuration of the remote server:
    $ git remote -v
    Add a remote for the repository:
    $ git remote add Fetch the data from the remote server:
    $ git fetch
    Remove a remote connection from the repository:
    $ git remote rm
    Rename remote server:
    $ git remote rename
    Show additional information about a particular remote:
    $ git remote show
    Change remote:
    $ git remote set-url

  • Git origin master
    Push data to the remote server:
    $ git push origin master Pull data from remote server:
    $ git pull origin master

10. Pushing Updates

  • Git push
    Transfer the commits from your local repository to a remote server. Push data to the remote server:
    $ git push origin master Force push data:
    $ git push -f
    Delete a remote branch by push command:
    $ git push origin -delete edited

11. Pulling updates

  • Git pull
    Pull the data from the server:
    $ git pull origin master
    Pull a remote branch:
    $ git pull

  • Git fetch
    Download branches and tags from one or more repositories. Fetch the remote repository:
    $ git fetch< repository Url> Fetch a specific branch:
    $ git fetch
    Fetch all the branches simultaneously:
    $ git fetch -all
    Synchronize the local repository:
    $ git fetch origin

12. Undo changes

  • Git revert
    Undo the changes:
    $ git revert
    Revert a particular commit:
    $ git revert

  • Git reset
    Reset the changes:
    $ git reset -hard
    $ git reset -soft:
    $ git reset --mixed

13. Removing files

  • Git rm
    Remove the files from the working tree and from the index:
    $ git rm <file Name>
    Remove files from Git But keep the files in your local repository:
    $ git rm --cached
13
Subscribe to my newsletter

Read articles from Parmeet Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Parmeet Singh
Parmeet Singh

I know technical challenges can broaden our technical knowledge.