Learn Git Commnds in 30 minutes(Beginners)

Prabhakar Kumar
11 min readNov 23, 2020

--

Git is a distributed version-control system for tracking changes in source code during software development.

Version Control System / Source Code Manager

A version control system (abbreviated as VCS) is a tool that manages different versions of source code. A source code manager (abbreviated as SCM) is another name for a version control system.

VCS INFO

There are a number of Version Control Systems out there. This alone should prove that version control is incredibly important. Three of the most popular version control systems are:

1.Git
2.Subversion
3.Mercurial

There are two main types of version control system models:

  1. The Centralized model — all users connect to a central, master repository
  2. The Distributed model — each user has the entire repository on their computer

Commit (snapshot):-Git thinks of its data like a set of snapshots of a mini file system. Every time you commit, or save the state of your project in Git, it basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot.

Repository (repo): A directory that contains your project work, as well as a few files (hidden by default in Mac OS X) which are used to communicate with Git. Repositories can exist either locally on your computer or as a remote copy on another computer.

Working Directory: The files that you see in your computer’s file system. When you open your project files up on a code editor, you’re working with files in the Working Directory. This is in contrast to the files that have been saved (in commits!) in the repository. When working with Git, the Working Directory is also different from the command line’s concept of the current working directory which is the directory that your shell is “looking at” right now.

Checkout: When content in the repository has been copied to the Working Directory. It is possible to checkout many things from a repository; a file, a commit, a branch, etc.

Staging Area or Staging Index or Index: A file in the Git directory that stores information about what will go into your next commit. You can think of the staging area as a prep table where Git will take the next commit. Files on the Staging Index are poised to be added to the repository.

SHA: A SHA is basically an ID number for each commit. It is a 40-character string composed of characters (0–9 and a–f) and calculated based on the contents of a file or directory structure in Git. “SHA” is shorthand for “SHA hash”. A SHA might look like this:

ex:- e2adf8ae3e2e4ed40add75cc44cf9d0a869afeb

Branch:- A branch is when a new line of development is created that diverges from the main line of development. This alternative line of development can continue without altering the main line. Going back to the example of save point in a game, you can think of a branch as where you make a save point in your game and then decide to try out a risky move in the game. If the risky move doesn’t pan out, then you can just go back to the save point. The key thing that makes branches incredibly powerful is that you can make save points on one branch, and then switch to a different branch and make save points there, too.

Git Commands

  1. Git Init:-

Use the git init command to create a new, empty repository in the current directory.

$ git init

Running this command creates a hidden .git directory. This .git directory is the brain/storage center for the repository. It holds all of the configuration files and directories and is where all of the commits are stored.

  1. Git Clone:- The git clone command is used to create an identical copy of an existing repository.
Git Clone
$ git clone <path-to-repository-to-clone>

This command:

  • takes the path to an existing repository
  • by default will create a directory with the same name as the repository that’s being cloned
  • can be given a second argument that will be used as the name of the directory
  • will create the new repository inside of the current working directory

The git status command will display the current status of the repository.

  1. Git Status:-
$ git status

I can’t stress enough how important it is to use this command all the time as you’re first learning Git. This command will:

  • tell us about new files that have been created in the Working Directory that Git hasn’t started tracking, yet
  • files that Git is tracking that have been modified
  • a whole bunch of other things that we’ll be learning about throughout the rest of the course ;-)

4.Git Log:-

Let’s do a quick recap of the git log command. The git log command is used to display all of the commits of a repository.

$ git log

By default, this command displays:

  • the SHA
  • the author
  • the date
  • and the message

…of every commit in the repository. I stress the “By default” part of what Git displays because the git log command can display a lot more information than just this.

Git uses the command line pager, Less, to page through all of the information. The important keys for Less are:

  • to scroll down by a line, use j or
  • to scroll up by a line, use k or
  • to scroll down by a page, use the spacebar or the Page Down button
  • to scroll up by a page, use b or the Page Up button
  • to quit, use q
  1. git log — oneline:-

To recap, the -oneline flag is used to alter how git log displays information:

$ git log --oneline

This command is used for:-

  • lists one commit per line
  • shows the first 7 characters of the commit’s SHA
  • shows the commit’s message

6.git log — stat:-

$ git log --stat

This command:

  • displays the file(s) that have been modified
  • displays the number of lines that have been added/removed
  • displays a summary line with the total number of modified files and lines that have been added/removed

Git log Vs Git log --stat

Git log Vs git log — — stat

7.git log -p:-

To recap, the p flag (which is the same as the -patch flag) is used to alter how git log displays information:

$ git log -p

This command adds the following to the default output:

  • displays the files that have been modified
  • displays the location of the lines that have been added/removed
  • displays the actual changes that have been made

8.Git Show:-

The git show command will show only one commit. So don't get alarmed when you can't find any other commits - it only shows one. The output of the git show command is exactly the same as the git log -p command. So by default, git show displays:

  • the commit
  • the author
  • the date
  • the commit message
  • the patch information

However, git show can be combined with most of the other flags we've looked at:

  • -stat - to show the how many files were changed and the number of lines that were added/removed
  • p or -patch - this the default, but if -stat is used, the patch won't display, so pass p to add it again
  • w - to ignore changes to whitespace

9.Git Add:-

The git add command is used to move files from the Working Directory to the Staging Index.

$ git add <file1> <file2> … <fileN>

This command:

  • takes a space-separated list of file names
  • alternatively, the period . can be used in place of a list of files to tell Git to add the current directory (and all nested files)

The untracked HTML, CSS, and JavaScript files add from the Working Directory to the Staging Index.

10.Git Commit:-

The git commit command takes files from the Staging Index and saves them in the repository.

