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 Git efficiently

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "how to use Git efficiently". In daily operation, I believe many people have doubts about how to use Git efficiently. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Git efficiently". Next, please follow the editor to study!

Git is a free and open source version control system, and it is the most popular source code management tool at present. This article covers most of the size problems encountered in the process of using Git from basic instructions to advanced operations of Git.

Basic operation

Initialization Settin

Set up user name and mailbox

$git config-- global user.name "user name" $git config-- global user.email "mailbox"-- where-- global is a global setting. If you want to use different configurations for a specific project, you can cancel this parameter-- git config can also set other options, because it is not usually used, so for more information, please see git config-- help.

View configuration

$git config-- list # View a configuration of Git $git config user.name

Create SSH Key

$ssh-keygen-t rsa-C "mailbox" # then under the user's home directory, you will find the .ssh directory, which contains two files, id_rsa and id_rsa.pub. # id_rsa.pub is the SSH Key public key. Add it to the SSH keys of github, and you can push the local warehouse to the remote warehouse.

Create a local warehouse for basic source code management

# initialize the repository $git init # submit the source code to the staging area $git add. # submit the source code of the staging area $git commit-m "instructions for this submission" # View current status $git status $git status-sb / / View branch and short status # View submission history $git log

Synchronize local code to a remote warehouse

# Clone $git clone warehouse address # remote warehouse $git remote # branch $git branch branch name / / create $git checkout branch name / / switch # merge $git merge branch name # pull update $git fetch hostname branch name / do not merge $git pull hostname branch name / / merge # push $git push hostname Branch name # labeled $git tag-a version number-m "version notes"

Advanced operation

Ignore SSL certificate error

When accessing the Git remote repository through HTTPS, if the server's SSL certificate is not signed by a third party, then Git will prevent this operation

$env GIT_SSL_NO_VERIFY=true git clone warehouse address $cd warehouse name $git config http.sslVerify "false"-use the env command to ensure that ignoring certificate errors applies only to this clone

Storage

When there are uncommitted changes, you can use git stash to temporarily store the current changes when you perform some operations that require clean to be maintained in the warehouse state to switch branches or pull updates.

# Storage $git stash # View Storage $git stash list # apply recent Storage and delete $git stash pop # Application specified Storage does not delete $git stash apply [Storage name] # remove Storage $git stash drop Storage name

Undo action

For the changes to the workspace and staging area, we can't guarantee that the last operation is perfect. Git is so powerful that it provides a lot of back-to-back operations.

# discard the file in the staging area and put it back in the workspace $git reset HEAD file # discarding the modification of the workspace $git checkout-- the file-- the modified file is not added to the staging area, then undo it to git commit-- add it to the staging area, then undo it to the git add status # overwrite the last submission $git commit-amend [- m "instructions for this submission"]

Version fallback

# fallback to a previous submission $git reset-hard commit_id-HEAD points to the current version-git log can view the submission history to determine which version to fallback to-git reflog can view the command history to determine which future version to go back to # undo a submitted $git revert commit_id-instead of removing the commit from the project history, generate a new commit

Branch

Git has many advantages, among which the ease of use of branches is a very significant one. When creating a warehouse, master is the default branch. Generally, we will create other branches to develop on it, and then merge them into the main branch.

# create a development branch $git branch dev # create and switch to the development branch $git checkout-b dev # switch back to the main branch $git checkout master # quickly check out the previous branch $git checkout-# View all branches $git branch-a # View the list of branches merged into the current branch $git branch-merged # see the list of branches that have not been merged into the current branch $git branch -- no-merged # Delete temporary branch $git branch-d fix # push development branch to remote warehouse $git push origin dev # rename branch $git branch-m dev develop # Delete remote branch-$git push origin-- delete branch name-$git push origin: branch name

Label

When a new version of the software is to be released, we usually give it a tag

# tagging $git tag [- a] signature [- m Note Information id of a submission] # push the tag to the remote warehouse $git push origin signature $git push origin-- tags/ / push all tags # Delete the local tag $git tag-d signature # Delete the remote tag $git push origin-delete signature $git push origin: refs/tags/ signature

Submit

# fancy View submission $git log-- pretty=oneline / / display only one line $git log-- abbrev-commit / / display only the first few characters of SHA-1$ git log-- graph / / display the branch merge history of the ASCII graphical representation $git log-- relative-date / / use a short relative time to display $git log-- name-only / / only show after the submission information Show the list of modified files $git log-n / show the most recent n submissions $git log-- author=fengshangwuqi / / show only the submissions related to the specified author $git log-- grep / / show only the submissions with the specified keywords # commit without any changes git commit-m "a no chnage commit"-- allow-empty master To be used in the following situations:-mark the start of a new work or a new feature-record code-independent changes to the project-communicate with others using your warehouse-as the first submission of the warehouse Because it cannot be submitted by rebase after the first submission.

