Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the entry-level Git operation method?

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "entry-level Git operation method is what", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "entry-level Git operation method is what" it!

Guide Git is an open source distributed version control system that can effectively and quickly handle project version management from very small to very large.

Git global configuration

Generally speaking, on the new system, we need to configure our own Git working environment first. The configuration only needs to be done once, and the current configuration will be used for future upgrades. If necessary, you can modify the existing configuration with the same command at any time:

Git config-- global user.name "Breeze Yan" # configure global username git config-- global user.email "yanw02@mysoft.com.cn" # configure global user mailbox git config-- unset-- global user.name "Breeze Yan" # Unconfigure global username git config-- unset-- global user.email "breeze.yan@newbiiz.com" git config-- list # View git configuration git config user.namegit config user.email

Create a version library

# create a directory:

Mkdir git_test

# perform the following operations under the project directory to complete the initialization operation:

Git init

After initialization, a .git directory appears under the project directory, where all the data and resources required by git are stored.

Common operations of git

All files under the working directory are in these two states: tracked or untracked. Tracked files refer to files that have been incorporated into version control management. The status of tracked files may be modified, temporarily saved, or not updated (unmodified). Untracked files, which have neither the last updated snapshot nor the current staging area, are usually newly created files in the working directory.

After editing some files, git marks them as modified. We will gradually save these modified files to the staging area until the last one-time submission of all these files in the staging area, and so on. So the change period of file state when using Git is shown in the figure.

Version submission and fallback

Version submission

Create a file, code.txt, under the git_test directory, as follows:

This is the first line

Submit a version with the following command:

Yanwei@ubuntu:~/git_test$ git add code.txtyanwei@ubuntu:~/git_test$ git commit-m'first commit' [master (root submission) d66bdc0] first commit 1 file changed, 1 insertion (+) create mode 100644 code.txt

Use the following command to view the version record:

Yanwei@ubuntu:~/git_test$ git logcommit d66bdc0189d3663db2feed6193c00751b277e80d (HEAD-> master) Author: yanweiDate: Sun Jul 15 22:35:33 2018 + 0800

Version fallback

Submit a version again:

# after adding another line to the code.txt, the content is as follows:

Yanwei@ubuntu:~/git_test$ cat code.txt this is the first linethis is the second line

# submit again:

Yanwei@ubuntu:~/git_test$ git add code.txtyanwei@ubuntu:~/git_test$ git commit-m'second commit' [master 227ecaa] second commit1 file changed, 1 insertion (+) yanwei@ubuntu:~/git_test$ git logcommit 227ecaa7a5aeca38d392662263f2704c66e1e64a (HEAD-> master) Author: yanweiDate: Sun Jul 15 22:43:49 2018 + 0800second commitcommit d66bdc0189d3663db2feed6193c00751b277e80dAuthor: yanweiDate: Sun Jul 15 22:35:33 2018 + 0800first commit

Now if you want to fall back to the previous version, you can use the following command:

Yanwei@ubuntu:~/git_test$ git reset-- hard head ^ head is now located in d66bdc0 first commityanwei@ubuntu:~/git_test$ git logcommit d66bdc0189d3663db2feed6193c00751b277e80d (HEAD-> master) Author: yanweiDate: Sun Jul 15 22:35:33 2018 + 0800first commityanwei@ubuntu:~/git_test$ cat code.txt this is the first line

Where HEAD represents the latest version of the current version, head ^ represents the previous version of the current version, head ^ ^ represents the previous version of the current version, or you can use HEAD~1 to represent the previous version of the current version, and HEAD~100 represents the first 100th version of the current version.

If you need to go back to the second commit version at this time, you can use the following command:

# find the operation record by using the following command:

Yanwei@ubuntu:~/git_test$ git reflogd66bdc0 (HEAD-> master) HEAD@ {0}: reset: moving to head ^ 227ecaa HEAD@ {1}: commit: second commitd66bdc0 (HEAD-> master) HEAD@ {2}: commit (initial): first commit

