From: juejin. Cn/post / 684490…

Although learned Git before, but with less, many principle is not very clear, so recently bought a video, calm down and learn from zero under the Git, but Git used to use to actually is that a few commands, this article today, I sums up to classify those commands (of course, is refer to the classification of the others). Most of these summaries come from a Git mouse pad I bought in case I forgot to find it later. I believe this Git treasure book must be worth your collection.

Git configuration

If you’re using Git for the first time, you’ll need to start by configuring your identities so that when you commit a task, others know who committed the commit.

Git Minimum Configuration

Configure a global account that is valid for all Git repositories

git config --global user.name 'Your Account name'
git config --global user.email 'your Email'Copy the codeCopy the code

2. Configure a local account that is valid only for the current Git repository

git config --local user.name 'Your Account name' 
git config --local user.email 'your Email'Copy the codeCopy the code

Notice the difference is that one parameter is global and one parameter is local

3. View related configurations

Once configured, it is obvious that sometimes you need to view the information about our current configuration. You can use the following command

1. Check the configuration of the global type

Git config --global --listCopy the code

2. View the configuration of a warehouse

Git config --local --listCopy the code

2. Local basic operations

This part of the command is a little too many, is also the most frequently used command, I will list them one by one, suggest collection

1. Basic operations

1. View the changes

Git status copies the codeCopy the code

2. Check which branch you are currently working on

Git branch -v copies codeCopy the code

3. Switch to the specified branch

Git checkout specifies the name of the branch to copy the codeCopy the code

4. Add all changes in the current directory and subdirectories to the staging area

Git add. //'. '; Copy the codeCopy the code

5. Put all changes in the warehouse into the temporary storage area

Git add-a copies the codeCopy the code

6. Add the specified file to the staging area

Git add file 1 file 2... File n copies the codeCopy the code

Create a formal COMMIT, that is, commit the current data

Git commit Copies the codeCopy the code

2. Compare differences

1. Compare a file workspace with a staging area

Git diff copy code for a fileCopy the code

2. Compare the difference between a file’s staging area and HEAD

Git diff --cache code to copy a fileCopy the code

3. Compare all differences between workspace and staging area

Git diff copies codeCopy the code

4. Compare all differences between the staging area and HEAD

Git diff --cache copy codeCopy the code

3. Roll back between the staging area and the workspace

1. Restore the workspace specified file to the same as the staging area

Git Checkout file 1 file 2... File n copies the codeCopy the code

2, restore the file named in the staging area to the same as HEAD

Git reset file 1 file 2... File n copies the codeCopy the code

3, restore all files in the staging area and workspace to the same as HEAD

Git reset --hard copies codeCopy the code

4. Use DiffTool to compare the differences between any two commits

Git DiffTool commit1 commit2 Copy codeCopy the code

Note that rolling back from the workspace to the staging area uses checkout, or reset

4, other

Check which files are not controlled by Git

Git ls-files --others copies the codeCopy the code

Third, temporary task processing

1. Save the unprocessed changes in stash

Git Stash copies the codeCopy the code

2. Pick up where you left off when temporary tasks are finished

Git stash pop // A pop is the equivalent of popping out of the stack as well as pushing in, popping out the previous task or git stash apply. The difference with a pop is that an apply takes the task off the top of the stack, but removes it from the stack and copies codeCopy the code

3. Look at all stash

Git Stash list copies the codeCopy the code

4. Retrieve the stash change

Git stash pop stash @{numeral n} Copies the codeCopy the code

Modify personal branch history

Each time the contents of our repository change to a COMMIT, a new COMMIT will be generated. However, if we want to change the contents of our repository by modifying the previous COMMIT, we can use the following command

1. Modify the last commit

Amend the file in the workspace. Add git. Commit gitCopy the code

2 |, modify the middle commit (assuming code-named X)

Git rebase -i X: git rebase -i X Git add 4. Git rebase --contiue Copies the codeCopy the code

5. View change logs

1. Each commit of the current branch is displayed in one line

git log--online Copies codeCopy the code

2. Display the latest n commit entries

git log-n Replicates the codeCopy the code

Graphical display of all branch histories

git log--online --graph --allCopy the code

4. View all commits that involve changes to a file

git logA file copy codeCopy the code

5. Modify the corresponding commit and author for each line of a file

Git blame Code for copying a fileCopy the code

Branch and label

1. Create a branch

Create a new branch based on the current branch

Git branch New branch copies codeCopy the code

Creates a new branch based on the specified branch

Git branch New branch An existing branch replicates codeCopy the code

Create a branch based on a COMMIT

Git branch Replicates code with the ID of a commit in the new branchCopy the code

Create a branch and switch to it

Git chechout -b New branch copy codeCopy the code

2. List branches

Listing local branches

Git branch -v copies codeCopy the code

Lists local and remote branches

Git branch-av copies codeCopy the code

List all remote branches

Git branch-rv replicates codeCopy the code

Lists the remote branches of the name symbol for a style

git branch -rv -l 'something style'Copy the codeCopy the code

3. Delete branches

Safely delete a local branch

git branch -dThe branch to be deleted copies the codeCopy the code

Forcibly delete the local branch

Git branch -d Specifies the branch copy code to deleteCopy the code

Delete all local branches that have been merged to the master branch

git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -dCopy the codeCopy the code

Delete all local branches of remote Origin that no longer exist

Git remote Prune OriginCopy the code

4. Label

Label from COMMIT

Git tag Tag name Commit ID of the replication codeCopy the code

Integration between two branches

1, merge branch A into current branch and create commit for merge

Git merge A branch copies codeCopy the code

Merge Merge (A) into (B) and create A COMMIT for Merge

Git merge A branch B branch copies the codeCopy the code

3. Rebase the current branch based on the B branch to merge the B branch into the current branch

Git rebase B branches copy codeCopy the code

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

Git rebase B git rebase A git rebaseCopy the code

5. Resolve conflicts with MergeTool

Git mergetool copies the codeCopy the code

8. Interact with remote devices

1, List all remote

Git remote -v copies the codeCopy the code

2. Add remote

Git remote add url copy codeCopy the code

3. Delete remote

Git remote remove git remoteCopy the code

4, Change the name of remote

Git remote rename git remote renameCopy the code

5. Pull all remote branch and label changes locally

Git fetch remote copies codeCopy the code

6. Merge changes from the remote branch to the local branch

Git pull Remote Name Branch name Copy codeCopy the code

Git pull and Git fetch are different from each other

7. Push the local branch to the remote

Git push remote name Branch name copy codeCopy the code

8. Delete the remote branch

Git push remote --delete remote branch name or git push remote: remote branch name copies codeCopy the code

9. Submit the specified label to the remote end

Git push Remote tag name copy codeCopy the code

10. Submit all labels to the remote end

Git push Remote -- Tags copy codeCopy the code

conclusion

If you master these commands, git will come easily. However, many commands are easy to forget, so you still need a Git manual. When you use them in the future, you will be able to remember them.

But only command does not know the principle of it is not good, as for the principle, you can find online articles slowly understand, of course, if necessary, I will share some principles of Git later, so that you can read git from the essence.