The difference between git pull and git pull-rebase

$git pull = git fetch + git merge $git pull-- rebase = git fetch + git rebase

The difference between git merge and git rebase

# the final result of the same point integration points to the same snapshot # different points # # different submission history-merge generates a new commit node, submission history faithfully records what actually happened-rebase does not generate additional commit nodes, submission history reflects what happens during the project # # conflict handling strategies are different-merge stops when it encounters a conflict Wait for the conflict to be manually resolved and resubmitted before merge-rebase suspends the current operation when it encounters a conflict. Developers can choose to resolve the conflict manually, and then git rebase-- continue to continue, or-- skip to skip, or-- abort to stop # Note # # it is recommended to combine git merge with no-ff-git merge defaults to fast forward (Quick merge) It is suitable for branch B to come out of checkout from branch A, branch A has no commit-if branch B is checkout and branch An is modified, then it is impossible to merge quickly, an additional merge commit will be established, and a merge operation will be done for branch An and branch B, namely-- no-ff, which has the advantage of maintaining the structure of the branch.

Other

Ignore special files

For files automatically generated by the operating system, intermediate files generated by compilation, and configuration files with sensitive information, we do not want to track or put them in our remote repository. At this point, we create a .gitignore file to ignore the above files. Here is a file reference ignored by the front-end project.

# Logs logs * .log # Runtime data pids * .pid * .seed * .DS_Store # testing coverage # Dependency directory node_modules # Bower bower_components/ dist # WebStorm file * .idea/ # vscode file .vscode / # Emacs. Tern-port. # * # * * ~ # vim .agram # sensitive information default.yml

Commit message Guid

A good Commit message can provide more historical information, facilitate quick browsing and search, and can also generate Change log directly, generally including at least type and subject,type are categories of commit, and subject is a short description of commit.

# # type-feat: add new features-fix: fix defects-docs: modify document-style: modify format-refactor: refactoring-perf: optimize-test: add testing-chore: build process or accessibility changes-revert: roll back to the previous version

In addition, interested students can also add more content such as gitmoji and validate-commit-msg.

Turn off Issue using Commit information

If a submission fixes an Issue, when submitting to a branch, you can use keywords such as fix/fixes/fixed, close/closes/closed or resolve/resolves/resolved in the submission information, followed by the Issue number, so that the Issue will be closed

$git commit-m "fix:..., fix # 1, # 2"

This will close Issue # 1 and # 2 and associate references to this submission in the Issue discussion list

If you want to link to the Issue of other warehouses, use {user} / {repo} # ISSUE_NUMBER

Quickly add license Fil

When creating a warehouse, Github will provide you with a list of preset software licenses

If there is no choice at this time, you can add it later through the web interface create new file, enter LICENSE, and click choose a license template on the right

Some good github plug-ins

Octotree: provides the project directory

GitHub Plus: provides the size and download link of each file

GitHub Hovercard: when hovering over a user's profile picture or warehouse address, provide more information through the hover box

Sourcegraph for GitHub: provides common functional operations on IDE

Awesome Autocomplete for GitHub: provide more powerful intelligent search

Isometric Contributions: more friendly presentation of submission records

Git.io

Git.io is a short URL service of Github.

You can use it with the normal HTTP protocol through the Curl command

$curl-I https://git.io-F "url= https://github.com/..." HTTP/1.1 201Created Location: https://git.io/abc123 $curl-I https://git.io/abc123 HTTP/1.1 302Found Location: https://github.com/...

Gists

Gists makes it easy for us to manage code snippets without having to use a fully functional warehouse.

Gist can easily get HTML code that is easy to embed into other websites.

Moreover, Gists can be cloned like any standard repository, and you can modify and update Gists like a Github repository, except that Gists does not support directories, and all files are added to the root directory of the repository

Use shortcut keys

The github warehouse page provides some keyboard shortcuts for quick navigation, press? You can view the list of shortcut keys supported on the current page

Use emoji

We can add our favorite emoticons in Pull Requests, Issues, commit, Markdown files, using the following methods

: name_of_emoji:

For example: 喜悦: will return a

For more expressions, please refer to emoji-cheat-sheet

Among them, the commonly used expressions may be

: tada:

: sparkles:

: bug::

: white_check_mark::

: wrench:

: ok_hand:

: construction_worker:

: whale:

: clap:

At this point, the study on "how to use Git efficiently" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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