# fallback to second commit:

Yanwei@ubuntu:~/git_test$ git reset-- hard 227ecaaHEAD is now located at 227ecaa second commityanwei@ubuntu:~/git_test$ git logcommit 227ecaa7a5aeca38d392662263f2704c66e1e64a (HEAD-> master) Author: yanweiDate: Sun Jul 15 22:43:49 2018 + 0800second commitcommit d66bdc0189d3663db2feed6193c00751b277e80dAuthor: yanweiDate: Sun Jul 15 22:35:33 2018 + 0800first commit

Workspace, version library and temporary storage area

Work area

The directory where we manipulate files, such as git_test, is a workspace.

Version library

The workspace has a hidden directory. Git, which is not the workspace, but the git version library.

There are many things in git's version library, the most important of which are the temporary storage area called stage (or index), the first branch master that git automatically created for us, and a pointer to master called HEAD.

Because git automatically created the only master branch for us when we created the git version library, git commit is now committing changes to the master branch.

It can be simply understood that all the file changes that need to be submitted are placed in the temporary storage area, and then all changes to the temporary storage area are submitted at once.

Work-stage-repo

When we added the file to the git version library earlier, it was performed in two steps:

The first step is to add the file with git add, which is actually adding file changes to the temporary storage area.

The second step is to commit the changes using git commit, which essentially commits all the contents of the staging area to the current branch.

Next, we create another file, code2.txt, under the git_test directory, with the following contents:

Yanwei@ubuntu:~/git_test$ cat code2.txt the code2 first line

Then edit the code.txt again and add a line to it. The edited content is as follows:

Yanwei@ubuntu:~/git_test$ cat code.txt this is the first linethis is the second linethis is the third line

Use git status to view the status of the current working tree:

Yanwei@ubuntu:~/git_test$ git status is located in the branch master for changes that have not been temporarily stored for submission: (use "git add." Update the content to be submitted) (use "git checkout--..." Discard workspace changes) modify: files not tracked by code.txt: (use "git add..." To include the content to be submitted) code2.txt

Modifications have not been added to submission (using "git add" and / or "git commit-a")

The above indicates that code.txt has been modified, but code2.txt is not tracked.

We add code.txt and code2.txt to the staging area, and then check the working tree status again:

Yanwei@ubuntu:~/git_test$ git add code.txtyanwei@ubuntu:~/git_test$ git add code2.txtyanwei@ubuntu:~/git_test$ git status is located in the branch master changes to be submitted: (use "git reset HEAD..." To cancel temporary storage) modify: new code.txt file: code2.txt

Then, by executing git commit, you can commit all changes to the staging area to the branch at once to create a version:

Yanwei@ubuntu:~/git_test$ git commit-m'third commit' [master e4fb2aa] third commit 2 files changed, 2 insertions (+) create mode 100644 code2.txtyanwei@ubuntu:~/git_test$ git logcommit e4fb2aa04ca8aa3b6a32ef46a69fa5f97ae625fa (HEAD-> master) Author: yanweiDate: Sun Jul 15 23:16:56 2018 + 0800 third commitcommit 227ecaa7a5aeca38d392662263f2704c66e1e64aAuthor: yanweiDate: Sun Jul 15 22:43:49 2018 + 0800 second commitcommit d66bdc0189d3663db2feed6193c00751b277e80dAuthor: yanweiDate: Sun Jul 15 22:35:33 2018 + 0800 first commit

Once submitted, the workspace is "clean" without making changes to the workspace again:

Yanwei@ubuntu:~/git_test$ git status is located in the branch master with no files to submit, and the clean workspace now looks like this: work-stage-repo

Manage changes to files

Modify a file

Add a line to the code.txt again, and the modified content is as follows:

Yanwei@ubuntu:~/git_test$ cat code.txt this is the first linethis is the second linethis is the third linethis is the forth line

Then use the git add command to add it to the staging area