$ git commit# Shortcut command to creates a commit of all the staged changes and takes an inline commit message.$git commit -am "commit message"

This command:

  • will open the code editor that is specified in your configuration
  • (check out the Git configuration step from the first lesson to configure your editor)

Inside the code editor:

  • a commit message must be supplied
  • lines that start with a # are comments and will not be recorded
  • save the file after adding a commit message
  • close the editor to make the commit

Bypass The Editor With The Flag

TIP: If the commit message you’re writing is short and you don’t want to wait for your code editor to open up to type it out, you can pass your message directly on the command line with the m flag:$ git commit -m "Initial commit" In the example above, the text "Initial commit" is used as the commit message. Be aware that you can't provide a description for the commit, only the message part.

Changing The Last Commit

You’ve already made plenty of commits with the git commit command. Now with the -amend flag, you can alter the most-recent commit.

$ git commit --amend

If your Working Directory is clean (meaning there aren’t any uncommitted changes in the repository), then running git commit --amend will let you provide a new commit message. Your code editor will open up and display the original commit message. Just fix a misspelling or completely reword it! Then save it and close the editor to lock in the new commit message.

11.Git Diff:-

The git diff command is used to see changes that have been made but haven't been committed, yet:

$ git diff

This command displays:

  • the files that have been modified
  • the location of the lines that have been added/removed
  • the actual changes that have been made

The changes in this section were used to demo the output of git diff. They were not committed to the repository. If you’d like, you can definitely commit the changes to the repository, just know that your git log will look slightly different from mine because it includes this extra commit.

11. Git Tag:-

The git tag command is used to add a marker on a specific commit. The tag does not move around as new commits are added.

$ git tag -a beta

This command will:

  • add a tag to the most recent commit
  • add a tag to a specific commit if a SHA is passed

Create tag with some name git tag “tag name” example : git tag v1.0 git tag -a v1.0 -m “ver 1 of ..” (to create annotated tags)

Display or Show tags git tag git show v1.0 git tag -l “v1.*”

Push tags to remote git push origin v1.0 git push origin — tags git push — tags (to push all tags at once)

Delete tags (if required only) to delete tags from local : git tag -d v1.0 git tag — delete v1.0 To Delete tags from remote : git push origin -d v1.0 git push origin — delete v1.0 git push origin :v1.0 To Delete multiple tags at once: git tag -d v1.0 v1.1 (local) git push origin -d v1.0 v1.1 (remote)

Adding A Tag To A Past Commit

Running git tag -a v1.0 will tag the most recent commit. But what if you wanted to tag a commit that occurred farther back in the repo's history?

All you have to do is provide the SHA of the commit you want to tag!

$ git tag -a v1.0 a87984

12.Git Branch:-

The git branch command is used to manage branches in Git:

# to list all branches
$ git branch
# to create a new "footer-fix" branch
$ git branch footer-fix
# to delete the "footer-fix" branch
$ git branch -d footer-fix

This command is used to:

  • list out local branches
  • create new branches
  • remove branches

13.Git Merge:-

The git merge command is used to combine branches in Git:

$ git merge <other-branch>

There are two types of merges:

  • Fast-forward merge — the branch being merged in must be ahead of the checked out branch. The checked out branch’s pointer will just be moved forward to point to the same commit as the other branch.
  • the regular type of merge
  • two divergent branches are combined
  • a merge commit is created

Merge Conflict

  • A merge conflict happens when the same line or lines have been changed on different branches that are being merged. Git will pause mid-merge telling you that there is a conflict and will tell you in what file or files the conflict occurred. To resolve the conflict in a file:
  • locate and remove all lines with merge conflict indicators
  • determine what to keep
  • save the file(s)
  • stage the file(s)
  • make a commit
  • Be careful that a file might have merge conflicts in multiple parts of the file, so make sure you check the entire file for merge conflict indicators — a quick search for <<< should help you locate all of them.

In Git there are 2 ways to integrate changes from one branch to another

  • Git Merge
  • Git Rebase

Git Merge

  • Is a non-destructive operation
  • Existing branches are not changed in any way
  • Creates a new merge commit in the feature branch

Git Rebase:- $ git rebase <branch-name>

  • Moves the entire feature branch to begin on the tip of the master branch
  • Re-writes the project history
  • We get much cleaner and linear project history

14.Git Revert:-

  • The git revert command is used to reverse a previously made commit:
  • $ git revert <SHA-of-commit-to-revert>
  • This command:
  • will undo the changes that were made by the provided commit
  • creates a new commit to record the change

15.Git Reset:-

  • The git reset command is used erase commits:

$ git reset <reference-to-commit>

It can be used to:

  • move the HEAD and current branch pointer to the referenced commit
  • erase commits with the -hard flag
  • moves committed changes to the staging index with the -soft flag
  • unstages committed changes -mixed flag
  • Typically, ancestry references are used to indicate previous commits. The ancestry references are:
  • ^ – indicates the parent commit
  • ~ – indicates the first parent commit
  • Reset vs Revert
  • At first glance, resetting might seem coincidentally close to reverting, but they are actually quite different. Reverting creates a new commit that reverts or undos a previous commit. Resetting, on the other hand, erases commits!
  • ⚠️ Resetting Is Dangerous ⚠️You’ve got to be careful with Git’s resetting capabilities. This is one of the few commands that lets you erase commits from the repository. If a commit is no longer in the repository, then its content is gone.To alleviate the stress a bit, Git does keep track of everything for about 30 days before it completely erases anything. To access this content, you’ll need to use the git reflog command.

Click on Articles to see what other views might have in store or Further Research✨

1.Git Cheat Sheet

2.Git Learning School

3.Git Community Book

--

--