Git use mechanism

Git’s regions are divided into workspaces, cache areas, and local warehouse areas

Workspace: is the root directory of the local development project

Buffer: A Stage or Index is set up in the repository to interact directly with files in the workspace. The first choice for submitting or rolling back files in the workspace is through the staging area. The final location for submitting files in the repository is the Branch. There is a Master branch by default when you create a repository.

Local repository: After adding a local repository for the project, a hidden directory “.git “is generated in the workspace, as shown below. The.git directory is the local repository for the current workspace

  • Git add Files: Copy the current working file to the staging area
  • Git commit: Create a snapshot of the file in the cache and commit it to the local repository
  • Git reset-files: Undoes the last git add files
  • Git reset: Resets all cache files
  • Git checkout — files: Overwrites files from the staging area to the working directory, which is used to discard local changes.

The reset operation discards the selected target file from the staging area without affecting files in any other staging area, which is equivalent to a self-refresh action

Related commands:

  • Git commit -a executes git add and git commit successively, first copying the file from the working directory to the cache, and then from the cache to the repository
  • Git checkout HEAD — Files does the opposite, synchronizing both workspace and staging to the state in the current branch, the state of the last commit
  • Git diff to see what parts of files that have not yet been provisionally updated; The difference between the workspace and the staging area in the current state.
  • Git diff-cached looks for the difference between the staged file and the snapshot from the last commit; That is, the difference between the staging area and the branch in the current state
  • Git diff HEAD This is the difference between workspace and branch in the current state

Undo operation:

  • Git commit — Amend reverses the last commit and recommits the cache file
  • Git checkout –file pulls the cache file and replaces it with a workspace file
  • Git reset HEAD –file Pulls the last file committed to the repository into the cache without affecting the workspace

Uncached to cached:

  • git add file

The cache is committed to the local repository

  • git commit -m

Commit directly from workspace to local repository

  • Git commit-am

The local library falls back to the cache

  • Git reset – soft hash value
  • git reset -soft origin/master

The local library falls back to the initial state of the file

  • Git reset-hard hash value

  • Note here that the fetch is usually performed once, ensuring that the local version is the latest version of Origin, and then rolled back. (Best of all, there is no conflict and the file will be the same as origin.)

    • git fetch origin
    • Git reset – hard origin/master
    • Note in particular: this will cause all changes you made to the file to be erased and restored to its original state.
  • When pushing to origin, it is usually used to pull the file first and then push it. Generally, it is used to change the submitted file and pull the same file.

    • git pull

    • git push origin master

The cached area falls back to the uncached area

  • git reset -files
  • Git reset

The uncached area reverts to the initial state of the file

  • git checkout – files

The cache reverts to the initial state of the file

  • git checkout head -files

2. Git branch understanding

This feature is used, if we need to add a new function, to project under the condition of the code is not finished, if commit lead to other members of project team, project code cannot be used, do not commit there is the risk of loss to the project schedule, we can open a new branch, and then merge branches again after completion.

  • The master branch in Git is usually the master branch. The HEAD refers to the current branch. If you keep submitting code to the master branch, the master branch will get longer and longer
  • Create a new branch, other, and switch to work on that branch. HEAD points to Other, and committing code on other causes Other to get longer and longer
  • Merge branches just point master directly to other and the current commit completes the merge
  • conflict

Git cannot perform a quick merge in this case and must resolve the conflict manually before committing

  • Branch common instruction
    • Git branch
    • Git branch name
    • Branch git Checkout name
    • Git checkout -b name
    • Git merge name git merge name
    • Deleting a branch Git branch -b name
    • Git log –graph
  • Git will use<<<<<<<.= = = = = = =.>>>>>>>Mark the content of the different branches