Yanwei@ubuntu:~/git_test$ git add code.txtyanwei@ubuntu:~/git_test$ git status is located in the branch master changes to be submitted: (use "git reset HEAD..." To cancel temporary storage) modify: code.txt

Modify the file again and add a line. The modified content is as follows:

Yanwei@ubuntu:~/git_test$ cat code.txt this is the first linethis is the second linethis is the third linethis is the forth linethis is the fifth line

After submitting a version through git commit and viewing it using git status, we found that after the second modification of code.txt content, it was not added to the temporary storage area, so when a new version was created, it was not submitted:

Yanwei@ubuntu:~/git_test$ git status is located in the branch master changes to be submitted: (use "git reset HEAD..." To cancel temporary storage) modify: changes that code.txt has not temporarily saved for submission: (use "git add." Update the content to be submitted) (use "git checkout--..." Discarding workspace changes) modification: code.txtyanwei@ubuntu:~/git_test$ git commit-m'forth commit' [master 0a96a0f] forth commit 1 file changed, 1 insertion (+) yanwei@ubuntu:~/git_test$ git status in branch master that has not been temporarily stored for submission: (use "git add..." Update the content to be submitted) (use "git checkout--..." Discard changes to the workspace) modify: code.txt

Modifications have not been added to submission (using "git add" and / or "git commit-a")

Undo the modification

Discard changes to the workspace

You can use git checkout-- to discard workspace changes:

# use the following instruction to undo our second modification

Yanwei@ubuntu:~/git_test$ git checkout-code.txtyanwei@ubuntu:~/git_test$ git status

Located in the branch master

No documents to submit, clean work.

# check the code.txt file again and find that the last modification has been discarded:

Yanwei@ubuntu:~/git_test$ cat code.txt this is the first linethis is the second linethis is the third linethis is the forth line

Undo the modification of the temporary storage area

Let's edit code.txt again, add a line, and add it to the staging area:

Yanwei@ubuntu:~/git_test$ cat code.txt this is the first linethis is the second linethis is the third linethis is the forth linenew lineyanwei@ubuntu:~/git_test$ git add code.txtyanwei@ubuntu:~/git_test$ git status is located in the branch master changes to be submitted: (use "git reset HEAD..." To cancel temporary storage) modify: code.txt

Undo the changes to the staging area and put them back into the workspace with the following command:

Canceling temporary changes after yanwei@ubuntu:~/git_test$ git reset HEAD code.txt reset: M code.txtyanwei@ubuntu:~/git_test$ git status is located in the branch master for changes that have not been temporarily stored for submission: (use "git add." Update the content to be submitted) (use "git checkout--..." Discard changes to the workspace) modify: code.txt

Modifications have not been added to submission (using "git add" and / or "git commit-a")

Finally, you can discard the changes to the file completely by discarding the changes to the workspace above.

Summary

When you mess up the contents of a workspace file and want to discard the workspace changes directly, use the command git checkout-- file.

When you not only change the contents of a file in the workspace, but also add it to the temporary storage area, you want to discard the changes in two steps. The first step is to use the command git reset HEAD file to return to the above situation, and then if you have submitted inappropriate changes to the version library according to the above situation, if you want to cancel this submission, you can directly return to the version library.

File difference comparison

Common instructions for differential comparison

Git diff # View the difference between the current file and the snapshot of the staging area in the working directory git diff-- cached # View the difference between the staging file and the snapshot when it was last committed Same git diff HEAD # as git diff-staged View all changes after the last commit git diff v1.6-filename # modifications to files starting from a specific point git diff v1.6 v1.7-stat # differences between two submissions compared to git diff master...branchname # View changes before merging a branch git diff-name-only v1.6 HEAD # list v1.6 version Difference files from this to the latest version Show only files, not discrepancies

Concrete operation

Compare the changes between the current workspace and the HEAD version

# modify the code.txt of the current workspace, as follows:

Yanwei@ubuntu:~/git_test$ cat code.txt this is the first linethis is the second linethis is the third linethis is the forth linenew line

