“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”

preface

Git plays a great role in front-end engineering, and it is also a technology that front-end ER must learn when he just gets into company projects.

Git has a lot of commands. When you learn git, you will encounter some problems such as not knowing the function of commands and not remembering commands. This article mainly shares the author’s understanding of git workflow and principle, and then to master the familiar command, Git can be perfected!!

Understanding of Git workflow

Connection between remote repository and local workspace

As can be seen from the figure, we generally clone the resources from the remote repository to the local workspace, then modify the resources in the workspace in a series of operations, and finally push the modified code to the remote repository to realize the update of a remote repository and the local workspace

In the case of multiple users, be aware that someone may have updated the remote repository during work, so be careful to update the local repository before committing the workspace changes

Locally, from workspace to staging area to version library (Master branch)

The figure above depicts the local workflow:

  • After the workspace operation is complete, commit the changes to the staging area with the Git add

    operation. At this point: the directory tree of the staging area is updated, and the contents of the files modified by the workspace are written to an object in the object library, and the object’S ID is recorded in the staging file index

  • When git commit -m ‘< remarks >’ is executed, the temporary directory tree is written to the repository.

    Note: HEAD is currently a ‘cursor’ pointing to the master branch, so the HEAD in the command is actually the master

  • When you run git reset HEAD, the staging directory tree is overwritten by the master directory tree

  • When you run the git rm -cached

    command, the files are deleted directly from the staging area, leaving the workspace unaffected

  • When you execute git checkout. Or git checkout —

    , you replace the workspace files with all or specified files from the staging area. (This operation is dangerous and clears changes in the workspace that have not been added to the staging area)

  • When you execute git checkout HEAD. Or git checkout HEAD

    , you replace the staging area and files in the workspace with all or part of the files in the master branch that the HEAD points to. (The master tree overwrites the staging area and workspace, so this command is also dangerous because it clears the staging area of uncommitted changes as well as uncommitted changes.)

Git basic operations

Create warehouse command

  1. Git init initialization, using the current directory as git repository
Git add *. C git add README git commit -m '< description >' commit the. C end and README files in the current directory to the repositoryCopy the code
  1. git clone
Git clone < git repository > < local directoryCopy the code
  1. configuration
Git config -e --global git config --global user. Name "<Aaron_hj> --global user. Email < email >Copy the code

Submission and Modification

  1. Git add — Add files to the staging area
git add [file1] [file2] ... Git add [dir] Add the specified directory to the staging git add. Add all files to the staging areaCopy the code
  1. Git status — View files that have changed since the last commit
Git status -s gets the shortened information, that is, the changed file nameCopy the code
  1. Git diff — Compares files in the staging area and workspace
Git diff [file] View the differences between the staging area and the workspace git diff --cached [file] View the differences between the staging area and the version base. Git diff [first-branch] [second-branch] View the differences between two commitsCopy the code
  1. Git commit — Add staging content to the local repository
Git commit [file1] [file2] -m "commit the specified file in the staging area to the repositoryCopy the code
  1. Git reset — version rollback
Grammar: Git reset [- soft | - mixed | -- hard] [HEAD] - mixed as the default says it will reset the staging area file into the last submission (commit), The contents of the workspace remain unchanged --soft falls back to a version --hard undoes all uncommitted changes in the workspace, returns the staging area and workspace to the previous version, and deletes all previous information commitsCopy the code
Git reset HEAD^ git reset HEAD^ hello. PHP git reset 052ECopy the code
  1. Git rm — Delete files
Git rm -f <file> Forcibly delete a specified file from the staging area and workspace git rm --cached <file> Deletes the file from the staging area and saves it in the workspaceCopy the code
  1. Git mv — Move or rename a file, directory, or soft connection
Git mv [oldfile] [newfile] git mv -f [oldfile] [newfileCopy the code
  1. Git log — View historical commit records
Git blame <file> Displays a list of historical changes to a specified fileCopy the code

Remote operation

  1. Git remote — Remote repository operations
Git remote add [shortname] [url] Git push -u Git remote rename old_name new_name Specifies the name of the remote repositoryCopy the code

conclusion

Read this, I believe that I have carefully read the text, if you feel helpful, welcome to praise attention oh!!

The author will continue to update the summary of front-end core technology points and share cutting-edge technology in the community. Hope we can make progress together…