Git should save more developers ' jobs than other technologies. As long as you often use Git to save your work, you'll always have the opportunity to return the code to the previous state, so you can undo the mistakes you made in the middle of the night.
In spite of that, Git's command-line interface is a tricky one to grasp. Next, give you 7 tips to maximize the role of Git.
Typically, most of the time we use only add, commit, Branch, and Push/pull commands. Most people are familiar with the set of workflows that operate in one direction only. Have you ever thought about how you could cancel a previous operation if you added the wrong file to the warehouse or submitted the code to the wrong branch and the submission was wrong? If you are doing the same as depicted in the comics above (that is, deleting the local project folder and then downloading the warehouse again), then you need to know the following git tips.
Seven Git tricks you can't ignore
1. Modify the wrong submit information commit message
The submissions will remain in your code base for a long time, so you definitely want to get a good idea of how the code is being modified through this information. The following command allows you to edit the most recent submission, but you must make sure that no changes are made to the current code base working copy, or the changes will be submitted together.
$ git commit--amend-m "your-new-commit-message"
If you have already submitted the code (GIT push) to the remote branch, you will need to force the push of this code submission through the command below.
$ git push <remote> <branch>--force
You can focus on this question and answer on the stack Overflow website for more details.
2. Undo git Add before submitting
If you add some wrong files to the registers staging area, no code has been submitted yet. You can use a simple command to undo it. If you only need to remove a file, enter:
$ git reset < filename >
Or if you want to remove all uncommitted modifications from the registers:
$ git reset
You can focus on this question and answer on the stack Overflow website for more details.
3. Undo the most recent code submission
Sometimes you may accidentally submit the wrong file or omit something from the beginning. Here are three steps to help you solve this problem.
$ git reset--soft head~1
# make the necessary changes to the working file
$ git add-a.
$ git commit-c orig_head
When you execute the first command, Git moves the head pointer back to the previous commit before you can move the file or make the necessary changes.
You can then add all the changes, and when you execute the last command, Git opens your default text editor, which contains the information from the last commit. If you want, you can modify the submission, or you can skip this step by using-C instead of-C in the final command.
4. Git warehouse revocation to the previous commit state
"Undo" revert is very necessary in many cases-especially if you mess up the code. The most common scenario is that you want to go back to the previous code version, check the code base at that time, and then go back to the current state. This can be achieved by using the following command:
$ git checkout <SHA>
"" is the first 8 to 10 characters in hash code that you would like to view for a commit. This command will leave the <HEAD> pointer out of the detach, allowing you to view the code without checking out any of the branches--leaving head is not as scary as it sounds. If you want to commit the changes in this case, you can do so by creating a new branch:
$ git checkout-b <SHA>
To get back to your current work schedule, just check out the branch you were in before checking out.
You can focus on this question and answer on the stack Overflow website for more details.
5. Withdrawal of merge merges
To undo a merge, you may have to use the RESTORE command hard reset back to the last commit state. The work done by "merging" basically is resetting the index, updating the different files in the working tree (the work trees), the different files in the current commit () code from the head cursor, but the merge retains the index and working Part of the difference between the tree (for example, modifications that are not tracked).
$ git checkout-b <SHA>
Of course, there are always other implementations of git that you can view to see this article and continue to understand.
6. Remove an untraceable local file from the current Git branch
Suppose you happen to have some files that are not being tracked (because they are no longer needed) and do not want to be displayed each time you use the git status command. Here are some ways to solve this problem:
$ git clean-f-N # 1
$ git clean-f # 2
$ git clean-fd # 3
$ git clean-fx # 4
$ git clean-fx # 5
(1): Option-n will show which files will be removed when execution (2) is performed.
(2): This command removes the file displayed in all commands (1).
(3): If you also want to remove the file, please use the option-D.
(4): If you only want to remove files that have been ignored, use option-X.
(5): If you want to remove a file that has been ignored and not ignored, use option-X.
Note the difference between x in the last two commands.
For more information, check out the official documentation for Git-clean's introduction.
7. Delete local and remote git branches
To delete a local branch:
$ git branch--delete--force <branchName>
Or use option-D as a shorthand:
$ git branch-d
To delete a remote branch:
$ GIT push origin--delete <branchName>
Recommendation: To better grasp the usage of git, please read the GIT official documentation carefully.
Through this article, I hope to help to have the need of friends, thank you for your support of this site!