preface

Life is your intimate, set to pay without early evening.

Git command summary

Zhihu Column && Jianshu Special Topic: Front-end Aggressor (Zhihu) Front-end Aggressor (Jianshu)

The body of the

1. Introduction of Git

The birth of Git is an interesting story. We know that Linus created the open source Linux, and Linux has evolved since then to become the largest server system software. (Please don’t confuse Linus with Linux.)

But as Linux got bigger and bigger, it needed all kinds of version control, so Linus and his boys used BitKeeper, and then for some reason the BitKeeper company stopped them from using it. It took Linus two weeks to write Git and open source it (BitKeeper has been crying in the toilet), amitabha Buddha. Luckily BitKeeper won’t let Linus use Git, otherwise we wouldn’t have Git as good as it is today, and bloggers wouldn’t be writing this blog.

In the later years, github,coding and other websites that can use Git storage gradually emerged, and the status of Git became irreplaceable. If you are a developer but still don’t know how to use Git, you are too out.

Here’s a diagram to illustrate how Git works:

Git principle

  • Workspace: indicates the Workspacegit add *The command commits the change to the staging area and executes itgit pullCommand to pull and merge data from the remote warehouse to the current branchgit checkout [branch-name]Switch branch
  • Index: temporary storage areaGit commit -mThe command commits the change to the warehouse (current branch)
  • Repository: a warehouse area (or local Repository), executedgit push origin masterCommit to the remote repository and executeGit clone addressClone the remote repository locally
  • Remote: A repository provided by websites such as Github and coding

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

1.1 the Git terms

The term define
Repository A repository contains all version information, all branches, and tag information. In Git, every copy of the repository is complete. The repository allows you to get copies of your work from it.
Branches A branch means a separate code line with its own history. You can generate a new branch from existing code that is completely separate from the remaining branches. The default branch is usually called master. You can select a branch and run commands in the branchgit checkout branch.
Tags A marker refers to the state of a branch at a particular point in time. The tag makes it easy to switch to the state of the tag, such as the state of the code on the Testing branch on January 25, 2009
Commit After submitting the code, the repository creates a new version. This version can be retrieved later. Each submission includes an author and a committer, and the author and committer can be different people
Revision Used to represent the state of a version of code. Git identifies different versions with ids represented by the SHA1 hash algorithm. Each SHA1 ID is a 160-bit hexadecimal identifier string. The latest version is available via HEAD. Previous versions can be obtained with “HEAD~1”, and so on.

1.2 Ignore specific files

Git can be configured to ignore specific files or folders. These configurations are in the.gitignore file. This file can exist in different folders and can contain different file matching patterns. For example, the. Gitignore content could look like this:

Ignore file npm-debug.log Ignore folder dist/ node_modules/.idea/Copy the code

Git also provides global configuration of core.excludesFile.

Git will not commit the contents of any subsequent files or folders.

1.3 Use.gitkeep to track empty folders

Git ignores empty folders. If you want to include empty folders in version control, the convention is to place.gitkeep files in empty folders. There are no specific requirements for file names. Once there are files in an empty folder, the folder is in version control.

1.4 configuration

$Git config -e [--global] $Git config -e [--global] Whether to add global -global is optional, usually set to global directly. In addition, users need to pay attention to gmail,QQ is also ok, $git config [--global] user.email $git config [--global] user.email $git config [--global] user.email $git config [--global] user.email "[email address]"Copy the code

Git’s Settings file is.gitconfig, which can be in the user home directory (global configuration) or in the project directory (project configuration).

Personally, I think Git needs to be learned carefully. Although it is a tool, it is easy to get confused without learning it. I hope this blog can help you at some point, so that you can roughly understand how Git works and put the basic commands together. Git’s basic commands are listed below.

2. The Git installed

Git-for-window

Download and install this without saying….

3. Create a warehouse

$Git init $Git init $Git init $Git init $Git init $Git clone [url] $Git clone [url]Copy the code

Git -lah -lah -lah -lah -lah -lah -lah -lah

How to associate Git with remote repositories, such as Coding, github, etc

4. Submit documents

4.1 First Push

$git commit -m [message] $git commit -m [message [email protected]:abcd/ TMP. $git push -u origin master $git push -u origin master $git push -u origin masterCopy the code

After the first push is successful, you can see the following command:

$git add [file1] [file2]... $git add [dir] $git add * $git add -p $git rm [file1] [file2] $git rm --cached [file] The system has been renamed to $git mv [file-original] [file-renamed] # commit the system to the repository $git commit -m [message] # commit the system to the repository $git commit  [file1] [file2] ... $git commit -v $git commit -v $git commit -v $git commit -- amend-m [message] # amend the last commit, $git commit --amend [file1] [file2]... By default, $git pull origin master is automatically mergedCopy the code

If we were just maintaining our own small projects, the above commands would suffice, and we could do whatever we wanted in the Master branch

Branch of 5.

But git’s magic starts to show when you have multiple people working together, each with their own branch, working on their own branch without interfering with each other. Git tutorial – Creating merge branches

$git branch -r $git branch -r $git branch [branch-name] $git checkout -b [branch] Commit $git branch [branch] [commit] $git branch --track [branch] [remote-branch] $git checkout [branch-name] $git checkout - $git branch --set-upstream [branch] [remote-branch] $git branch --set-upstream [branch] [remote] $git merge [branch] $git merge [branch] $git cherry-pick [commit] $git branch -d [branch-name] # $git push origin --delete [branch-name] $ git branch -dr [remote/branch]Copy the code

6. Label

The function of tag is mainly used to do version rollback. About version rollback, this is also one of the highlights of Git, which plays the function of regret medicine

$git tag [tag] $git tag [commit] # Delete local tag $git tag $git show [tag] # $git push [remote] [tag] $git checkout -b [branch] [tag]Copy the code

7. Regret medicine

Imagine writing N files of code, committing to the local repository, and suddenly the entire application crashes! How to do? Git gives us the opportunity to regret:

$git checkout [commit] [file] $git checkout [file $Git reset --hard HEAD^ # resets the file in the staging area as the last commit. $git reset --hard # set the pointer to the current branch to commit, and reset the staging area. $git reset [commit] $git reset [commit] $git reset --keep [commit] # create a new commit $git revert [commit] # Remove the uncommitted changes temporarily and move to $git Stash $git stash pop laterCopy the code

And that’s where the tag comes in, because the COMMIT number is so verbose, it’s so hard to remember that with the tag we’re essentially customizing the COMMIT number

8. File information

$git log -- $git log -- $git log $git log -s [keyword] # $git log [tag] HEAD --pretty=format:%s # $git log [tag] HEAD --grep feature # --follow [file] $git whatchanged [file] # diff $git log -p [file --pretty --oneline # display all submitted users $git diff $git diff $git diff $git diff $git diff $git diff [first-branch] $git diff [first-branch] $git diff [first-branch] $git diff --shortstat "@{0 day ago}" $git show [commit] # $git show [commit]:[filename] $git show [commit]:[filename]Copy the code

8. Other commands

The command instructions
git blame filepath Git blame specifies the name of the file to which the change was made, and the name of the file to which the change was made
git status The changed file is displayed
git reflog Displays the most recent commits for the current branch

Afterword.

There are hundreds of git commands that you need to remember in order to be fully proficient with git, but this article should be sufficient for daily use. In addition, sincerely hope you can read the reference article, ruan teacher and Liao teacher summed up very in place. Many of the commands in this paper are also summarized by the two teachers.

Reference article:

  • Git tutorial
  • Git command list