A summary of Git commands organized by myself (recommended collection)

Git is currently a version control tool used by major Internet companies. To enter a large factory, you must learn the basic use of Git.

This article mainly summarizes and explains the usage commands of Git.

Git installation

Through https://git-scm.com/downloads , download the required version from the git official website, and install it all the way to the next step

After installation, right-click anywhere in the computer folder to see git-related commands.

git bash is a command line tool

git gui is a graphical tool

After opening git bash, enter git --version, if the version number can be output correctly, it proves that the installation is successful.

basic commands

Configure username and email

git config --global [user.name](http://user.name) 'own name'
git config --global [user.name](http://user.name) 'own mailbox'
  • local is only valid for the current repository

  • global valid for all repositories

  • system is valid for all users of the system

View configuration

git config --list --local
git config --list --global
git config --list --system

clear configuration

git config --unset --local [user.name](http://user.name)
git config --unset --global [user.name](http://user.name)
git config --unset --system [user.name](http://user.name)

Create repository

Go to the folder to be managed and execute

git init

Add files to the staging area

git add file name

Submit documents

git commit -m 'describe'

Check git status

git status

View Modifications

git diff file name

change file name

git mv original file name   new filename

View logs

The function is to view the log
git log

View the log, displayed on a single line
git log --pretty=oneline

The function is to view the historical operation records. For example, if you want to return to the "future" after rolling back the version, you can view the latest submitted version
git reflog

View commit information with visualization tools

gitk

version rollback

Go back to the previous version
git reset --hard head

When you know the corresponding version number, you can use this command, which is suitable for rolling back and going to the previous new version
git reset --hard version number

undo operation

new version git Prompt to undo with this command
git restore file name

Use this command to undo the old version, and the new version can also use this command
git checkout – file name

if already add into the staging area
git restore --staged file name

new version git This is an old version with this command git command, the new version can also use  
git reset head file name

Delete Files

git rm -f file name  

use branch

View current branch

git branch

Create the dev branch and switch over

-b Indicates to create and switch, which is equivalent to the following two commands
git checkout -b dev

create branch
git branch dev

switch branch
git checkout dev

Note: The above is the command of the old version. It is easy to distinguish between creating a branch and withdrawing with checkout. Therefore, it is recommended to use the new version to create a branch.

switch
 Create and switch to dev
git switch -c dev

switch directly to the existing dev branch
git switch dev

merge branch

Will dev The branch is merged into the current branch, and the information of the original branch will be lost after merging
git merge dev

merge dev to the current branch,–no-ff Indicates disabled fast forwad,When viewing the log later, you can see the information that the branch has been deleted
git merge --no-ff -m "merge with no-ff" dev

delete branch

git branch -d dev
git branh -D dev

uppercase -D if dev has not been merged

View branch merges

git log --graph --pretty=oneline --abbrev-commit

Use of stash (bug branch)

Save the current job site
git stash

View all saved jobs
git stash list

restore and delete the job site, equivalent to git stash apply + git stash drop
git stash pop

The development environment is under the dev branch, and the bug fixes are submitted in the master. How to quickly merge into the dev branch: transfer to the dev branch and execute the following command

git cherry-pick bug The commit version number of the branch

clone remotely to local

git clone my own git project address

If there is no project locally, pulling down the project from the remote is a clone

association

git remote add origin my own git project address

If the project is built locally, then execute this command to associate the local warehouse with the remote warehouse

Pull remote updates

git pull

After the first is associated with the remote, it is necessary to pull the remote update before submitting.

Basic push

The first push is to add-u,local master and remote master Associated to facilitate future push or pull
git push -u origin master

After push, you can use this command directly
git push origin master

View remote warehouse information

git remote

This command displays more detailed information
git remote -v

multi-person collaboration

  • git checkout -b branch name origin/branch name, create a branch corresponding to the remote branch locally, the name should be the same

  • git branch --set-upstream-to=origin/dev dev, establish the association between the local branch and the remote branch

  • git pull, grab the remote update first, if there is a conflict, manually resolve the conflict

  • git push origin branch name, push after resolving conflicts

Label

Basic operation

The role of the label can be simply understood as giving a name to the version

view all tags
git tag

Label the latest commit of the current branch, and the label name starts by itself
git tag label name

Tag a commit with a certain version number
git tag label name correspond commit version number

You can add descriptions to labels in this way,-a corresponding to the label name,-m Corresponding description information
git tag -a v0.1 -m "Description" version number

View label details
git show label name

remove tag
git tag -d label name

push tags

push a tag to remote
git push origin label name

push all tags to remote
git push origin --tags

Remove remote tags:
Delete local tags first
git tag -d label name
 
then delete from remote
git push origin: refs/tags/label name

Tags: git github

Posted by davejj on Fri, 11 Nov 2022 04:27:20 +1030