The most complete Git learning tutorial in history

 

 

Git is one of the most advanced distributed version control systems in the world.

                                     —— Git enthusiasts

Git is a "distributed version management tool", which has too many advantages: more convenient Merge, more convenient management, more robust system, less dependence on the network, less "warehouse pollution", etc. , Which makes Git the best way to collaboratively develop code.

This article will introduce more than seventy commonly used Git commands and techniques. Let's take a look at the charm of a Github project that exceeds 1.1W Stars.

Must read

If you have not used Git before, you can learn the Git white tutorial [1] Getting started:

  1. Be sure to test the effect of the command before using it in the working environment to prevent irreparable consequences! Don't come to me with a machete

  2. All commands are git version 2.7.4 (Apple Git-66)under test

  3. Unified concept:

  • Work area: changes (addition and deletion of files and contents)

  • Temporary storage area: Enter the command:, git add 改动的文件名this change will be put into the 'temporary storage area'

  • Local warehouse (abbreviation: local): Enter the command:, git commit 此次修改的描述this change is placed in the 'local warehouse', each commit, I call it a 'version'.

  • Remote warehouse (abbreviation: remote): Enter the command:, git push 远程仓库this change will be put into the 'remote warehouse' (GitHub, etc.)

  • commit-id: output command:, git logthe top line commit xxxxxx, the string behind is commit-id

 

table of Contents

  • Display help information

  • Back to the remote warehouse

  • Reset the first commit

  • View conflict file list

  • Show the difference between the work area and the staging area

  • Show the difference between the staging area and the latest version

  • Show the difference between the staging area, workspace and the latest version

  • Quickly switch to the previous branch

  • Delete the branch that has been merged into master

  • Show the situation of local branches associated with remote warehouses

  • Associate remote branch

  • List all remote branches

  • List local and remote branches

  • View the correspondence between remote branches and local branches

  • Deleted the branch remotely and wanted to delete it locally

  • Create and switch to local branch

  • Create and switch to a local branch from a remote branch

  • Delete local branch

  • Delete remote branch

  • Rename local branch

  • View tags

  • View label details

  • Create tags locally

  • Push tags to remote warehouse

  • Delete local label

  • Remove remote tags

  • Switch back to a label

  • Discard changes to the workspace

  • Recover deleted files

  • Restore the modification of a certain commit by adding a new commit

  • Return to the status of a certain commit and delete the following commit

  • Modify the description of the last commit

  • View commit history

  • Display locally updated git command records of HEAD

  • Modify author name

  • Modify the URL of the remote warehouse

  • Increase remote warehouse

  • List all remote warehouses

  • View changes within two weeks

  • Put a commit from branch A on branch B

  • Alias ​​the git command

  • Store the current changes, but no commit

  • Save the current state, including untracked files

  • Show all stashes

  • Back to a stash state

  • Go back to the state of the last stash and delete this stash

  • Delete all stash

  • Take out the modification of a file from stash

  • Show all tracked files

  • Show all untracked files

  • Show all ignored files

  • Forcibly delete untracked files

  • Forcibly delete untracked directories

  • Show simplified commit history

  • See who wrote a piece of code

  • Export a certain branch to a file

  • Import branch from package

  • Automatically stash before executing rebase

  • From the remote warehouse, according to the ID, pull down a certain state to the local branch

  • Show details of changes in a row

  • Clear .gitignorefile recorded in the file

  • Show all aliases and configs

  • Show ignored files

  • Commit history shows that Branch1 has it, but Branch2 has no commit

  • Show GPG signature in commit log

  • Delete global settings

  • Create and switch to a new branch, and this branch does not have any commit

  • Show the contents of a file in any branch

  • clone down the specified single branch

  • clone latest commit

  • Ignore changes to a file

  • Ignore changes in file permissions

  • List all Git branches in the order of last commit

  • Find relevant content in the commit log

  • Put the specified file in the temporary storage area into the work area

  • Force push

  • git configure http and socks proxy

  • git configure ssh proxy

  • A detailed picture

  • Submit Commit messages gracefully

  • contact me

Command usage

Display help information

git help -g

The command output as below:

