Configure the operating

Global configuration

Git config --global user. Name 'your name' git config --global user. Email 'your email' git config --global user Git config --system user.name 'git' --local user.name 'git' --local user.name 'git' --local user.name 'git' --local user.name 'git' --local user.name 'git Config -- system user. Email 'your email -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- for example -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the git config - global user. The name 'lvpeng' git config --global user.email '[email protected]'Copy the code

Clear set

Git config --unset --global Configuration item to be deleted git config --unset --local Current repository configuration item to be deleted git config --unset --system System configuration item to be deleted -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- for example -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the git config - the unset - global user. The name git config - the unset - global  user.emailCopy the code

Check the configuration

Git config --list --global git config --list --local git config --list --system View system configuration itemsCopy the code

Initialize the warehouse

CD Project code folder git initCopy the code

Establish a connection between the client and the remote server

Git remote add origin remote warehouse address -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- for example -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the git remote add origin git@server-name:path/repo-name.gitCopy the code

After the connection is established, the local code is pushed to the server

Git will not only push the contents of the local master branch to the new remote master branch, but also associate the local master branch with the remote master branch. You can remove the “-u” in future pushes or pulls to simplify command execution.

Git push -u origin local branch name -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- for example -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the git push -u origin masterCopy the code

The local operating

Viewing Changes

git status
Copy the code

Adds all changes under the current directory and its subdirectories to the staging area

git add .
Copy the code

Add all changes in the warehouse to the staging area

git add -A
Copy the code

Add file changes, file deletions, and file changes to the staging area (uncommon)

git add -u

Copy the code

Adds the specified file to the staging area

Git add file 1 file 2 file 3Copy the code

Commit the modified file directly to the local repository

Git commit -a -m git commit -amCopy the code

Change the file name

Git mv Original file Name New file nameCopy the code

View all files in the current directory

ls -al 
Copy the code

Compare all differences between workspaces and staging areas

git diff
Copy the code

Compares the difference between a file’s workspace and staging area

Git diffCopy the code

Compare all differences between the staging area and HEAD

git diff --cached
Copy the code

Compares the difference between a file in the staging area and the HEAD

Git diff --cached fileCopy the code

Compares a file workspace with a HEAD

Git diff HEAD fileCopy the code

View all commit records

git log
Copy the code

View displays the nearest n commits

git log -n
Copy the code

The current branch commit is displayed on a single line, which makes commitID shorter

Git log --oneline or git log --pretty=oneline --abbrev-commitCopy the code

Each commit of the current branch is displayed on one line, and commitID is long

git log --pretty=oneline
Copy the code

You can see the branch merge diagram

git log --graph --pretty=oneline 
Copy the code

Displays all commit records, both local and remote

git reflog
Copy the code

View the history of all branches

git log --all 
Copy the code

View the graphical log address

git log --all --graph
Copy the code

View all graphical log addresses. CommitId shortened

git log --oneline --graph --all
Copy the code

Check out four brief recent histories

git log --oneline -n4
Copy the code

View the graphical history of the last four single lines for all branches

git log --oneline --all -n4 --graph

Copy the code

The last line of a file changes to the corresponding commit and author.

Git blame fileCopy the code

Use DiffTool to compare the difference between any two Commits

git difftool commitId1 commitId2 
Copy the code

The git reset command can roll back both the version and the changes made in the staging area to the workspace. When we use HEAD, we mean the latest version.


Roll back all files in the staging area and workspace to the same level as HEAD

git reset --hard
Copy the code

Rollback of all files in the staging area and workspace to a version at a specified commit point (version traversal)

Git reset --hard commit_id git reflogCopy the code

Rollback to the previous version

Git reset --hard HEAD~1 git reset --hard HEAD^Copy the code

Operations for several zones:

  1. Workspace –> Staging area: git add
  2. Local repository: git commit
  3. The staging area – > workspace: git reset HEAD fileName | | git restore – staged fileName

Kick all files saved in the staging area back into the workspace

Git reset head (head)Copy the code

Undo Changes: Case one, changes have been made in the workspace but have not yet been committed to staging (no add and commit)

Git checkout -- fileName or git restore fileNameCopy the code

Undo changes: Case 2: Added to staging, but not committed yet

Git reset head (head) fileName (10) Git restore -- passage ten fileName (10) Git checkout -- fileName or git restore fileNameCopy the code

Compare file changes between staging area and workspace

git diff
Copy the code

Compares file changes between staging area and HEAD

git diff --cached
Copy the code

Check which files are not controlled by Git

git ls-files --others
Copy the code

Temporarily suspend the current branch

git stash
Copy the code

Resume suspended workspaces

