Suck the cat with code! This article is participating in the cat essay Activity.

preface

Git, as you know, is a free and open source distributed version control system that automatically logs every change to a file and allows developers to collaborate on editing. If you want to see a change, all you have to do is glance at it in the software. Most companies now use Git as a version control tool, and it’s something we deal with every day. So, can you wank git the cat?

Git Basic commands

git init

// initializes the file into a repository Git can manage. After initialization, open the hidden file to see a. Git file git initCopy the code

git add

It adds the code to the staging area from the workspace, tells the Git system which files to commit, and then commits them using git commit.

Git add. //-u means to add the modified and deleted files in the tracked files to the staging area, excluding the newly added files. Note that the deleted files will disappear from the Git system after they are added to the staging area, committed and pushed to the server's repository. Git add -u. // -a adds all changes and deletions of traced files to the staging area. git add -A .Copy the code

Git commit

The main idea is to commit changes from the staging area to the local repository. Every time you use git commit, you generate a 40-bit hash in your local repository, which is also called commit-id

// Submit all files in the staging area to the local repository. Git commit -m 'message' //-a commits all traced files to the local repository for modification or deletion, even if they are not added to the staging area by git add. But newly added files (that is, files that are not managed by the Git system) cannot be committed to the local repository. Git commit -am 'message' -am is equivalent to -a -mCopy the code

Git push

After committing changes from staging to the local repository using the Git commit command, the last step is to push the branch of the local repository to the corresponding branch on the remote server. Git push < remote host name > < local branch name > < remote branch name > Refs /for/master: pushes the local master branch to the corresponding master branch on the remote host Origin. Origin is the name of the remote host. The first master is the local branch name and the second master is the remote branch name.

Git push origin master git push origin master git push origin master git push origin master Git git push origin: Refs /for/master // If the current branch is traced to the remote branch, both the local and remote branches can be omitted. -delete master git push origin // If the current branch has only one remote branch, then the host name can be omitted. You can use git branch -r to see the remote branch name git pushCopy the code

git pull

// pull code from remote repository and merge it locally, Git pull -- git fetch -- git fetch -- git fetch -- git fetch -- git fetch -- git fetch < remote name >:< local name >Copy the code

git fetch

Git fetch < remote host name > Git fetch < remote host name > < branch name > git fetch < remote host name > Git log -p FETCH_HEAD: git log -p FETCH_HEAD: git log -p FETCH_HEAD: git log -p FETCH_HEAD: git log -p FETCH_HEADCopy the code

git branch

Create a branch

// Create test branch based on current COMMIT. The.git/HEAD file records the current branch name. git branch test:Copy the code

Delete the branch

Git branch -d test git branch -d test git branch -d test git branch -D testCopy the code

See the branch

Git branch --merged git branch --merged git branch --merged git branch --merged git branch --merged git branch --mergedCopy the code

Git submission specification

The commit message should be clear and state the purpose of the commit. Use the AngularJS commit specification as an example to describe git commit specifications. Each Commit message consists of three parts: header, body, and footer.

<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
Copy the code

The header is required, and the body and footer can be omitted. It is recommended that no line exceed 72 characters (or 100 characters) to avoid aesthetics caused by automatic line wrapping.

Header

The Header section is a single line with three fields: Type (required), scope (optional), and subject (required).

type

Only the following seven identifiers are allowed to describe the commit category.

Feat: New Feature

Fix: Fixes bugs

-Jenny: There are some docs.

Style: format (changes that do not affect code execution)

Refactor: refactoring (i.e. code changes that are not new features or bug fixes)

Test: Adds a test

Chore: Changes to the build process or helper

scope

Scope is used to specify the scope of the commit impact, such as the data layer, control layer, view layer, and so on, or to specify the functional module of the impact for quick location. If your changes affect more than one scope, you can use * instead.

subject

Subject is a short description of the submission, like an article title, but not too long.

Body

The Body section is a detailed description of the commit, listing the code’s capabilities in multiple lines. The body is optional, but the subject’s description is clear enough to omit the body.

footer

Describe the break change or issue associated with this submission. Applicable to the following situations:

1. Incompatible changes: If the current code is incompatible with the previous version, the Footer section begins with BREAKING CHANGE, followed by a description of the CHANGE, the reason for the CHANGE, and the migration method.

2. Close Issue: If the current commit is for an Issue, close the Issue in the Footer section.

Q&A

Change the submitted Git account

The first time you commit a change with Git, you will enter the name and email of the change. This will be used as the global configuration of the current node and will be used for subsequent commits. To change the account, perform the following operations:

Run the git config –list command to check the current git configuration, find user.name and user.email, and modify them.

To change global (change global default) name and email, run the following command:

Git config --global user.name yourName; git config --global user.email yourEmail;Copy the code

To change only the name and email of the current project, run the following command in the current project directory:

git config user.name yourName;
git config user.email yourEmail;
Copy the code

Undo local commit

git reset HEAD~2        # undo last two commits, keep changes
git reset --hard HEAD~2 # undo last two commits, discard changes 
Copy the code

The hard option resets the Working Tree and index, causing all changes to be lost.

Debug code branch

In daily development, it is possible to find changes in the wrong branch when you are ready to submit the code after developing the function. How to deal with that?

Conditions not yet committed:

Git checkout # Switch to the correct branch git stash pop # Release the code in the staging stackCopy the code

Commit situation:

Git reset HEAD~1 Git stash pop # Switch to the correct branch git stash pop # Switch to the correct branch git stash pop # Switch to the wrong branch git push origin # remove uncommitted remote objectsCopy the code

Git handling conflicts

When a pulled file conflicts with a locally modified file, commit your changes first, or store your changes temporarily

Git stash # To store local changes git pull #pull content git stash pop stash@{0} #Copy the code

Find a commit that broke something after the merge

Tracking a commit that introduces a bug after a large merge can be time-consuming. Git has a binary search tool called Git-Bisect. First do the initial setup:

git bisect start    # starts the bisecting session

git bisect bad    # marks the current revision as bad

git bisect good revision   # marks the last known good revision
Copy the code

Run the spec file again and submit the appropriate “good” or “bad” flags.

git bisect good   # or git bisec bad
Copy the code

conclusion

This paper summarizes the use of Git command in the work of relatively high frequency, as well as Git submission specifications, common problems. The use of Git is indispensable in our daily development. We want to masturbate like a cat.