git common commands

Posted by travelkind on Sun, 20 Feb 2022 07:07:27 +0100

git common commands

git is a distributed version control system

git features

  • version control
  • Multi person collaboration

Create SSH Key

$ ssh-keygen -t rsa -C "youremail@example.com"

Configure user information

$ git config --global user.name "Your Name"             
$ git config --global user.email "email@example.com"

## Check the configuration 
$ git config --list

Warehouse

Create a Git code base in the current directory

$ git init

Create a new directory and initialize it as a Git code base

$ git init [project-name]

Download a project and its entire code history

$ git clone [url]

Add / delete files

Adds the specified file to the staging area

$ git add [file1] [file2] ...

Adds the specified directory to the staging area, including subdirectories

$ git add [dir]

Add all files in the current directory to the staging area

$ git add .

Confirmation is required before adding each change
For multiple changes in the same file, it can be submitted in batches

$ git add -p

Delete the workspace file and put the deletion into the staging area

$ git rm [file1] [file2] ...

Stops tracking the specified file, but the file remains in the workspace

$ git rm --cached [file]

Rename the file and put the rename in the staging area

$ git mv [file-original] [file-renamed]

Code submission

Submit staging area to warehouse area

$ git commit -m [message]

Submit the changes in the workspace since the last commit and go directly to the warehouse area

$ git commit -a

Show all diff information when submitting

$ git commit -v

Use a new commit instead of the last commit
If there are no new changes in the code, it is used to rewrite the submission information of the last commit

$ git commit --amend -m [message]

Redo the last commit and include new changes to the specified file

$ git commit --amend [file1] [file2] ...

see information

Show changed documents

$ git status

Displays the version history of the current branch

$ git log

Displays the commit history and the files that change each time a commit occurs

$ git log --stat

Search submission history according to keywords

$ git log -S [keyword]

All changes after a commit are displayed, and each commit occupies one line

$ git log [tag] HEAD --pretty=format:%s

Display all changes after a commit, and its "submission description" must meet the search criteria

$ git log [tag] HEAD --grep feature

Displays the version history of a file, including file renaming

$ git log --follow [file]

Displays each diff related to the specified file

$ git log -p [file]

Show last 5 submissions

$ git log -5 --pretty --oneline

Displays all submitted users, sorted by submission times

$ git shortlog -sn

Displays who modified the specified file and when

$ git blame [file]

Show differences between staging and workspace

$ git diff

Displays the difference between the staging area and the previous commit

$ git diff --cached [file]

Displays the difference between the workspace and the latest commit of the current branch

$ git diff HEAD

Displays the difference between two submissions

$ git diff [first-branch]...[second-branch]

Show how many lines of code you wrote today

$ git diff --shortstat "@{0 day ago}"

Displays the metadata and content changes of a submission

$ git show [commit]

Displays the files that have changed in a submission

$ git show --name-only [commit]

Displays the contents of a file at the time of a submission

$ git show [commit]:[filename]

Displays the most recent commits of the current branch

$ git reflog

branch

List all local branches

$ git branch

List all remote branches

$ git branch -r

List all local and remote branches

$ git branch -a

Create a new branch, but still stay in the current branch

$ git branch [branch-name]

Create a new branch and switch to it

$ git checkout -b [branch]

Create a new branch and point to the specified commit

$ git branch [branch] [commit]

Create a new branch and establish a tracking relationship with the specified remote branch

$ git branch --track [branch] [remote-branch]

Switch to the specified branch and update the workspace

$ git checkout [branch-name]

Switch to previous branch

$ git checkout -

Establish a tracking relationship between an existing branch and a specified remote branch

$ git branch --set-upstream [branch] [remote-branch]

Merge the specified branch to the current branch

$ git merge [branch]

Select a commit to merge into the current branch

$ git cherry-pick [commit]

Delete branch

$ git branch -d [branch-name]

Delete remote branch

$ git push origin --delete [branch-name]

label

List all tag s

$ git tag

Create a new tag in the current commit

$ git tag [tag]

Create a new tag and specify the commit

$ git tag [tag] [commit]

Delete local tag

$ git tag -d [tag]

Delete remote tag

$ git push origin :refs/tags/[tagName]

View tag information

$ git show [tag]

Submit the specified tag

$ git push [remote] [tag]

Submit all tag s

$ git push [remote] --tags

Create a new branch to point to a tag

$ git checkout -b [branch] [tag]

Remote synchronization

Download all changes of remote warehouse

$ git fetch [remote]

Show all remote warehouses

$ git remote -v

Displays information about a remote warehouse

$ git remote show [remote]

Add a new remote warehouse and name it

$ git remote add [shortname] [url]

Retrieve the changes of the remote warehouse and merge with the local branch

$ git pull [remote] [branch]

Allow unrelated history submissions and force consolidation

$ git pull origin master --allow-unrelated-histories

Upload specified local branch to remote warehouse

$ git push [remote] [branch]

Forcibly push the current branch to the remote warehouse, even if there is a conflict

$ git push [remote] --force

Push all branches to remote warehouse

$ git push [remote] --all

revoke

Restore the specified files in the staging area to the workspace

$ git checkout [file]

Restore the specified file of a commit to the staging area and workspace

$ git checkout [commit] [file]

Restore all files in the staging area to the workspace

$ git checkout .

Reset the specified file in the staging area, which is consistent with the last commit, but the workspace remains unchanged

$ git reset [file]

Reset the staging area and workspace to be consistent with the last commit

$ git reset --hard 

Reset the pointer of the current branch to the specified commit, and reset the staging area at the same time, but the workspace remains unchanged

$ git reset [commit]

Reset the HEAD of the current branch to the specified commit, and reset the staging area and workspace at the same time, which is consistent with the specified commit

$ git reset --hard [commit]

Reset the current HEAD to the specified commit, but leave the staging area and workspace unchanged

$ git reset --keep [commit]

Create a new commit to revoke the specified commit
All changes in the latter will be offset by the former and applied to the current branch (but there is an undo version in the version record)

$ git revert [commit]

Temporarily remove uncommitted changes and move in later

$ git stash
$ git stash pop

Ignore profile (. gitignore)

1. Configuration syntax:

The directory is indicated by the slash "/";

Use asterisk "*" to match multiple characters;

With a question mark Wildcard single character

A matching list containing a single character in square brackets "[]";

With exclamation mark "!" Indicates that the matched file or directory will not be ignored (tracked);

In addition, git is useful for The ignore configuration file matches the rules from top to bottom on a line by line basis, which means that if the matching range of the previous rules is larger, the latter rules will not take effect;

2. Example:

(1) Rule: fd1/*
Note: ignore all contents in the directory fd1; Note that either the / fd1 / directory under the root directory or a subdirectory / child/fd1 / will be ignored;

(2) Rule: / fd1/*
Note: ignore all contents of / fd1 / directory under the root directory;

(3) Rules:

/*
!.gitignore
!/fw/bin/
!/fw/sf/

Note: ignore all contents, but do not ignore gitignore file and / fw/bin / and / fw/sf / directories in the root directory;

Topics: git github bash