Remove the suspended workspace from the suspended workspace with git Stash pop and restore the workspace with git Stash popCopy the code

View the currently pending branch

git stash list
Copy the code

Retrieve a stash change

Git stash pop stash@{number n}Copy the code

Modify the last commit information

Git commit --amend The command enters vim mode. 1. Press I to enter editing mode. The words "-- NSERT --" appear in the lower left corner of the terminal. 2. Modify the submission information. 3. Press ESC to exit the editing mode. 4. Switch the input method to English, enter :wq, save the configuration, and exit.Copy the code

Example Modify a commit information in a previous version

In this case, we want to modify the submission record of e35D8B3 joining the network request. The operation is as follows:

1. Query submission records

git log --oneline
Copy the code

Git rebase -i: git rebase -i: git rebase -i: git rebase -i Here commitId should be a05A00E

git rebase -i a05a00e
Copy the code

3. Then the viM mode will pop up and press “I” to enter the editing mode. The goal is to change the policy by changing the first line to r. Press ESC to exit the editing mode, enter :wq, save the configuration, and exit.

4. Press Enter, enter viM editing mode again, and modify the submission record

5, Enter again, modify success!

Organize consecutive commits into one

// TODO

Organize multiple non-consecutive commits into one

// TODO

Delete and restore files

  1. Delete a file from disk, then execute the add&commit operation, and the files on the branch are gone. If the file is deleted incorrectly, use version traversal to restore the file. The above operations are: delete the actions that have been committed and then restore them.
git reset --hard commitID
Copy the code
  1. If you do not have add and commit, you can do the undo change: case one

  2. If the add has been made but the commit has not been made, you can do the undo change: case 2

Compare the difference between two different submissions of a file

Git diff < commit_id1 > < commit_id2 > - < file_name > -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- for example -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the git diff 8740378 303df3b -- app/src/main/java/com/kugou/MainActivity.javaCopy the code

Compare the differences between two different branches of a file

Git diff < branch_1 > < branch_2 > - < file_name > -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- for example -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the git diff master dev  -- app/src/main/java/com/kugou/MainActivity.javaCopy the code

Delete files by deleting files in both the workspace and the staging area

Git rm file name -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- for example -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the git rm app/SRC/main/Java/com/kugou/MainActivity. JavaCopy the code

Branch operation

Create a branch (based on the current branch)

Git branch Specifies the branch nameCopy the code

Switch to the specified branch

Git checkout branch name or Git switch branch nameCopy the code

Create and switch branches

Git checkout -b branch name or git switch -c branch nameCopy the code

Creates a new branch based on the specified branch

Git branch New branch Specifies the branchCopy the code

Create a branch based on a commitId

Git branch A commitId for the new branchCopy the code

Delete the branch

Git branch -d git branch name git branch nameCopy the code

Forcibly Deleting branches

Git branch -d git branch name git branch nameCopy the code

View the current branch

Git branch -v displays the branch name and the last commitCopy the code

View local and remote branches

Git branch -av // Displays the branch name and the last commitCopy the code

Viewing remote branches

git branch -rv
Copy the code

Merge branch A into the current branch and create commitId for the merge

First switch to the current branch before you can merge branch A with the following command. Git mergeA branch git mergeA branchCopy the code

Merge branch A into branch B and create commitId for merge

Git merge A branch B branchCopy the code

Rebase the current branch on the B branch to merge the B branch into the current branch

Git rebase B branchCopy the code

Rebase branch A based on branch B to merge branch B into branch A

Git rebase B branch A branchCopy the code

Tags operate

Viewing existing Labels

git tag
Copy the code

The new label

Git tag v1.0Copy the code

Create a label with remarks

Git tag-a v1.0-mCopy the code

Label the specified COMMIT

Git tag v1.0 commitIdCopy the code

Push a local TAB

Git push origin v1.0Copy the code

Push all unpushed local tags

git push origin --tags
Copy the code

Delete a local label

Git tag - d v1.0Copy the code

Delete a remote label

Git push origin: refs/tags/v1.0Copy the code

The remote interaction

View all remote warehouses

git remote -v
Copy the code

Add a remote repository

git remote add url
Copy the code

Example Delete a remote repository

Git remote remove remote nameCopy the code

Rename the remote repository

Git remote rename git remote renameCopy the code

Pull all remote branch and label changes locally

git fetch remote
Copy the code

Pull changes from the remote branch to the local branch and merge them into the local branch

Git pull Origin Branch nameCopy the code

Push the local branch to the remote end

Git push Origin branch nameCopy the code

Delete the remote branch

Git push remote --delete remote branch nameCopy the code