Introduction of git
Git is version control system for tracking changes in computer files and coordinating work on those files among multiple people.
Git is free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.
1. Create
$ git init
Create a new local repository.
The git init
command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new, empty repository. Most other Git commands are not available outside of an initialized repository, so this is usually the first command you’ll run in a new project.
$ git clone <url>
Clone an existing repository.
git clone
is a Git command line utility which is used to target an existing repository and create a clone, or copy of target repository. Cloning a local or remote repository. Clone a bare repository. Using shallow options to partially clone repositories. Git URL syntax and supported protocols.
2. Local Changes
$ git status
Changes files in your working directory.
The git status
command displays the state of the working directory and the stating area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git. Status output does not show you any information regarding the committed project history.
$ git diff
Changes to tracked files.
git diff
is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more. The git diff
command is often used along with git status
and git log
to analyze the current state of a Git repo.
$ git diff <filename|filepath>
Show are list out the change of specific file as per comparison to previous commit.
$ git add . | $ git add ..
Add all current changes to the next commit.
This command updates the index using the current content found in the working tree, to prepare the content staged for the next commit. It typically adds the current content of existing paths as a whole, but with some options it can also be used to add content with only part of the changes made to the working tree files applied, or remove paths that do not exist in the working tree anymore.
The “index” holds a snapshots of content of the working tree, and it is the snapshot that is taken as the contents of the next commit. Thus after making any changes to the working tree, and before running the commit command, you must use the add command to add any new or modified files to the index.
This command can be performed multiple times before a commit. It only adds the content of the specified file(s) at the time the add command is run; if you want to subsequent changes included in the next commit, then you must run git add
again to add the new content to the index.
The git status
command can be used to obtain a summary of which files have changes that are staged for the next commit.
The git add
command will not add ignored files by default. If any ignored files were explicitly specified on the command line, git add
will fail with a list of ignored files. Ignored files reached by directory recursion on filename globbing performed by Git (quote your globs before the shell) will be silently ignored. The git add
command can be used to add ignored files with the -f (force) option.
$ git add FILENAME
Add particular file changes to the next commit.
$ git add -p <File>
Add some changes in to the next commit.
$ git commit -m 'Commit message'
Add commit message for stagging files (For green colored files)
Use the given as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs. The -m option is mutually exclusive with -c, -C, and -F.
$ git commit -a
Commit all local changes in tracked files.
Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.
$ git commit --amend
Change the last commit or revert
_Don’t amend publish commits._
$ git commit --amend -m "an updated commit message"
Change the last commit or update the last commit message
_Don’t amend publish commits._
$ git commit --amend --no-edit
The –no-edit flag will allow you to make the amendment to your commit without changing its commit message.
_Don’t amend publish commits._
3. Commit History
$ git log
Show all commits, starting with newest
A Git log is a running record of commits. A full log has the following pieces: A commit hash (SHA1 40 character checksum of the commits contents). Because it is generated based on the commit contents it is unique. Commit Author metadata: The name and email address of the author of the commit./n/nShows the commit logs./n/nThe command takes options applicable to the git rev-list command to control what is shown and how, and options applicable to the git diff-* commands to control how the changes each commit introduces are shown.
$ git log -p <file>
Show changes over time for a particular file.
$ git blame <file>
Who changes, what and when in .
Annotates each line in the given file with information from the revision which last modified the line. Optionally, start annotating from the given revision.
When specified one or more times, -L restricts annotation to the requested lines.
The origin of lines is automatically followed across whole-file renames (currently there is no option to turn the rename-following off). To follow lines moved from one file to another, or to follow lines that were copied and pasted from another file, etc., see the -C and -M options.
The report does not tell you anything about lines which have been deleted or replaced; you need to use a tool such as git diff or the “pickaxe” interface briefly mentioned in the following paragraph.
Apart from supporting file annotation, Git also supports searching the development history for when a code snippet occurred in a change. This makes it possible to track when a code snippet was added to a file, moved or copied between files, and eventually deleted or replaced. It works by searching for a text string in the diff. A small example of the pickaxe interface that searches for blame_usage:
$ git log --pretty=oneline -S'blame_usage'
5040f17eba15504bad66b14a645bddd9b015ebb7 blame -S <ancestry-file>
ea4c7f9bf69e781dd0cd88d2bccb2bf5cc15c9a7 git-blame: Make the output
$ git log --oneline --graph --decorate
Show all commit history with graph.
4. Branches and Tags
$ git branch
Show current working branch.
Git Branch. This document is an in-depth review of the git branch command and a discussion of the overall Git branching model. Branching is a feature available in most modern version control systems. … Instead of copying files from directory to directory, Git stores a branch as a reference to a commit.
$ git branch -av
List all existing branches.
$ git checkout <branch name>
Switch HEAD branch or Change branch.
Git Checkout. This page is an examination of the git checkout command. … The git checkout command operates upon three distinct entities: files, commits, and branches. In addition to the definition of “checkout” the phrase “checking out” is commonly used to imply the act of executing the git checkout command.
$ git checkout -b <new branch name>
Create and switch new branch as per the current branch.
$ git checkout -b destinationBranchName sourceBranchName
Create or check out new branch from other branch.
Lets say you are in “develop” branch and you want to create new branch from another branch without switching branch.so you can create new branch from other branch from develop branch, example:
$ git branch
develop*
feature/login
feature/register
current branch is “develop”. Now you want to create branch from “feature/login”. Here is you can fire it.
$ git checkout -b feature/login feature/social-login.
The base branch of feature/social-login is feature/login. Booommmmm.
$ git checkout --track <remote/branch>
Create a new tracking branch based on a remote branch.
It will only create ‘abranch’, not a branch with a different name.
$ git branch -m newBranchName
Rename current branch with newbranchName.
$ git branch -m old-name new-name
If you are on a different branch,You can renaming other branch from this command.
Move/rename a branch and the corresponding reflog.
$ git branch -d <branchname>
Delete local branch.
Delete a branch. The branch must be fully merged in its upstream branch, or in HEAD if no upstream was set with –track or –set-upstream-to.
$ git tag YourTagVersion
Create new tag.
$ git tag -a YourTagVersion -m "tag message"
Create tag with message.
Creating an annotated tag in Git is simple. The easiest way is to specify -a when you run the tag command. The -m specifies a tagging message, which is stored with the tag. If you don’t specify a message for an annotated tag, Git launches your editor so you can type it in.
`$ git show YourTagVersion~
Show tag command with commits.
Shows one or more objects (blobs, trees, tags and commits).
For commits it shows the log message and textual diff. It also presents the merge commit in a special format as produced by git diff-tree --cc
For tags, it shows the tag message and the referenced objects.
For trees, it shows the names (equivalent to git ls-tree with –name-only).
For plain blobs, it shows the plain contents.
The command takes options applicable to the git diff-tree
command to control how the changes the commit introduces are shown.
This manual page describes only the most frequently used options.
$ git tag | $ git tag -l | $ git tag --list
Listing the available tags in Git is straightforward
$ git tag -l "Tag pattern*"
You can also search for tags that match a particular pattern
The Git source repo, for instance, contains more than 500 tags. If you’re only interested in looking at the 1.8.5 series, you can run this: $ git tag -l "v1.8.5*"
$ git checkout YourTagVersion
Checkout specific tag.
You can directly checkout your tag version from firing this command.
$ git checkout -b NewBranchName YourTagVersion
Checkout new branch from specific version tag.