Git common command sorting

Time:2021-6-21

preface

Git will not be introduced here. The table is afraid when it sees the command line. These are the common commands, which basically meet more than 99% of the project requirements. It is very practical.

text

  1、 Common git commands

    1.1  git clone <url> [<directory>]

From the remote library clone code to the local directory, directory is used to specify a new directory name

    1.2  git status

Check the local status, you can view the current branch, new or modified files

    1.3  git checkout <file>

Restore the changes made to a file. You can also “git checkout.” restore all changes

    1.4  git add <file>

Add files to the cache and track new files. You can also “git add.” add all files to the cache

    1.5  git commit -m “commit message”

Submit the code to the local code base. It is highly recommended to commit once after completing a small function, and to replace a large commit with multiple commits, which is also convenient for subsequent code merging and review.

    1.6  git push

Synchronize local library code to remote library( Push failure will be followed by code merging)

    1.7  git branch

List available branches, GIT branch – a list all branches of local and remote libraries

    1.8  git checkout <branch>

Switch branches

    1.9  git log [-<number>]

View the local commit record. Number can be displayed by limiting log

    1.10  git diff [<file>]

Compare the differences between the current file and the staging area file

    1.11  git pull

Extract update code from remote library to local library

2、 Code revocation

2.1 undo the local code that has not been added or committed

Git checkout < File > undoes a single file, or “git checkout.” undoes all local modifications.

2.2 undo the local code that has been added but not committed

Git reset < File > undoes a single file, and git reset undoes all files

2.3 undo local committed but not pushed code

Git reset — hard head ~ 1 cancels the last commit and returns the code to the last commit. Note that the code is discarded( Do not add — hard can only revoke commit without rollback code)

2.4 undo the code that has been committed and pushed

      git revert <commit id>

2.5 modify the comment that has been committed

Git commit — amend, which is usually opened with vim. After modification, just exit WQ.

3、 Merge with branch code

Applicable scenario: when git pull fails or the remote database is known to have been updated, note that the local commit first and then rebase.

    3.1  git pull –rebase

Extract the update code from the remote library locally and try to merge the code. Unlike merge, no new commit record will be generated. When merging code, you need to be very careful not to lose the code of other partners!

    3.2  git add <file>

In case of conflict, solve the conflict. After the solution is completed, GIT add

    3.3  git rebase –continue

After all conflicts are resolved and added, continue to rebase, do not commit. You may need to continue and add operations many times until all the conflicts are merged.

    3.4  git push

Synchronize the local code library code to the remote library.

4、 Merging of different branch codes

Applicable scenario: merge the code from the development branch (Development) to the master branch (production)

    4.1  git cherry-pick <commit id>

Apply the commit modification of another branch to the current branch. Commit ID supports cross branch and short commit ID (the first 8 bits, such as 247d27c6). Of course, it also supports the same branch.

    4.2  git add <file>

In case of conflict, solve the conflict. After the solution is completed, GIT add

    4.3  git rebase –continue

After all conflicts are resolved and added, continue to rebase, do not commit. You may need to continue and add operations many times until all the conflicts are merged.

    4.4  git push

Synchronize the local code library code to the remote library.

5、 Code temporary storage

Applicable scenario: the function is not completed and you don’t want to commit, but you need to restore the code to the last commit (such as emergency bug repair)

    5.1  git stash

Temporarily store uncommitted code and restore all changes

    5.2  git stash pop

Restore the code of the last stash

6、 Others

    6.1  Android

Version management of Android studio is very easy to use. It is very convenient to switch branches, compare and modify, merge and submit code.

    6.2  iOS

Xcode version management can be used to detect and compare, commit to local, and then merge through the command line.

Many students also use sourcetree

6.3 git official Chinese version

      https://git-scm.com/book/zh

The above is the GIT commonly used command collation, friends in need can refer to.