Explain the basics of git in detail

Source: Internet
Author: User
Tags sha1 sha1 hash sha1 hash algorithm using git version control system git commands
This article is about Git basics, people who don't understand git basics or are interested in the basics of git, so let's take a look at this article, okay, cut the crap and get to the point.

Objective

This article explains in detail the techniques and methods used by Git commands.

Body

1.Git Introduction

The birth of Git is really an interesting story, we know that that year Linus created the open source of Linux, since then, the development of Linux systems, has now become the largest server system software. (Please don't be confused about Linus and Linux)

But as Linux continues to grow, it needs a variety of versioning, at first Linus with his younger brothers using the BitKeeper (Commercial version control system), and then, for some reason bitkeeper companies don't let them use it, So Linus himself spent two weeks to write git and open source (BitKeeper has been crying in the toilet), Amitabha, thanks to BitKeeper don't let Linus they use, otherwise we will not have so good git, bloggers will not write this blog.

In the years that followed, there were github,coding and other sites that could use Git to store, and Git's status became irreplaceable, and if you were a developer, you wouldn't be using Git.

Here, we'll start with a diagram to explain git.
Working principle:

1. Workspace: Workspace, execute git Add * command to commit changes to staging area, execute git pull command to drag remote repository data to current branch and merge, execute git checkout [branch-name] Switch Branch

2, Index: Staging Area, execute git commit-m ' description ' command to commit the changes to the warehouse area (current branch)

3. Repository: Warehouse area (or local warehouse), execute GIT push origin master submit to remote Repository, execute git clone address will clone remote repository to local

4. Remote Storage: A warehouse similar to github,coding and other websites

Note: The actual operation command and the above command will be different, here is to explain clearly the relationship between the command and the warehouse.

1.1 Git Terminology

Term definition warehouse (Repository) A warehouse includes all version information, all branches, and tag information. Every copy of the repository in Git is complete. The warehouse allows you to get a copy of your work from it. Branching (Branches) a branch means a separate code line with its own historical information. You can generate a new branch from the existing code, which is completely independent of the remaining branches. The default branch is often called Master. The user can select a branch and choose a branch to execute the command git checkout branch. tags (tags) a tag refers to the state of a particular point in a branch. Tagging makes it easy to switch to the state of the tag, such as January 25, 2009, when the Code status commit (commit) on the testing branch commits the code, the warehouse creates a new version. This version can be re-acquired in the future. Each submission includes the author and submitter, and the author and submitter can be different person revisions (Revision) used to represent a version status of the code. git identifies different versions by using the ID represented by the SHA1 hash algorithm. Each SHA1 ID is a string of 160-bit long, 16 binary identifiers: The latest version can be obtained through head. Previous versions can be obtained through "head~1", and so on.

1.2 Ignoring a specific file

You can configure GIT to ignore specific files or folders. These configurations are placed in the. gitignore file. This file can exist in different folders and can contain different file matching modes.
For example, the. Gitignore content can be as follows:

Ignore a file Npm-debug.log Ignore folder dist/node_modules/.idea/

At the same time Git also provides a global configuration, core.excludesfile.

After ignoring the file or folder Git will not submit the content.

1.3 use. Gitkeep to track empty folders

git ignores empty folders. If you want version control to include empty folders, it is customary to place the. gitkeep file under an empty folder. In fact, there is no specific request for file names. Once you have a file in an empty folder, the folder is within version control.

1.4 Configuration

# show the current git config $ git config--list# edit git config file, just configure the user information directly look at the following two lines of command can be git config-e [--global]# set the user information when the code is submitted, Whether to add the global--global in its own discretion, is generally directly set global. In addition, the user mailbox needs to pay attention to the best use of gmail,qq can also, need to be consistent with your remote warehouse or your contribution is not logged in the remote repository of $ git config [--global] user.name "[name]" $ git config [--global] user.email "[Email address]"

The settings file for Git is. Gitconfig, which can be in the user's home directory (global configuration) or under the project directory (project configuration).

Personally feel that git needs to learn carefully, although it is a tool but not easy to confuse themselves, I hope this blog can help you at some point, so you probably understand how git works and the basic commands to string up. So let's talk about Git's important basic commands.

2.Git Installation

Git-for-window

Download install this needless to say ....

3. Create a warehouse

# Create a folder in the current directory $ mkdir [project-name]# new git code library in the current directory $ git init# create a new directory, initialize it to git code base $ git init [project-name]# download a project and its entire Code history (each branch commits a record, etc.) $ git clone [url]

Git init will appear after the. git folder, there is a configuration file, if there is no GIT bash inside the input Ls-lah can see

For information on how to associate Git and remote repositories, such as Coding,github, you can read these two articles:
Git links to your GitHubCoding Help Center

4. Submission of documents

4.1 First time push

# Add all files of the current directory to staging area $ git add *# submit staging area to the warehouse area $ git commit-m [message]# for remote git renamed to origin$ git remote add Origin Git@github.com:ab cd/tmp.git# Push This modification, this is the first push need to add-u, then push can be directly git push  origin master,origin is a remote git name, this can be defined by itself, but generally with origin, Master is the default branch, if it is not submitted in the master branch need to write clearly branch name $ git push-u Origin Master

After the first push succeeds, you can look at the following command:

# Add the specified file to staging area $ git add [file1] [file2] ... # Add the specified directory to staging area, including subdirectories $ git add [dir]# Add all files of the current directory to staging area $ git add *# before adding each change, will require confirmation for the same Multiple changes in the file can be implemented by a sub-commit $ git add-p# Delete workspace file, and this deletion will be put into staging area $ git rm [file1] [file2] ... # Stop tracking the specified file, but the file will remain in the workspace $ git rm--cached [fi le]# renaming the file and placing this renaming into staging area $ git mv [file-original] [file-renamed]# commits staging area to the warehouse area $ git commit-m [message]# commits staging area the specified file to the warehouse area $ git com MIT [File1] [file2] ...-m [message]# submission workspace changes since last commit, direct to warehouse area $ git commit-a# submit when displaying all diff information $ git commit-v# use a new Commi T, instead of the last commit if the code does not have any new changes, it is used to overwrite the commit information of the last commit $ git commit--amend-m [message]# redo the last commit and include a new change to the specified file $ git commit--amend [ FILE1] [file2] ... # Commit changes to remote warehouse $ Git push origin master# pull remote change to local warehouse default auto merge Git pull Origin master

If we just maintain their own small project, the above command is enough, a person in the master branch to toss on the how to Toss

5. Branch

But if it is a multi-person collaboration, the charm of git begins to come out, everyone has a branch of their own, each working on their own branch without interference. Specifically look at this: Git tutorial-Create a merge branch

# List all local branches $ git branch# list all remote branches $ git branch-r# list all local branches and remote branch $ git branch-a# create a new branch, but still stay on the current branch $ git branch [branch-name] # Create a new branch and switch to that branch $ git checkout-b [branch]# Create a new branch, point to specify commit$ git branch [branch] [commit]# Create a new branch, build a trace relationship with the specified remote branch $ git BR Anch--track [branch] [remote-branch]# switch to the specified branch and update workspace $ git checkout [branch-name]# switch to previous branch $ git checkout-# establish tracking relationship, in existing branch with Specify the remote branch between $ git branch--set-upstream [branch] [remote-branch]# merge the specified branch into the current branch, if there is a conflict need to manually merge the conflict (that is, manually edit the file to save it), and then add, Commit re-commit $ git merge [branch]# Select a commit, merge into current branch $ git cherry-pick [commit]# Delete branch $ git branch-d [branch-name]# Delete remote branch $ gi T push Origin--delete [branch-name]$ git branch-dr [remote/branch]

6. Tags

The role of the label is mainly used to do version fallback, about the version fallback, which is one of the highlights of Git, has played the function of regret medicine ·

# list all tag$ git tag# create a new tag in the current commit$ git tag [tag]# create a new tag in the Specify commit$ git tag [tag] [commit]# delete local tag$ git tag-d [tag]# Delete remote tag$ git push origin:refs/tags/[tagname]# view tag information $ git show [tag]# commit specified tag$ git push [remote] [tag]# commit all tag$ git pus h [Remote]--tags# Create a new branch, point to a tag$ git checkout-b [branch] [tag]

7. Regret medicine

Think about after you finish writing n file code, commit to the local repository, suddenly found the entire application crashed! What's the whole? Git gave us somehow.
opportunity to:

# Restore Staging area specified file to workspace $ git checkout [file]# restore a commit file to staging area and workspace $ git checkout [commit] [file]# recover staging Area All files into workspace $ git checkout . # fallback to the previous version, in Git, with the HEAD representing the current version $ git reset--hard head^# reset Staging Area The specified file, consistent with the last commit, but the workspace is unchanged $ git reset [file]# resets staging area with the workspace, with the previous The second commit remains consistent $ git reset--hard# resets the current branch pointer to the specified commit while resetting the staging area, but the workspace is unchanged $ git reset [commit]# resets the current branch's head to the specified commit, while resetting the staging area and workspace , with the specified commit consistent $ git reset--hard [commit]# resets the current head for the specified commit, but keeps the staging area and workspace unchanged $ git reset--keep [commit]# creates a commit, All changes that are used to revoke the specified commit# will be offset by the former and applied to the current branch $ git revert [commit]# temporarily remove uncommitted changes and later move the git stash$ git stash pop

This time the role of the label is reflected, because the commit number is too lengthy, it is too cumbersome to remember the label we are equivalent to the custom commit number

8. File information

# show the version history of the current branch $ git log# show commit history, and each commit changed file $ git log--stat# search commit history, According to the keyword $ git log-s [keyword]# shows all changes after a commit, each commit occupies a line of $ git log [tag] HEAD--pretty=format:%s# shows all the changes after a commit, its "mention Cross description "must meet search criteria $ git log [tag] HEAD--grep feature# show a file version history, including file Rename $ git log--follow [file]$ git whatchanged [file]# display specified File related every time diff$ git log-p [file]# Show last 5 commits $ git log-5--pretty--oneline# Show all submitted users, sort by commits $ git shortlog-sn# show who the specified file is  At what time did you modify the $ git blame [file]# show staging area and workspace differences $ git diff# show staging area and the difference of the previous commit $ git diff--cached [file]# show the difference between the workspace and the current branch's latest commit $ Git diff head# shows the difference between two commits $ git diff [First-branch] ...  [second-branch]# shows how many lines of code you wrote today $ git diff--shortstat "@{0 Day ago}" # shows the metadata and content changes of a commit $ git show [commit]# show a file that has been changed by a commit git Show--name-only [commit]# shows the contents of a file when a commit, git show [Commit]:[filename] 

9. Other commands

Command description git blame filepath git blame clearly record a file's change history and change people, is simply to see the back pot man's weapon, FilePath is need to view the file path git status shows the changed file Git reflog show the current branch of the Last several submissions

Postscript

To be thoroughly proficient in using git, there are many commands to remember, at least hundreds of, but in daily use, the commands involved in this article should be sufficient, and the usual commands that are missing are welcome to add. In addition, sincerely hope you can refer to the article a good read, Ruan teacher and teacher Liao summed up in place. Many of the commands in this article are also summarized by the two teachers who use them.

Related recommendations:

Common phrases for Git

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.