A Brief Compilation of Git Commands



Automatically help record every file change, and cooperate forever.

Learning git commands is necessary and sufficient

Centralized and Distributed

Distributed everyone’s computer is a complete version library, there are also computers similar to the central server, but this server is only used for interactive fights modification without it, we all work the same, but interactive modification is not convenient.

Installation is done by installing Xcode

Warehouse: Document modification, deletion and addition can be tracked by git. Recognition time can track history or restore a certain brother time in the future can only track changes in text files.

Create directories and view paths

MKDIR Create Directory
Pwd is used to display the current directory

Git command usage instructions

Initialize a repository (version library) with git directories, including temporary areas and master branches
Git init Views Files in the Current Directory Including Hidden Files
Ls-ah Manual Page for Getting Git Commands
Some global configuration is required before git help can be used

> 1. Configuration under user directory, which is invoked by the current login user in all warehouses ~/.gitconfig

> 2. Configuration of project directory with high priority, covering global configuration configuration configuration user name and mailbox

git config --global user.name "wirelessqa"
git config --global user.email [email protected]
Git config -- global color. UI true makes Git display color, which makes command output look more striking

Gitignore configuration is not included in Git management, mainly system files, compiled files, custom files, which are also pushed to remote libraries.

Https://github.com/github/gitignore, create touch command

Configuration editor git needs to enter some messages

git config --global core.editor emacs

Configuration comparison tool

git config --global merge.tool vimdiff

Read configuration (the same configuration option will appear multiple times, whichever is the last)

git config --list

Prior to submission of documents

Look at the current status first and give you instructions on what to do next, such as modifying the workspace file content, prompting you to add to the temporary area or discard the workspace modification commands.

git status

See what the files have actually changed

git diff

Submit amendments

Incorporate git warehouse management and add changes to the temporary storage area (use. Represents adding all changes)

git add .

Submit all changes in the temporary area to the current branch and create a snapshot

git commit -m ""

If you confirm that all current changes are the same commit, you can add – a directly, so you don’t have to add one by one.

git commit -a -m ""

The difference between workspace and the latest version in the version Library

git diff HEAD -- readme.txt

View submission history

git log --pretty=oneline

Revoke the amendment

git checkout -- readme.txt

> 1. If the modification has not been added to the temporary area, undo the modification and return to the state of the last commit, which is the same as the state in the version library.

> 2. After the modification, it is added to the temporary storage area, and it is modified again. When the modification is revoked, it returns to the state added to the temporary storage area, that is to say, it is consistent with the state in the temporary storage area.

> 3. Replace the working version with the version in the version library, whether the workspace is modified or deleted

The modification of the temporary storage area may be revoked.

git reset HEAD file

Delete and modify submission files to temporary storage area

git rm test.txt

Version regression

Git version fallback knowledge changed the head pointer, and by the way, the workspace files were updated.

git reset --hard HEAD^
git reset --hard 3628164

View command history

You can see the history of commands such as reset and commit, which you can use to view the last time

commitidgit reflog

Workspace and temporary storage area

Workspace: The directory you can see on your computer. There’s A. git hidden directory. It’s not a workspace. It’s the version Library of git. It contains the stages and git to create the first branch for us automatically.

Remote Library Management

View remote library information

git remote -v

Modify the remote warehouse address (use it to modify the address when the remote warehouse address has been added)

git remote set-url origin https://github.com/USERNAME/OTHERREPOSITORY.git

Associate remote repositories (add remote repository addresses)

git remote add origin [email protected]:linhbcode/first-test.git

The local master branch is pushed to the remote master branch. The principle is that the master branch should be synchronized with the remote master branch. The dev branch development branch also needs the remote synchronization.

git push origin master 

The master branch is pushed to the remote master branch, and the local master branch is associated with the remote master branch.

git push -u origin master

Git automatically maps the local master branch to the remote master branch, and the default name of the remote warehouse is

origingit clone [email protected]:linhbcode/first-test.git

If git pull prompts conflict resolution to grab information, there are two ways to resolve it without links

> 1. git branch — set-upstream branch-name origin/branch-name to establish association between local branch and remote branch

> 2、git pull origin branchname

Branch management

1. Incomplete code submitted on other branches

2. Each submission is at a node on a timeline, and the branch is equivalent to a pointer to a node.

Head is essentially a pointer to a branch.

Create and switch branches

git checkout -b dev

Local branches create the same branches as remote branches, mainly warehouses coming from remote clones (usually git fetch first, then git checkout-b branch-name origin/branch-name)

git checkout -b branch-name origin/branch-name

Creating Branches

git branch dev

Switch branches if the current work is only half done and no submission is made

git checkout dev

You can save the worksite with git stash, and before using it you can view several temporary lists with git stash list

Look at several temporary lists

git stash list

Preservation of work site

git stash

Resumption of work site

git stash apply

To delete the work site

git stash drop

View the current branch

git branch

View remote branches

git branch -r

Merging branches

git merge dev

Delete branches

git branch -d dev

Forced deletion of branches

git branch -D dev

(Delete remote branches) There must be no fewer spaces in front of the colon. The principle is to push an empty branch onto the server, which is equivalent to deleting the branch.

git push origin :branch-name

Submit local test branch as remote master branch

git push origin test:master

Submit local test branch as remote test branch

git push origin test:test

The newly submitted remote test will be deleted, but it will be saved locally. Don’t worry.

git push origin :test

View branch merge graph

git log --graph

View submission history

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

Merging branches

git merge branchname

When merging branches, FF mode is not used (branches delete the branch history information that can be seen)

git merge --no-ff -m "merged bug fix 101" issue-101

Label management

Describe the development process and tag a release

Look at all the labels

git tag

Label a submission history

git tag v1.0 [commit id]

View label information

git show [tagname]

It’s better to use this statement to mark the description of this label under formal development.

git tag -a v0.1 -m "version 0.1 released" 3628164

delete a tap

git tag -d v0.1

Push label

git push origin v1.0

Push all labels

git push origin --tags

Remove labels remotely (remove labels locally first)

git push origin :refs/tags/v0.9


At present, we have sorted out some common commands, enough to deal with ordinary work. If you want to read the Git principle, we recommend that you look at the original address: http://www.liaoxuefeng.com/wiki/00137395163059606dd18361248578c67b8067c8c017b000/001373962845513aefd77a99f4145a0a7a7ca057e7500, which is very complete. 。


Above is the whole content of this article. I hope that the content of this article has a certain reference value for everyone’s study or work. If you have any questions, you can leave a message and exchange it. Thank you for your support to developpaer.