# difference comparison

Yanwei@ubuntu:~/git_test$ git diff HEAD-- code.txtdiff-- git a/code.txt b/code.txtindex 66f9219..9bc8cf3 100644 a/code.txt #-the file representing HEAD+++ b/code.txt # + represents the current workspace @ @-2 this is the first line this is the second line this is the third line this is the forth line+new line # represents one more line than HEAD in the current workspace.

Compare the differences between the two versions

# check the history submission first

Yanwei@ubuntu:~/git_test$ git reflog0a96a0f (HEAD-> master) HEAD@ {0}: commit: forth commite4fb2aa HEAD@ {1}: commit: third commit227ecaa HEAD@ {2}: reset: moving to 227ecaad66bdc0 HEAD@ {3}: reset: moving to head ^ 227ecaa HEAD@ {4}: commit: second commitd66bdc0 HEAD@ {5}: commit (initial): first commit

# compare the differences between second commit and third commit

Yanwei@ubuntu:~/git_test$ git diff 227ecaa e4fb2aa-- code.txtdiff-- git a/code.txt b/code.txtindex 9899a76..01e1274 100644 Muhammad-a third commit code.txtxtcards @-1 third commit added one more line of content than second commit.

Delete a file

Simple explanation

To remove a file from Git, it must be removed from the list of tracked files (specifically, from the staging area), and then committed. You can do this with the git rm command, along with deleting the specified file from the working directory.

If you simply delete files manually from the working directory, you will run git status in the "Changed but not updated" section.

If the deletion has been modified before and has been placed in the temporary storage area, you must use the forced delete option-f to prevent the modified content from being lost after mistakenly deleting the file.

If you want to delete only the files in the staging area and not the files in the working directory, you can use git rm-- cached

Concrete operation example

Yanwei@ubuntu:~/git_test$ rm code2.txt yanwei@ubuntu:~/git_test$ git status is located in the branch master for changes that have not been temporarily stored for submission: (use "git add/rm." Update the content to be submitted) (use "git checkout--..." Discard changes to the workspace) Delete: code2.txt

Modifications have not been added to submission (using "git add" and / or "git commit-a")

Yanwei@ubuntu:~/git_test$ git rm code2.txtrm 'code2.txt'yanwei@ubuntu:~/git_test$ git status is located in the branch master changes to be submitted: (use "git reset HEAD..." to cancel temporary storage) Delete: code2.txt

Move Fil

The command to move a file is: git mv

To rename a file in git, you can execute

Git mv file1 file2

Actually running git mv is equivalent to running the following three commands:

Mv readme.txt readmegit rm readme.txtgit add readme

View submission history

Git log # lists all updates by submission time, with the most recent update at the top-p-2 #-p to show the content difference of each submission -2 shows only the last two updates-stat # shows only brief line count-online # shows one version per line-pretty='format' # common options for customizing the record format to be displayed are as follows:% H submit object (commit) complete hash string% h submit object short hash string Full hash string of% T-tree object (tree)% short hash string of t-tree object% P parent object (parent) full hash string% p short hash string of parent object% an author (author) name of ae author email address% ad author revision date (you can use- Date= option Custom format)% ar author revision date How long ago to display the name of the cn submitter (committer), the email address of the ce submitter, the cd submission date, the cr submission date, and the s submission description as long ago

It should be noted that the author refers to the person who actually made the change, and the submitter is the person to whom the documents were submitted for the last time.

Git log-- pretty=format: "% H -% an,% ar:% s" git log-- oneline--shortstat displays only the last number of rows in the-- stat that modify, add and remove statistics. -- name-only displays a list of modified files only after submitting the information. -- name-status displays a list of newly added, modified, and deleted files. -- before= "2 weeks ago"-- after= "2012-10-29"-- pretty=oneline # date range thank you for your reading. The above is the content of "what is the entry-level Git operation method?" after the study of this article, I believe you have a deeper understanding of what the entry-level Git operation method is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report