This article brings you relevant knowledge about Git. It mainly summarizes common operational issues, including creating warehouses, viewing files, adding files, removing files, modifying content, etc. Question, hope it helps everyone.
Recommended study: "Git Tutorial"
Open required To create the location of the warehouse, open the Git
command interface or Terminal terminal and enter git init
to create the warehouse.
Get a prompt after the creation is completed Initialized empty Git repository in /Users/huaqiangsun/Git/.git/
The empty Git
repository has been initialized in the current directory , you can also see the .git
folder in the directory (usually a hidden folder, you can view hidden files on Mac through the shift cmd .
shortcut key combination).
When mentioning Git
, the concepts of workspace, staging area, and version library are often mentioned. This is In a very general way, in fact, the workspace generally refers to the directory where the files we can see and the local operation files are located. The code files we normally write and the resource files we manage are all operated in the workspace, and the files here are subdivided. For files that are version controlled and files that are not version controlled.
When it comes to the temporary storage area, it is connected to the index
file. For new files in the workspace and files that have been modified and are under version control, use git add file_name
It can be added to the temporary storage area, which is equivalent to registering a name. When submitting to the repository in the future, these registered files will be brought with it. In fact, all the files will be generated after executing the git add
command. The corresponding object objects are placed in the .git/objects
directory, and the status becomes staged
. When submitted to the repository, the branch will reference these objects.
The repository is the destination for file modifications. The final modification will be submitted to the repository. At this time, the status of the submitted file becomes committed
, which is actually a kind of unmodified
Status, the repository will record every submission you make and can trace the content of every modification you make.
The file status can usually be divided into:
- not under version control
untracked
status;- is under version control and has been Modified
modified
status;- Modified under version control and submitted to the staging area
staged
status;- Committed from the staging area
committed
status to the local warehouse;unmodified
status submitted to the local warehouse without modification or cloned from the remote warehouse;
Use git status
to see file modifications and uncommitted files in the current warehouse.
Among them,
Changes to be committed
means that the temporary storage area already exists and needs to be submitted to the warehouse;Changes not staged for commit
is a file that has been operated on and has not yet been submitted to the staging area. Such files need to be added to the cache area usingadd
and then submitted to the warehouse. ;Untracked files
are files that are not in the temporary storage area;
When the modified file is added to the temporary storage area, before submission After modification again, the file will appear again in the non-temporary storage area, and it needs to be add
added to the temporary storage area again, otherwise the file in the warehouse will not contain the secondary modification after commit
Content.
Summary
If no message is prompted, it means the file was added successfully.
git status
You can only view the number of untransmitted submissions, but not the specific file information;git cherry -v
can only view the description/instructions of untransmitted commits;git log master ^origin/master
can view untransmitted commits Details of Add to the staging area.
4. Submit the files in the temporary storage areaUse
is used to submit the files that have been added to the temporary storage area, each submission Multiple files can be submitted.
5. Remove files from the warehouse
Delete the file in the cache. After resubmission, the file will no longer be included in version management. If the current operation is a mistake, you can retrieve the file through rollback operation.
If you want to delete files that have been modified before or have been placed in the temporary storage area, you must use the forced deletion option -f
parameters.
If some unnecessary files are submitted to the warehouse due to misoperation, you can use --cached
to delete only the records in the warehouse and not delete them from the disk.
git rm
The name of the file or directory can be listed after the command, or the glob
mode can be used. For example: git rm log/\*.log
.
Note the backslash \
before the asterisk *
, because Git
has its own file pattern extension matching method, so ## is not used #shell to help expand. This command deletes all files with the extension
.log in the
log/ directory.
.gitignoreFile Write the file name or expression that needs to be ignored into the
.gitignore file to achieve the purpose of ignoring the file.
.gitignore The format specification is as follows:
# will be ignored by Git.
pattern matching can be used, which is applied recursively throughout the workspace.
) to prevent recursion.
) to specify the directory.
) before the pattern.
git also supports
Glob mode.
Glob mode is a simplified regular expression in
Shell.
) matches zero or more any characters;
matches any column in the square Characters in brackets (this example matches either an a, a b, or a c);
) only matches one arbitrary character;
if Use a dash to separate two characters in square brackets, which means that everything within the range of these two characters can be matched (for example, [0-9] means that all numbers from 0 to 9 are matched). ) to match any intermediate directory. For example, a/**/z can match a/z, a/b/z or a/b/ c/z etc.
.gitignore The steps for the file to take effect are as follows:
git statusUsing
- ##git status --ignored
##git rm -r --cached .// Check the status and verify whether the ignored files have been included
- // Clear the cache, -r means recursive deletion
git status --ignored
- // Check the specific effect
//Retrace file##git add .
- // Submit and comment
git commit -m "update .gitignore"
7. View File modification content
View the modification comparison of all tracking files. It should be noted that
git diff
is to view the modified content in unstaged files. After the file is added to the staged area, it cannot be used again. git diff
To view the modifications, you need to use git diff --cached
. 8. Move files
When you need to rename a file, you can use
The renaming operation will be divided into three steps. The first step is to rename the file, then delete the original file from the warehouse, and finally add the new file to the staging area to wait for submission. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$ mv README.md LOOKME.md
$ git rm README.md
$ git add LOOKME.md</pre><div class="contentsignin">Copy after login</div></div>
If you use software to modify files in batches, you must also follow this process to delete the original files first and then add new files. 9. Undo operation
When the file content is submitted or modified due to some operational errors, you can roll back to the state before the modification through the
GitRecall the file status mentioned earlier, the file status can usually be divided into:
untracked
status;modified
status;staged
status;committed
status that has been submitted from the staging area to the local warehouse;unmodified
status that has been submitted to the local warehouse without modification or cloned from the remote warehouse;
The temporary storage area files can be restored. <blockquote><p> Description: The <code>git restore
command is newly added after the Git 2.23
version. It is used to share the functions of the git checkout
command. By using the temporary The files in the storage area or repository overwrite the modifications of the local files to achieve the purpose of rolling back the modifications. At the same time, you can also use the files in the repository to overwrite the files in the temporary storage area to achieve the purpose of rolling back.git add
command the goal of.
!!Note that this operation will not affect the branch record, it is equivalent to the previous git checkout
command to check out a file again to overwrite local modifications.
git reset
is actually used to set the head point of the branch. After a series of submissions, I suddenly found that there were problems with the recent submissions, and I wanted to start from the submission To delete from the record, the git reset
command will be used. This command is followed by commit id
, which means that the current branch is rolled back to a certain commit id
corresponding status, subsequent log records will be deleted, and the status of files in the workspace will be restored to different states depending on the parameters.
--soft
: Modifications of those versions that were rolled back will be placed in the temporary storage area and can be submitted again.
--mixed
: Default option, the changes to the rolled back versions will be placed in the working directory. You can add them to the staging area first and then submit them. .
--hard
: The modifications of the rolled back versions will be discarded directly, as if they never came.
Use the git rest HEAD file_name
command to roll back a file to the state corresponding to the HEAD
pointing version, which is actually the current version The state in the library is equivalent to restoring local modifications.
For files in the workspace that have not been added to the staging area and repository, they can be restored by the following method after executing the git add
operation:
git rm --cached newfile
Note: When using the last two commands, it cannot be the first file in the repository.
9.2 Undo modifications to a fileUsegit checkout -- [fileName] to roll back the file to the last submitted state.
git checkout -- is a dangerous command. Any local modifications you make to that file will be lost.
Git will overwrite it with the most recently committed version. Do not use this command unless you know for sure that you do not want to make local modifications to that file.
Statement: Since files that have not been added to the staging area cannot be tracked, any modifications to them cannot be rolled back! This can only be done through local file undo operations.
10. View operation historyIf you want to view all submission information in a project, you can usegit log to print the submission records of all participants.
SHA-1 verification, the author's name and email address, and the submission time, arranged in reverse order of submission time.
In addition to simple
git log, you can also add parameters to filter and format the output information:
-p --patch: It will show the differences introduced by each commit. It is also possible to limit the number of log entries displayed, for example using the -2 option to only display the two most recent commits.
--stat: List all modified files, how many files were modified, and Which lines of the modified file were removed or added. There is also a summary at the end of each submission.
--pretty: This option has some built-in sub-options for you to use. For example,
oneline will display each submission on one line, which is very useful when browsing a large number of submissions. There are also
short,
full and
fuller options, which display information in basically the same format, but with varying degrees of detail;
Use
formart to customize the printing format. Commonly used format information is as follows:
$ git log --pretty=oneline
$ git log --pretty=short
$ git log --pretty=full
$ git log --pretty=fuller
修改文件人员与提交文件人员可以不是同一个人,所以在查询日志时会区分修改人与提交人。
推荐学习:《Git学习教程》
The above is the detailed content of Detailed summary! Git common operations. For more information, please follow other related articles on the PHP Chinese website!