The common Git guides are:
   attributes          Defining attributes per path
   cli                 Git command-line interface and conventions
   core-tutorial       A Git core tutorial for developers
   cvs-migration       Git for CVS users
   diffcore            Tweaking diff output
   everyday            A useful minimum set of commands for Everyday Git
   glossary            A Git Glossary
   hooks               Hooks used by Git
   ignore              Specifies intentionally untracked files to ignore
   modules             Defining submodule properties
   namespaces          Git namespaces
   repository-layout    Git Repository Layout
   revisions           Specifying revisions and ranges for Git
   tutorial            A tutorial introduction to Git
   tutorial-2          A tutorial introduction to Git: part two
   workflows           An overview of recommended workflows with Git


'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept.

Back to the remote warehouse

Discard all local changes and return to the state of the remote warehouse.

git fetch --all && git reset --hard origin/master

Reset the first commit

That is, put all the changes back into the work area, and clear all the commits, so that you can resubmit the first commit.

git update-ref -d HEAD

Show the difference between workspace and staging area

Different (different) output work area and temporary area.

git diff

You can also display file changes between any two commits in the local warehouse:

git diff <commit-id> <commit-id>

Show the difference between the staging area and the latest version

The difference between the output staging area and the local latest version (commit) is different.

git diff --cached

Show the difference between the staging area, workspace and the latest version

Differentiated (different) output workspace, staging area, and the most recent local version (commit).

git diff HEAD

Quickly switch to the previous branch

git checkout -

Remove branches that have been merged into master

git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d

Display the status of the local branch associated with the remote warehouse

git branch -vv

Associate remote branch

After the association, git branch -vv can display the associated remote branch name, and Push directly to the remote warehouse: git push, no need to specify the remote warehouse.

git branch -u origin/mybranch

Or add -u parameter when pushing

git push origin/mybranch -u

List all remote branches

The -r parameter is equivalent to: remote

git branch -r

Cite local and remote branches

The -a parameter is equivalent to: all

git branch -a

Build and switch to local branch

git checkout -b <branch-name>

Build from remote branch and switch to local branch

git checkout -b <branch-name> origin/<branch-name>

Remove local branch

git branch -d <local-branchname>

Remove remote branch

git push origin --delete <remote-branchname>

or

git push origin :<remote-branchname>

Rename local branch

git branch -m <new-branch-name>

View tags

git tag

Display the most recent tag of the current branch

git describe --tags --abbrev=0

View label details

git tag -ln

Build tags locally

git tag <version-number>

The default tag is the most recent commit. If you need to specify the commit tag:

$ git tag -a <version-number> -m "v1.0 发布 (描述)" <commit-id>

Push tag to remote warehouse

First of all, we must ensure that the label is built locally before we can push the label to the remote warehouse:

git push origin <local-version-number>

Push all the tags at once and synchronize to the remote warehouse:

git push origin --tags

Remove local label

git tag -d <tag-name>

Remove remote tags

To remove a remote label, you need to remove the local label first, and then execute the following command:

git push origin :refs/tags/<tag-name>

Switch back to a label

Generally, tags will be added before going online, in order to prevent problems after going online, and to quickly roll back to the previous version. The following command returns to the state under a certain label:

git checkout -b branch_name tag_name

Discard changes to the workspace

git checkout <file-name>

Discard all changes:

git checkout .

Recover removed files

git rev-list -n 1 HEAD -- <file_path> # 得到 deleting_commit
git checkout <deleting_commit>^ -- <file_path> # 回到移除文件 deleting_commit 之前的状态

Revert the changes of a certain commit by adding a new commit

git revert <commit-id>

Return to the status of a certain commit, and remove the following commit

Difference from revert: The reset command will erase all commits after a certain commit id

git reset <commit-id> # 默认就是-mixed 参数。
git reset –mixed HEAD^ # 回退至上个版本,它将重置 HEAD 到另外一个 commit, 并且重置暂存区以便和 HEAD 相匹配,但是也到此为止。工作区不会被更改。
git reset –soft HEAD~3 # 回退至三个版本之前,只回退了 commit 的信息,暂存区和工作区与回退之前保持一致。如果还要提交,直接 commit 即可
git reset –hard <commit-id> # 彻底回退到指定 commit-id 的状态,暂存区和工作区也会变为指定 commit-id 版本的内容

