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

Git getting started tutorial

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Git is an open source distributed version control system. The core of popular version control systems such as GitHub,GitLab,Gerrit, etc. is to manage code through Git. Here are some common git commands.

1、 git clone remote_git_url local_directory

This command clones the remote git library into local_directory. If you do not specify local_directory, the remote repository name is used as the folder name for local storage

git init and git init --bare

git init : Create a new git library in the current folder. A folder named.git will be generated under the folder. The current folder is equivalent to the workspace. You can execute git add, git commit, git branch, etc. If the local folder has stored the code that needs to be managed, it is more appropriate to use git init. You can continue to carry out corresponding development work in this folder.

git init -bare: Initialize a bare library in the current folder, only used to store version information, there is no workspace, unable to perform git add operations, suitable as an initial library, to share code with others.

3、 git status

This command provides an overview of native code changes and the status of the current code base

As shown in the figure above, executing git status compares the code in the current workspace with the latest code in the local code base and gives the status of the relevant changes.

Deleted: indicates that the file test has been deleted from the workspace

Modified: Indicates that the file test2 has been modified

Untracked files: indicates that the file test3 has not been added to the code base for tracking management

Git also gives commands for further processing, such as git add/rm , git checkout --shown in the figure above.

4、 git add

This command is used to add changes from the workspace to the staging area. For example, new files, changed files, deleted files, etc., the method is git add filename1 filename2, if you want to add all local changes to the temporary area, you can use git add .

We perform git add on the git library in the image above. Then run git status to get the workspace status after adding the changes to the staging area

5、 git rm

This command is used to delete files directly from the workspace and submit them to the staging area. This is equivalent to executing rm filename before executing git add filename.

6、 git config

After we add our workspace changes to the staging area, the next action is to commit the changes to the repository. We need to configure our commit user information before executing the commit for the first time.

git config user.name Richard

git config user.email Richard@qq.com

user.name is the username displayed in the commit log, user.email is the user email displayed in the commit log

This command simply submits information to the current repository configuration. If you want to configure submission information for all git libraries under your account, you need to use git config -global user.name and git config -global user.email

In addition to configuring the user information at the time of submission, you can also configure the editor used when editing the submission information, as follows: git config -global core.editor vim

You can use vim instead of the default editor.

7、 git commit

After configuring the user information we can use the git commit command to commit the changes that have been added to the staging area to the repository. As shown below, typing git commit opens an editor where you can type whatever you want. git commit -m " delete test, modify.. ", submit directly

Sometimes after we commit, we may find that we missed a change, and then we don't want to split it into two commits. In this case, after we execute git add, we can execute git commit -amend to merge the two changes into one commit.

8、 git push

Used to push changes from the local repository to the remote master repository.

The git push origin master subcommand pushes the most recent commit from the master branch of the local repository to the master branch of the origin remote repository.

9、 git pull

Get updates from remote repositories and merge with local branches.

By default git pull will take changes from the remote repository and git merge with the local branch. If we want to perform git rebase instead of git merge we can run git pull-rebase

10、 git log

View Submission Log

View the latest 5 posts git log -5

View logs since a commit git log commit_hash.. HEAD

View changes in a file from line 3 to line 10: git log -L 3,10:filename

View changes to a function in a file: git log -L: funcnmae: filename

11、 git show

See the specific changes currently submitted by HEAD

You can view the corresponding changes by clicking on the commit id of other submissions

12、 git diff

View changes made to the current workspace relative to the latest code in the library, only unstaged changes

To see the changes compared in the staging and library, use git diff --cached

13、 git branch

View current branch information

Git branch -av View all branches and latest commit id

14、 git checkout

Switch branches or restore workspace files

git checkout -b new_branch Create a new_branch

15、 git format-patch

Used to generate patch files.

Generate patch, git format-patch base_commit_id based on a commit

The generated patch, usually named 0001-.patch,0002.patch

16、 git am

Type a series of patch files generated by git format-patch into git library

17、 git apply

Type a patch into the git repository. The difference between am and git am is that am will incorporate the original submission information. Apply will only incorporate the changes into the workspace, and will not automatically enter the repository.

18、 git reset

Go back to some commit.

Git reset -hard commit_id, reverts both workspace and code base to a specified commit

Git reset -soft commit_id, rolls back the code base to the specified commit and saves the changes to the staging area.

19、 git merge, git rebase

are used to merge branches. The difference is that git merge is a direct merge, git rebase is to temporarily store the local commit, then update HEAD to the latest, and then merge the local commit.

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

Internet Technology

Wechat

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

12
Report