Summary of git common commands

config information configuration

git view some configurations

git config --list
git config -l

git view global configuration

git config --global --list
git config --global --l

View the specific configuration by adding specific items, as follows:

git view global user name

git config --global

git view global user mailbox

git config --global

git modify global user name

< username > is the new user name

 git config --global <userName>

git modify global mailbox

< useremail > is the new email address

git config --global <userEmail>

Clone project

git clone remote project

< projecturl > is the project address

git clone <projectUrl>

Branch operation

git view local branch

git branch

git look at all branches

git branch -a

git create branch

< branchname > is the branch name, the same below.

git branch <branchName>

git switch branch

git checkout <branchName>
git switch <branchName>

git creates and switches branches

git checkout -b <branchName>
git switch -c <branchName>

git pushes local branch to remote end

git push origin <branchName>
git push --set-upstream origin <branchName>

git detects remote branch to local

git checkout -b <localBName> <remoteBName>

git delete local branch

The current branch cannot be deleted on the current branch

git branch -d <branchName>

git Delete remote branch

git push origin --delete <branchName>

File operation

git view file status

git status

git discards all changes in the workspace

git checkout .
git restore .

git submits all changes in the workspace to the staging area

git add .
git add --all

git restores all changes in the staging area to the workspace

git reset HEAD

git submits the staging area file to the local warehouse

git commit -m <notes>

git view submission details

< commitid > is the id of this submission, which can be found in the submission log. See the code log below.

git show <commitId>

git pushes the local warehouse to the remote

git push

git pull remote code

git fetch

git merge branch

Merge < branchname > branch code to current branch

git merge <branchName>

git pulls the remote code to the local and executes merge

git pull
git pull = git fetch + git merge`When in master Branch execution`git pull`When, it is equivalent to master Branch execution`git fetch` Then execute`git merge origin/master

Code log

git print operation record

You can view all operation records of all branches, including commit and reset operations, including the deleted commit records.

git reflog

git print submission record

git log
git log --oneline        // Simplify log information into one line

As shown in Figure 1, the first line id is 1fd5003, which exists in the local warehouse and has not been submitted to the remote warehouse; The second line id is 92196d3, which has been submitted to the remote warehouse. Below, < commitid > is used to represent the submission id

Code fallback


soft fallback

Fallback the submission of the code, and restore the code submitted after the commitId to the staging area.
Therefore, you can use the command git reset --soft 92196d3 to restore the code after 92196d3 to the temporary storage area, so that the code returns to the submission state of 92196d3.

git reset --soft <commitId>

mixed fallback

Back off the submission of the code to < commitid >, and restore the code submitted after the < commitid > to the workspace,

git reset --mixed <commitId>

hard fallback

Return the code to the submission of < commitid >, and discard all submission codes after < commitid >.

git reset --hard <commitId>

During the development process, if the local warehouse has been submitted but not push ed, the submission of the local warehouse needs to be revoked. As shown in the status in Figure 1, you can save the time for git log to find < commitid >, directly use the remote branch name to return the local warehouse code to the temporary storage area or work area, or directly discard and keep consistent with the remote branch.

git reset --soft origin/master
git reset --mixed origin/master
git reset --hard origin/master

git revert is used to "reverse" a version to undo the modification of the version. For example, when we commit three versions (version 1, version 2 and version 3), we suddenly find that version 2 does not work (for example, there is a bug). If we want to revoke version 2 but do not want to affect the submission of revocation version 3, we can reverse version 2 with the git revert command to generate a new version 4. In this version 4, the contents of version 3 will be retained, but the contents of version 2 will be revoked.

Original link:

git revert -n <commitId>

Current branch application specified submission

Git cherry pick merge specified submission code

Compared with git merge merging all changes of one branch into another branch, cherry pick can merge one or more submissions of one branch into another branch.

git cherry-pick <commitId>

Suppose there are two scenarios:

Scenario 1: there are two different functional branches. The second branch needs to use a submission of the first branch. You can use git log to query the < commitid > that needs to be applied to the second branch in the first branch, and then switch to the second branch. Use cherry pick the < commitid > to realize the submission of the first branch and apply it to the second branch.

Scenario 2: if you have finished developing functions on one branch and are ready to submit, if you find the wrong branch, you should develop on another branch. At this time, you only need to commit the modified code of this branch, but do not push. git log records the submitted, switch to another branch, use cherry pick to merge it into this branch, and then switch back to the wrong branch and discard the developed code, You can realize the transfer of code.

Save progress

git saves the current work progress

Saving the current work progress will save the changes in the temporary storage area and work area. After executing this command, the current work area will become a clean work area without any changes.

git stash

git displays a list of saved progress

git stash list

git restores the latest progress to the workspace

By default, changes to the workspace and staging area will be restored to the workspace.

git stash pop   //The recovered progress is deleted
git stash apply   //Recovered progress will not be deleted

git restores the latest progress to the workspace and staging area

Attempt to restore changes from the original staging area to the staging area

git stash pop --index   //The recovered progress is deleted
git stash apply --index   //Recovered progress will not be deleted

git restores the specified progress to the workspace

<stashId>`Yes`git stash list`Command, as shown in Figure 2, 0 and 1 are`<stashId>
git stash pop <stashId>   //The recovered progress is deleted
git stash apply <stashId>   //Recovered progress will not be deleted

git delete storage progress

git stash drop    //Delete the latest storage progress
git stash drop <stashId>   //Delete specified storage progress
git stash clear   // Delete all stored progress.

Usage scenario: if you are developing code in one branch, you need to switch the branch temporarily to deal with the problem. You want to switch back to the original development branch to continue the development after the problem of the other branch is submitted.

Multi branch synchronous development

If you need to run the code of two branches at the same time, the previous method was to clone multiple warehouses. Now git worktree can solve this problem by connecting multiple working directories in a git warehouse for synchronous development.

Create a new directory whose path is < Path > based on the < Branch > branch

git worktree add <path> <branch>

List the details of all existing WorkTrees in the current warehouse

git worktree list

Clear the information of worktree

Delete the associated directory directly after use. After deleting the associated directory of worktree, clear the information of worktree. So that a worktree can be completely deleted.

git worktree prune

Tags: git

Posted by dsoftnet on Thu, 14 Apr 2022 16:03:04 +0930