git common commands

git command

1. Version number query

$ git --version

2. Clear screen

$ clear

3. Set user name and mailbox

$ git config --global "user name"
$ git config --global "mailbox"

4. Initialize local warehouse

$ git init

5. View directory

$ ll
$ ll -la

//Back to the previous directory
$ cd ..

6. Submit file to staging area

$ git add file name

7. Submit the file to the local library (- m is to annotate the file)

$ git commit -m "File Comments " file name

8. View file status

$ git status

9. View commit log

[1]Mode 1:
$ git log

//When the log is too long and paging occurs,
Next page: spaces
 previous page: b
 sign out: q

[2]Mode 2:
$ git log --pretty=oneline

[3]Mode 3:
$ git log --oneline

[4]Mode 4: (most commonly used)
$ git reflog

10. Forward or backward to a historical version

$ git reset --hard Indexes

Parameter Description:

[1]hard Parameters: (most commonly used)
While the pointer of the local library moves, reset the staging area and reset the workspace

[2]mixed Parameters:
While the pointer of the local library moves, the staging area is reset, but the workspace does not move.

[3]soft Parameters:
When the pointer of the local library moves, the staging area and workspace do not move.

11. Delete file

$ rm file name

//Synchronize to local library after deletion
1> $ rm file name
2> $ git add file name
3> $ git commit -m "Related notes" file name

12. Recover / retrieve deleted files (focus on recovery history operations)

//If you want to retrieve the deleted files in the local library, it is actually the backward of the historical version. Execute the backward command of the historical version:

$ git reset --hard Indexes

13. File comparison: the workspace and staging area are inconsistent

//Single file comparison
$ git diff file name

//Multi file comparison
$ git diff

//Staging area and local library ratio pair:
$ git diff Index file name


1. View branch

$ git branch -v

2. Create branch

$ git branch Branch name

3. View the contents of a file

$ cat file name

4. Switch branch

$ git checkout Branch name

5. Branch conflict problem

Q:When will branch conflicts occur?
A:Modify in the same location of the same file.

The company or individual decides to delete or merge the branch content and leave the desired content

After deletion, resubmit to the staging area and then submit to the local library

Local library and remote library interaction

1. Create local library

$ mkdir Library name

2. Check if the remote library has an alias

$ git remote -v

3. Set alias for remote library

$ git remote add Alias remote warehouse address

fetch: Files can be retrieved from a remote warehouse
push: Files can be pushed to a remote warehouse

4. Push file to primary branch:

$ git push alias/github Warehouse address master 

//In case of verification timeout in push, ssl verification can be cancelled
$ git config --global http.sslVerify "false"

5. Clone

$ git clone Remote library address

Cloning can help us:
(1)Initialize local library
(2)Clone the contents of the remote library completely to the local library
(3)Create a remote library alias for us

Pull operation

[1] pull is equivalent to fetch + merge
[2] Before pulling, confirm whether the content of the remote library is up-to-date.
[3] Project pull
(1) fetch command: only the contents of the remote library are downloaded locally, and the files in the workspace are not updated.

$ git fetch alias master

//Switch remote branch
$ git checkout origin/master

(2) Merge: merge;

$ git merge origin/master

(3) Remote library fetching can be completed directly with the pull command:

$ git pull origin master

fetch + merge - > be careful for insurance
pull - > the code is simple and easy

push failure in collaboration may be caused by merge conflict!
Pull the conflict file first, handle the conflict, and then push it (refer to the conflict handling method above)

. ssh secret free operation

[1] Enter the home directory

$ cd ~

[2] Execute the command to generate an. ssh directory

$ ssh-keygen -t rsa -C mailbox

//Parameter details:

Keygen ---> key generation
 be careful: C Capitalize
 The mailbox behind is registration GitHub Mailbox bound at
 Press enter three times to determine the default value

There are two files in the. ssh Directory:

[3] Open file to copy the contents
[4] Log in to GitHub account: open setting

Then find the SSH and GPG keys option in the left sidebar:

[5] After generating the secret key, you can use the push operation normally
Alias the ssh Remote address: (remember to check the alias to prevent manual errors)

$ git remote add origin_ssh Remote address

//Show alias
$ git remote -v

The advantage of ssh: you don't have to verify your identity every time
Defect: only one account can be used

Allow merging of unrelated history

//Pull from two different warehouses
$ git pull origin master --allow-unrelated-histories

//Push push
$ git push -u origin master -f

How to avoid conflicts:

[1] Avoid changing code in the same file during team development
[2] Before modifying a file, pull before push

Tags: git github

Posted on Tue, 26 Oct 2021 06:10:38 -0400 by meshi