Git series: analysis of common instructions

Git series blog:

  1. Git series: Summary of GitHub warehouse building and remote synchronization steps, link
  2. Git series: detailed instructions for getting started, link
  3. Git series: One Finger Zen for common operations, link
  4. Git series: analysis of common instructions, link
  5. Git series: common error handling, link
  6. Git series: Troubleshooting in common scenarios, link

This article summarizes the analysis of common Git concepts and the comparison of instructions.

Command Discrimination


The workspace, temporary storage area, and version library are stupidly unclear?

  • Workspace: directly visible and operable files locally, content that can be directly modified by the editor
  • Temporary storage area: The content under the .git directory is mainly the directory index index, which is not directly visible. After git add, it is placed here to save the modification record
  • Repository: divided into local and remote, the result after git commit is placed in the local repository, and the result after git push is pushed to the remote repository

What is the relationship between trunk and branch?

  • The same: all belong to the code management in the same project
  • The difference: similar to the concept of a tree, the trunk is shown in the blue part of the picture, and the branches are shown in the purple and green parts of the picture
    • The trunk, often called master/trunk, is often used for the official delivery version of the main body, and the trunk is often merged into various features of the branch
    • Branches, often called branches, are often used for development branches, and branches are differentiated from the trunk
    • For advanced understanding, please refer to the link: branch of rookie tutorial
      insert image description here

Three lines are commonly used in daily development:

  • master trunk
    • Official release, overall inspection, remote synchronization, multiple people
  • dev branch
    • Stage release, developers, remote synchronization, multiplayer
  • bug branch
    • Local debugging, personal development

The core process of Git is to collaborate based on branches, trunk merge, and then delete branches. This is where it differs from SVN. SVN is based on folder management version, Git is based on branch processing, and there is only one warehouse in general.

Analysis of git fetch/merge/pull/checkout

  • git fetch
    • Pull the latest commit content and its commit id from the remote warehouse to the local branch. Whether it is merged or not is another matter (you can use the merge command to merge into the local branch after checking by the user)
    • Update the various trunks and branches of the remote repo, and store the records FETCH_HEADin
  • git merge
    • If you operate git merge locally, merge the fetched remote results into the local work
    • If you operate git merge remotely, the results of the local submission will be merged into the remote warehouse. Merge the remote to the local separately after the fetch, command:git merge FETCH_HEAD
    • Specifically, git merge merges the fetch tracking into the local branch, which will change the content of the local branch
  • git pull
    • The essence is equivalent to git fetch + git mergedirectly merging after pulling down the latest content of the remote host.
  • git checkout
    • The common function is to switch branches
    • When the remote branch has the same name as the new branch, the progress of the latest remote branch is automatically pulled and merged into the new branch

For advanced understanding, please refer to the link:

  1. Usage of git fetch/pull/checkout
  2. stackoverflow: the difference between pull and fetch
  3. Introduction to Git fetch, pull, checkout and push

Practical application:

  • Requirement: Pull the cloned remote dev branch to the local as a baseline for further development.
    • Note that the remote master is empty, and the local master is also empty. After cloning to the local, the default is the master branch.
  • solve:
    • Method 1: git checkout
      • git checkout -b dev origin/dev,Equivalent to:git checkout dev
      • Function: Get the remote copy to the newly created local branch with the same name.
      • Process: Create a new dev local branch, switch the dev branch from the current master branch, and pull the origin/dev changes to the dev branch
    • Method 2: git fetch + git merge
      • git fetch --all# Get the remote warehouse and change it to the local warehouse, put it in HEAD, but don't merge it into any local branch
      • git merge origin/dev# Merge the remote dev branch changes into the local current master branch

Discrimination between git merge and cherry-pick

  • merge is to merge all commits by branch
  • Cherry-pick is to merge the changes before and after a certain commit according to the commit id, which is more atomic

When the version rolls back to reset, --hard and --soft are the difference between hard rollback and soft rollback?

  • Command: git reset --hard HEAD^, hard roll back to the previous version, delete and undo all the changes made in the previous version
  • Command: git reset --soft HEAD^, soft rollback to the previous version, only the head pointer is moved, and the latest version before the rollback is not deleted

When viewing logs, what is the difference between git log and git reflog?

  • Instruction analysis: log and reflog

  • log

    • Display the commit log version submission including the remote warehouse. If you roll back to the previous commit id version, only the version id after rollback will be displayed
      • View commit records, such as instructions: git log -gorgit log --pretty=oneline
  • reflog

    • The display shows the operation log of the local warehouse, including the deleted commit operation and reset operation, such as instructions: git reflogandgit reflog --pretty=oneline
  • Note that adding after the command --pretty=onelinecan simplify the output, only displaying the commit version number and the comment information at the time of submission; -git is displayed in the form of a text table

What is the difference between git diff and status?

  • diff, display the file difference between the temporary storage area and the work area, you can view the modification of the existing file before and after, but you cannot view the diff of the newly added file (the difference between the newly added file diff itself is 0 and 1, you can directly view the new file, no need Compare the differences before and after), note that git addyou cannot view after
  • status, to obtain the list of modified files, you can check the status of file modification (the specific modification content will not be told), and you can distinguish whether it is a modification in the work area or a modification in the temporary storage area

Relevant information


  1. Git series: detailed instructions for getting started, link
  2. Git series: One Finger Zen for common operations, link

Guess you like

Origin blog.csdn.net/qq_17256689/article/details/129309059