Detailed summary! Git common operations
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"
1. Create a Git warehouse
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).
2. Check the status of warehouse files
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.
To add files, you need to fill in the correct file path. If you need to add multiple files, use spaces to separate them.
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
When deleting a file that has been submitted to the warehouse from the disk, because the file still exists in the warehouse cache, use
git rm [ fileName]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:
- All empty lines or lines starting with
# will be ignored by Git.
Standard - glob
pattern matching can be used, which is applied recursively throughout the workspace.
The matching pattern can start with ( - /
) to prevent recursion.
The matching pattern can end with ( - /
) to specify the directory.
To ignore files or directories outside the specified pattern, you can negate it by adding an exclamation point ( - !
) before the pattern.
git also supports
Glob mode.
Glob mode is a simplified regular expression in
Shell.
- The asterisk (
- *
) matches zero or more any characters;
- [abc]
matches any column in the square Characters in brackets (this example matches either an a, a b, or a c);
The question mark ( - ?
) 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).
Use two asterisks ( - **
) 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
can only view the modification status of each file, but you cannot see what content of each file has been modified, so you can use
git diffView 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
.
GitThe 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
Gitrollback operation.
Recall the file status mentioned earlier, the file status can usually be divided into:
untracked
status;- Under version control and modified
modified
status; - Under version control, modified and submitted to the staging area
staged
status; - The
committed
status that has been submitted from the staging area to the local warehouse; - The
unmodified
status that has been submitted to the local warehouse without modification or cloned from the remote warehouse; -
9.1 Undo the staging area file Use
or
git reset HEAD [fileName]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
- ##git restore --staged newfile
- git reset HEAD 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,
onelinewill display each submission on one line, which is very useful when browsing a large number of submissions. There are also
short,
fulland
fulleroptions, which display information in basically the same format, but with varying degrees of detail;
Use
formartto customize the printing format. Commonly used format information is as follows:
示例 10.1 将日志信息显示在一行上
$ git log --pretty=oneline
示例 10.2 以 short 格式输出仓库修改信息
$ git log --pretty=short
示例 10.3 以 full 格式输出仓库修改信息
$ git log --pretty=full
示例 10.4 以 fuller 格式输出仓库修改信息
$ 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Resolve: When Git download speed is slow, you can take the following steps: Check the network connection and try to switch the connection method. Optimize Git configuration: Increase the POST buffer size (git config --global http.postBuffer 524288000), and reduce the low-speed limit (git config --global http.lowSpeedLimit 1000). Use a Git proxy (such as git-proxy or git-lfs-proxy). Try using a different Git client (such as Sourcetree or Github Desktop). Check for fire protection

To delete a Git repository, follow these steps: Confirm the repository you want to delete. Local deletion of repository: Use the rm -rf command to delete its folder. Remotely delete a warehouse: Navigate to the warehouse settings, find the "Delete Warehouse" option, and confirm the operation.

To download projects locally via Git, follow these steps: Install Git. Navigate to the project directory. cloning the remote repository using the following command: git clone https://github.com/username/repository-name.git

Steps to update git code: Check out code: git clone https://github.com/username/repo.git Get the latest changes: git fetch merge changes: git merge origin/master push changes (optional): git push origin master

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Git Commit is a command that records file changes to a Git repository to save a snapshot of the current state of the project. How to use it is as follows: Add changes to the temporary storage area Write a concise and informative submission message to save and exit the submission message to complete the submission optionally: Add a signature for the submission Use git log to view the submission content

To submit an empty folder in Git, just follow the following steps: 1. Create an empty folder; 2. Add the folder to the staging area; 3. Submit changes and enter a commit message; 4. (Optional) Push the changes to the remote repository. Note: The name of an empty folder cannot start with . If the folder already exists, you need to use git add --force to add.

When developing an e-commerce website, I encountered a difficult problem: How to achieve efficient search functions in large amounts of product data? Traditional database searches are inefficient and have poor user experience. After some research, I discovered the search engine Typesense and solved this problem through its official PHP client typesense/typesense-php, which greatly improved the search performance.
