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

How to use the git command

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use the git command, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

First, check which Git versions are available on your computer and choose the version you want to use

one

Which-a git / / check which versions are available

Configure the .bash _ profile file and select the version you want

one

Vim .bash _ profile

To join, we need to establish the relative path of environment.

one

Export PATH= "/ user/local/git/bin:$SPAH"

After editing, reload .bash _ profile

one

Source .bash _ profile

2. Configure your own Git information

Configure the user name:

one

Git config-global user.name jspang

Configure mailboxes:

one

Git config-global user.email jspang@126.com

III. Three levels of Git configuration

1. System-for the system

2. Global-for the current user

3. Local-for the current warehouse

Configured priority: local > global > system

4. How to view Git documents

The first method:

one

Git config-help

The second method:

one

Git help config

Rename the Git command

one

two

three

four

Git config-global alias.co checkout

Git config-global alias.br branch

Git config-global alias.st status

Git cofnig-global alias.ci commit

6. Git uses SHA-1 Hash with 40 hexadecimal characters to uniquely identify objects

one

E8bd40626e0af320b3c1bdf6154b44a59ed78039

7. The four basic object types of Git make up the more advanced data structure of Git:

1. Blobs: each blob represents a (version) file. Blob only contains the data of the file, while ignoring other metadata of the file, such as name, path format, etc.

2. Trees: each tree represents the information of a directory, including blobs, subdirectories (corresponding to sub-trees), file name, path and other metadata under this directory. Therefore, for directories with subdirectories, git is equivalent to storing nested trees.

3. Commits: each commmit records all the metadata for submitting an update, such as the pointing tree, parent commit, author, submitter, submission date, submission log, etc. Each submission points to a tree object that records the directory information at the time of the class submission. A commit can have multiple (at least one) parent commits.

4. Tags:tag is used to assign a name to an object of the above type that is easy for developers to remember, which is usually used in a commit.

Basic object diagram

8. Get the order of Git warehouse

1.git init: initialize a Git repository

one

Git init git_test

This command creates a repository called git_test and creates a git_test folder under your directory. .git is also generated in the folder.

one

Git init-bare git_bare_test

Generate a naked warehouse without a .git file, that is, without a workspace.

2.git clone:

one

Git clone https://github.com/shenghy/Scroll.git

Clone an existing repository, followed by either a URL or a local path. This command is often used. If you see other people's good open source projects on GitHub, you can clone down to do some research.

IX. Three areas of Git warehouse

1.working directory: workspace

2.staging area: staging area

3.history repository: history warehouse

Git workspaces deal directly with staging areas. Staging areas deal with version repositories.

Add content from the workspace to the staging area.

Submit the contents of the staging area to the version library.

Check out the content from the version library to the workspace.

10. Git process operation command

1.git add: mainly used to add the file information we want to submit to the temporary storage area.

one

Git add-u

Add information from all tracked files that have been modified or deleted to the staging area. It does not process untracted files.

one

Git add-A

Means to add the file information of all modified or deleted tracked files and all untracted files to the staging area.

one

Git add-I

View all files that have been modified or deleted but not submitted.

2.git commmit: add the staging area content to the version repository.

one

Git commit-m "submitted description information"

If we don't use the parameter-m here, git will use Vim to let us enter the submission information.

one

Git commit-a-m "submitted description information"

We may have updated a lot of documents in one day, but we forgot their names, and at this point we added all the updates to the version library.

3.git status: view the differences between workspaces, staging areas, and version libraries.

4.git rm: in git, we can delete a file through the git rm command and remove it from git's warehouse management system. But in the end, the heart of the git commit is actually submitted to the git repository.

one

two

Git rm a

Git commit-m "delete a file"

Delete the files in the temporary storage area and use the-cached parameter

one

Git rm-- cached a

Use commands when renaming or moving paths to 5.git mv:git files

Rename the file a to c

one

Git mv a c

In fact, git does not have renaming or moving operations, but Git is actually a combination of a series of operations.

6.gitignore: add files that don't need to be traced to this file to avoid tracing. Wildcards can be used in this file.

Sometimes you don't have this file in the folder, so you can create a .gitignore file yourself.

11. Git creates branches locally

one

Git branch test / / New test Branch

When we create a branch, we don't go directly to this branch, but on a main branch. We need to switch to the newly created test branch with the switch branch command.

one

Git branch

With no parameters, lists the branches that already exist locally, and the current branch is preceded by a "*" mark.

one

Git branch-r / / list remote branches

one

Git branch-a / / lists local and remote branches

Rename the branch, if the newbranch name branch already exists, you need to use-M to force the rename, otherwise, use-m to rename.

one

Git branch-m |-M oldbranch newbranch

Delete branchname Branch

one

Git branch-d |-D branchname

Delete remote branchname branch

one

Git branch-d-r branchname

12. Git gives the branch an alias (also called tagging)

There are two types of git tags: lightweight tags and note tags. The lightweight tag is a reference to the submission object, while the note tag is a separate object in the repository. Note tags are recommended.

Create a lightweight label

one

Git tag v0.1.2-light

Create a note label

one

Git tag-a v0.1.2-m "0.1.2 version"

To create a lightweight label, you don't need to pass parameters, just make the label name.

When creating a note tag, the parameter an is the abbreviation of annotated, and the label type is defined, followed by a signature. The parameter m specifies the label description, which is saved in the label object.

Before we learn about aliasing the branch, we will use a command to view the Log log, use this command to find out our current Git situation, and then name it.

View the version history schematic:

one

Git log-oneline-decorate-graph-all

one

Git tag "v0" 51bcb0d

Of course, when we look at the historical version of the schematic diagram, the command is too long, we can give this command a different name:

one

Git config-global alias.lol "log-oneline-decorate-graph-all"

In the future, you can view the schematic diagram of the historical version with the git lol command.

Name it "v0" with the Git command and represent his HS value. If the Hash is not specified, the Hash specified by HEAD (that is, the current HS) will be specified by default. Generally, we only use 5-7 bits of Hash.

XIII. Switching between branches

one

Git checkout test / / switch branch to test

Save local changes and changes to the temporary storage area before switching branches

When we made changes on a branch, we committed to the staging area, but not to the version library. When we want to switch branches (such as switching to master branch git checkout master), the system will report an error, indicating that there are files in the temporary storage area that have not been submitted. If we don't want to submit the file yet, we can use the following command to save the file in the staging area.

one

Git stash save-a "stash2"

Stash means to hide in English. When we get back to the hidden branch after we have finished working in other branches, how can we restore the hidden content?

1) you can use the command to see what we are hiding.

one

Git stash list

Through this command, we will list references to what we are hiding.

2) you can restore the hidden things with the following command.

one

Git stash pop-- index stash@ {0}

At this time, we use the git status command to check the git status, and we will find that what we have hidden has been restored. This command not only restores the hidden content, but also deletes the contents of the hidden area, which consists of multiple actions. If we only want to restore the contents of the staging area, but do not delete the contents of the staging area, we can use the following command.

one

Git stash apply-- index stash@ {0}

After we use the git stash apply-index stash {0} command to restore the hidden content, we can use the following command when we want to delete the content of the hidden area.

one

Git stash drop stash@ {0}

If we do not add the stash@ {0} reference, it will delete the top one in the stash stack by default. If we have more than one stash to clean up, we can use the

one

Git stash clear

To clean up.

15. Merge branches locally

There are many cases of merging branches, and sometimes conflict situations have to be dealt with. There is not too much introduction here today, only the most basic grammar.

one

Git merge branchName

There are two kinds of merge

1) fast-farword merge

Derived from the master branch

2) non-fast-farword merge

Tripartite merger of branches

XVI. Undo the amendment

1.git checkout: restore the workspace

It turned out that we used the git checkout command to switch branches, but today we learn another use diagram to restore our workspace. The case command is as follows:

one

Git checkout-master.txt

This command actually overwrites the contents of the workspace with the contents of the staging area.

2.git reset: restore staging area

When we use the contents of the workspace with git add. After submitting to the staging area, we can use the git Reset fileName command to restore the contents of the staging area. The example command is as follows:

one

Git reset master.txt

After entering this command, you can use git status to check the Git status and find that there is no difference.

Restore other historical version information:

one

Git checkout INITIAL_COMMIT-master.txt

INITIAL_COMMIT is a version of the reference.

one

Git checkout HEAD-master.txt

3.git clean: clear files that are not tracked

Check out the workspace content that we have not tracked and removable, that is, the content that will be deleted.

one

Git clean-n

After viewing it, we can delete the content with the following command.

one

Git clean-f

Use the git clean command to view the uncommitted file specified by the .gitignore that will be removed, as follows:

one

Git clean-n-x

Delete .gitignore with the git clean-X-f secret order. Note that the X here is an uppercase X.

one

Git clean-X-f

4.git revert: generate new submissions, overwriting previous submissions

one

Git revert HEAD

17. Remote collaboration main command git clone: clone the remote repository to the local

one

Git clone https://github.com/shy-test/test.git

Through this order, we have cloned the remote warehouse to our local area. To use this command, as long as you go to the working directory, there is no need to create a warehouse directory, the warehouse directory will be cloned by itself.

2. Git fetch: get all the contents of the remote repository, including all branch content

First enter the local warehouse directory, and then use the git fetch command to get all the contents of the remote warehouse. It is equivalent to getting the latest version remotely to the local, and will not automatically merge.

one

two

three

Git fetch origin master

Git log-p master..origin/master

Git merge origin/master

The meaning of the above command:

First download the latest version from the master main branch of the remote origin to the origin/master branch, then compare the differences between the local master branch and the origin/master branch, and finally merge.

The above process can actually be carried out in the following clearer ways:

one

two

three

Git fetch origin master:tmp

Git diff tmp

Git merge tmp

Get the latest version from the remote to the local tmp branch, and then compare and merge.

3. Git pull, which is the operation combination of git fetch and git merge.

one

Git pull origin master

The above command is actually equivalent to git fetch and git merge, but in practice, git fetch is more secure because we can check the updates before merge and then decide whether to merge or not.

4. Git push: command the Royal user to push the update of the local branch to the remote warehouse host.

one

Git push:

Notice that the branch push order is written as:, so git pull is:, and git push is:.

If the local branch name is omitted, the local branch is pushed to the remote branch with which it has a "tracking relationship" (usually with the same name), and if the remote branch does not exist, it will be created.

one

Git push orgin master

The above command indicates that the local sub-master branch is pushed to the origin-based master branch. If the latter does not exist, it will be created.

If the local branch name is omitted, the specified remote branch is deleted because this is equivalent to pushing an empty local branch to the remote branch.

one

two

three

Git push origin: master

/ / equivalent to

Git push origin-delete master

The above command indicates that the master branch of the origin host is deleted.

If there is a tracking relationship between the current branch and the remote branch, both the local branch and the remote branch can be omitted.

one

Git push origin

The above command indicates that the current branch is pushed to the corresponding branch of the origin host.

If the current branch is just a tracking branch, the hostname can be omitted.

one

Git push

If we want to push a tag, we can use the following command:

one

Git push-tags

Delete the remote branch with push:

one

Git push-delete origin feacher

The above is all the contents of this article "how to use git commands". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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