Git is a very powerful tool with a wealth of tools to maintain projects. This article describes some of the git practical tips and commands that Git uses during its daily use, and hopes that it will help everyone.
(Read first: The Birth of Git – The Version Control System )
Seven Git practical tips and command summary
Normally, we will complete the requirements development in our own branch, and there will be a need to compare our own branches with other branches. This feature can be passed
Git diff branch1 branch
Command to achieve.
If you want to compare the scratchpad with the current HEAD, then use
Git diff --cached
The command will be very convenient. The normal
git diff command compares by default the files that are not added to the index.
Recovery staging area
If you have added some files to the staging area and regret it, Git provides several commands to implement this function, depending on the situation at the time.
Git rm path/to/file --cached
This command removes the file from the scratchpad index, but still keeps the file in the working directory. This is more than direct use
Git rm file -f
It is safer to completely delete files.
If you want to restore some of the changes that have already been submitted, we can use
This command can perform many different behaviors, so it needs to be used according to the actual scenario.
If you want to remove all modifications, including the contents of the index and the changes in the working directory, you can use
Git reset --hard
If you just want to reset the index, you can use
Git reset --mixed
This is also the default behavior of the
git reset command. A mixed reset will preserve the changes in the current working directory. Finally, if you only want to modify the HEAD of the branch, you can pass
Git reset --soft
When running the
git reset command, we can specify multiple target files to pass in as parameters. Of course you can pass
Git reset --hard COMMIT_ID
Restore to the specified commit version.
You should be familiar with the
git stash command, which can be easily restored with the
git stash pop command. However, if there are untracked files in the working directory, they are not stored in the temporary storage area by default. In order to temporarily save untracked files, you can use
Git stash --include-untracked
Another very useful command is
Git stash list
It can list the contents of the temporary storage area.
Git comes with a very powerful tool to view project and changes to specific files. I personally like to use one of these commands:
Git log --graph --decorate --oneline
It can be used to display a modified commit history. This command is very verbose, so I suggest you can create an alias for it (this is probably the most useful of all the tricks, because many commands are hard to remember). The git log command can display HEAD, all submitted IDs, and branch information. With this information, we can use
Git show COMMIT_ID/HEAD/BRANCH
To display more detailed information.
Sometimes we need to know who made changes to a file, this is exactly
Git blame path/to/file
The functionality provided by this command.
I mentioned the
git diff command before, it is also a tool for viewing history. For example, if you need to compare the current HEAD and the first two commits, you can use
Git diff HEAD HEAD~2
In order to be able to display more detailed updates in each submission, you can use
Git log --patch
If you only want to see the submission containing the keyword “apple”, use
Git log --grep apples --oneline
To see the commit history between two points in the history submission record, we can use
Git log HEAD~5..HEAD^ --oneline
Can be used for branches
Git log branch_name..master --oneline
Fix bug submission
Note: The following commands will modify the commit history, please make sure to understand before using .
When submitting an error, we may want to be able to modify the commit history. I don’t recommend modifying the commit history that has been pushed to the remote repository (even if git allows it), but for the commit history of the local repository, I personally think it can be modified. by
Git commit --amend
You can delete the previous commit and create a new commit record to replace the previous commit.
Another git trick I like very much is interactive rebase. It can be used to edit submissions or to compress multiple submissions into one submission, which is one of my favorite features. In order to perform interactive rebase on all commits after the master branch of the remote repository origin, you can use
Git rebase -i origin/master
This command displays a detailed description of the submission list and the actions that can be performed. For example, the following operation will compress multiple submissions into one:
1 pick 80f2a48 Add feature X 2 squash 2c74ea2 Add junit tests for feature X 3 squash 4k81nm5 Bugfix for feature X
The end result will be a commit that submits a message with “Add feature X”.
If you need to recover a problematic submission, we can use
Git revert COMMIT_ID
This command creates a new commit that restores the current project state to the specified commit.
If we have mishandled the problem, we accidentally deleted the file that should not be deleted. We can still recover from the repository because git saves all modified versions, including the removed commits.
git reflog command is used to implement this function.
Picking submission (cherry-pick)
Suppose we and our colleagues are developing on separate branches. Colleagues have an important commit and we want to apply to their own branch, but do not need other commits from the other branch. At this time we can use
Git cherry-pick COMMIT_ID
These are my favorite git utility tips. I hope that you can also learn some new knowledge. These are very useful commands I found in everyday use and they are very useful for my daily work. If you have similar git utility tips or common commands, you can share them and share them.