Change the description of the last commit

If there are changes in the staging area, the changes in the staging area will also be submitted to the previous commit

git commit --amend

View commit history

git log

View contributors of a piece of code

blame means "blame", you know.

git blame <file-name>

Display locally updated git command records of HEAD

The git commands such as commint, amend, cherry-pick, reset, revert, etc., which are updated every time, will be recorded (unlimited branches), just like the history of the shell. This way you can reset to any operation after updating HEAD, not just to return to the state after a commit under the current branch.

git reflog

Change author name

git commit --amend --author='Author Name <[email protected]>'

Change the URL of the remote warehouse

git remote set-url origin <URL>

Increase remote warehouse

git remote add origin <remote-url>

List all remote warehouses

git remote

View changes within two weeks

git whatchanged --since='2 weeks ago'

Put a commit from branch A on branch B

This process requires the cherry-pick command, refer to [2]

git checkout <branch-name> && git cherry-pick <commit-id>

Alias ​​the git command

Simplified commands

git config --global alias.<handle> <command>
比如:git status 改成 git st,这样可以简化命令
git config --global alias.st status

Save current changes without committing

For details, please refer to Teacher Liao Xuefeng's git tutorial

git stash

Store current status, including untracked files

untracked files: newly created files

git stash -u

Show all stashes

git stash list

Back to a stash state

git stash apply <stash@{n}>

Go back to the state of the last stash and remove the stash

git stash pop

Remove all stash

git stash clear

Take a file change from stash

git checkout <stash@{n}> -- <file-path>

Show all tracked files

git ls-files -t

Show all untracked files

git ls-files --others

Show all ignored files

git ls-files --others -i --exclude-standard

Force removal of untracked files

Can be used to remove newly created files. If no file name is specified, all working untracked files are cleared. The clean command, pay attention to two points:

1. After clean, the removed files cannot be retrieved

2. Does not affect changes to tracked files, only remove untracked files

git clean <file-name> -f

Force removal of untracked directories

Can be used to remove newly created directories. Note: This command can also be used to remove untracked files. See the previous article for details.

git clean <directory-name> -df

Show simplified commit history

git log --pretty=oneline --graph --decorate --all

Export a branch to a file

git bundle create <file> <branch-name>

Import branch from package

Create a new branch, the branch content is the content exported by the git bundle create command above

git clone repo.bundle <repo-dir> -b <branch-name>

Automatically stash before executing rebase

git rebase --autostash

From the remote warehouse, according to the ID, pull down a certain state to the local branch

git fetch origin pull/<id>/head:<branch-name>

Show changes in a row in detail

git diff --word-diff

Clear the files recorded in the gitignore file

git clean -X -f

Show all aliases and configs

Note: config is divided into: current directory (local) and global (golbal) config, the default is the config of the current directory

git config --local --list (当前目录)
git config --global --list (全局)

Show ignored files

git status --ignored

Commit history shows that Branch1 has it, but Branch2 has no commit

git log Branch1 ^Branch2

Show GPG signature in commit log

git log --show-signature

Remove global settings

git config --global --unset <entry-name>

Create and switch to a new branch, and this branch does not have any commit

Equivalent to saving changes, but rewriting commit history

git checkout --orphan <branch-name>

Display the contents of a file in any branch

git show <branch-name>:<file-name>

clone down the specified single branch

git clone -b <branch-name> --single-branch https://github.com/user/repo.git

Ignore changes to a file

Turn off changes to the specified file of track, that is, Git will not record changes to this file

git update-index --assume-unchanged path/to/file

Recover changes to track specified files

git update-index --no-assume-unchanged path/to/file

Ignore changes in file permissions

No longer treat changes in file permissions as changes

git config core.fileMode false

List all Git branches in the order of last commit

The latest is on top

git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/

Find relevant content in the commit log

Find by grep, given-text: the field to be found

git log --all --grep='<given-text>'

Put the specified file in the temporary storage area into the work area

No parameters added, default is -mixed

git reset <file-name>

Force Push

git push -f <remote-name> <branch-name>

 

Published 45 original articles · won praise 2 · Views 5228

Guess you like

Origin blog.csdn.net/xixiaoyaoww/article/details/105037023