Table of Contents
1. Create a Git warehouse
2. Check the status of warehouse files
To add files, you need to fill in the correct file path. If you need to add multiple files, use spaces to separate them.
When deleting a file that has been submitted to the warehouse from the disk, because the file still exists in the warehouse cache, use
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
.
rollback operation.
or
示例 10.1 将日志信息显示在一行上
示例 10.2 以 short 格式输出仓库修改信息
示例 10.3 以 full 格式输出仓库修改信息
示例 10.4 以 fuller 格式输出仓库修改信息
Home Development Tools git Detailed summary! Git common operations

Detailed summary! Git common operations

Mar 02, 2022 pm 05:51 PM
git

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.

Detailed summary! Git common operations

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.
Detailed summary! Git common operations
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 controluntracked status;
  • is under version control and has been Modifiedmodified status;
  • Modified under version control and submitted to the staging areastaged 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.
Detailed summary! Git common operations
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 using add 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

  • 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.
If no message is prompted, it means the file was added successfully.

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

git commit -m "description"

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.

6. Set ignore files

For some log files, temporary files and configuration or output files generated by some software, file management is not required, so you can create a

.gitignoreFile Write the file name or expression that needs to be ignored into the .gitignore file to achieve the purpose of ignoring the file.

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 status --ignored

    // Check the status and verify whether the ignored files have been included

  • ##git rm -r --cached .
  • // Clear the cache, -r means recursive deletion

  • git status --ignored
  • // Check the specific effect

    ##git add .
  • //Retrace file
  • git commit -m "update .gitignore"
  • // Submit and comment
  • 7. View File modification content
Using
git status

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 diff

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. Detailed summary! Git common operationsgit diff
To view the modifications, you need to use git diff --cached. 8. Move filesWhen you need to rename a file, you can use

git mv [oldFileName] [new FileName]

.

Git

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

Git

rollback operation.

Recall the file status mentioned earlier, the file status can usually be divided into:

Not under version control

untracked

status;
  • Under version control and modifiedmodified status;
  • Under version control, modified and submitted to the staging areastaged 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 fileDetailed summary! Git common operations
  • Use
git restore --staged [fileName]

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 addcommand 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 file

Use

git checkout -- [fileName] to roll back the file to the last submitted state.

! Note: Remember

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 history

If you want to view all submission information in a project, you can use

git log to print the submission records of all participants.

Each record will display the submitted

SHA-1 verification, the author's name and email address, and the submission time, arranged in reverse order of submission time.
Detailed summary! Git common operations 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.
    Detailed summary! Git common operations

  • --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.
    Detailed summary! Git common operations

  • --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;
    Detailed summary! Git common operations Use
    formart to customize the printing format. Commonly used format information is as follows:
    Detailed summary! Git common operations

示例 10.1 将日志信息显示在一行上

$ git log --pretty=oneline
Copy after login

Detailed summary! Git common operations

示例 10.2 以 short 格式输出仓库修改信息

$ git log --pretty=short
Copy after login

Detailed summary! Git common operations

示例 10.3 以 full 格式输出仓库修改信息

$ git log --pretty=full
Copy after login

Detailed summary! Git common operations

示例 10.4 以 fuller 格式输出仓库修改信息

$ git log --pretty=fuller
Copy after login

Detailed summary! Git common operations
修改文件人员与提交文件人员可以不是同一个人,所以在查询日志时会区分修改人与提交人。

推荐学习:《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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if the git download is not active What to do if the git download is not active Apr 17, 2025 pm 04:54 PM

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

How to delete a repository by git How to delete a repository by git Apr 17, 2025 pm 04:03 PM

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.

How to download git projects to local How to download git projects to local Apr 17, 2025 pm 04:36 PM

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

How to update code in git How to update code in git Apr 17, 2025 pm 04:45 PM

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

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

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.

How to use git commit How to use git commit Apr 17, 2025 pm 03:57 PM

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

How to submit empty folders in git How to submit empty folders in git Apr 17, 2025 pm 04:09 PM

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.

How to solve the efficient search problem in PHP projects? Typesense helps you achieve it! How to solve the efficient search problem in PHP projects? Typesense helps you achieve it! Apr 17, 2025 pm 08:15 PM

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.

See all articles