Summary of Git commands

Keywords: git

Global configuration

Git Distributed Version Control System needs to record name and Email address, use - Global to modify global configuration, or configure only for a warehouse:

~ ➤ git config --global user.email "dw.jerry.c@gmail.com"
~ ➤ git config --global user.name "JerryBaby"

Create Version Library

~ ➤ git init jerry
Initialized empty Git repository in /Users/jerry/git/jerry/.git/
jerry ➤ touch readme.md
jerry ➤ echo "test" > readme.md
jerry ➤ git add readme.md
jerry ➤ git commit -m "add readme.md"
[master (root-commit) 6c71199] add readme.md
 1 file changed, 1 insertion(+)
 create mode 100644 readme.md

View the submission log

jerry ➤ echo "test2" >> readme.md
jerry ➤ git add .
jerry ➤ git commit -m "add test2"
[master 5380bc0] add test2
 1 file changed, 1 insertion(+)

jerry ➤ git log
commit 5380bc0f617b744cf331d0a8d65f96492b7b4573
Author: JerryBaby <dw.jerry.c@gmail.com>
Date:   Tue Jun 20 21:05:59 2017 +0800

    add test2

commit 6c71199d2399cdb37591e216efa7777e08c450e6
Author: JerryBaby <dw.jerry.c@gmail.com>
Date:   Tue Jun 20 21:04:06 2017 +0800

    add readme.md

Back Version

In Git, the current version is represented by HEAD, the last version is HEAD ^, the last version is HEAD ^, and so on.

jerry ➤ git reset --hard HEAD^
HEAD is now at 6c71199 add readme.md

The git reset command sets the current HEAD to the specified state. --hard parameter

If you want to restore to the latest version, just make the version number:

jerry ➤ git reset --hard 5380bc0f617b744cf331d0a8d65f96492b7b4573
HEAD is now at 5380bc0 add test2

Workspace and temporary storage area

Work directory

A workspace is a directory that you can see on your computer.

repository

The workspace hides directories. Git is Git's version library. What's more important is the temporary area called stage (or index), the first branch master automatically created by Git, and a pointer HEAD to the current branch.

  • The git add command adds modifications to the temporary area
  • The git commit command submits the contents of the temporary area to the current branch

Revoke the amendment

  • When you modify the contents of a file in the workspace, you want to discard the changes in the workspace directly and execute git checkout -- filename
  • When you modify the contents of a file in the workspace and add it to the temporary area, you want to discard the modification and execute git reset HEAD filename; git checkout -- filename
  • When you modify the contents of a file in the workspace and submit it to the version library, you want to discard the modification and use version rollback.

Delete files

After deleting the file with the rm command:

  1. To delete the file from the version library, execute git rm; git commit
  2. Delete the error, restore the file, and execute git checkout -- filename

Add remote warehouse

Associate a remote warehouse with the command git remote add origin git@server-name:path/repo-name.git

After association, use the command git push-u origin master to push all the contents of the master branch for the first time

Thereafter, after each local submission, you can use the command git push origin master to push the latest changes whenever necessary

Branch management

Create and switch branches:

jerry ➤ git checkout -b dev
Switched to a new branch 'dev'
//Amount to
jerry ➤ git branch dev
jerry ➤ git checkout dev
Switched to a new branch 'dev'
jerry ➤ echo "branch dev" >> readme.md
jerry ➤ git add .
jerry ➤ git commit -m "add branch dev"
[dev 007f57b] add branch dev
 1 file changed, 1 insertion(+)
jerry ➤ git checkout master
Switched to branch 'master'
jerry ➤ git branch
  dev
* master
jerry ➤ git merge dev
Updating 5380bc0..007f57b
Fast-forward
 readme.md | 1 +
 1 file changed, 1 insertion(+)

The git merge command is used to merge the specified branch to the current branch

jerry ➤ git branch -d dev
Deleted branch dev (was 007f57b).
jerry ➤ git branch
* master

Temporary modification

When the current version has not been modified, use git stash to keep the work site, modify and merge the new branch, and then restore with git stash pop

Label management

The command git tag & lt; name & gt; is used to create a new tag, which defaults to HEAD, or to specify a commit id.

Git tag - A & lt; tagName & gt; - M "..." can specify label information

Git tag - S & lt; tagName & gt; - M "..." can be signed with PGP tags.

The command git tag allows you to view all tags

The command git pus origin & lt; tagName & gt; can push a local tag

The command git push origin --tags can push all local tags that have not been pushed

Command git tag-d & lt; tagName & gt; you can delete a local tag

The command git push origin: refs/tags/< tagname> can delete a remote Tag

Reference resources: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/

Posted by Cerebral Cow on Thu, 20 Jun 2019 15:19:45 -0700