Home > Article > Development Tools > You may not know these useful Git functions yet!
1. The code I just submitted, I found that it needs to be fine-tuned
I just submitted a piece of code, and then told the front end that the interface is ready . After 2 minutes, the front end tells you, hey, brother, can that amount be returned to an integer, without decimal points?
At this time, we usually modify it and then submit a version. Then after another 2 minutes, the front end came again and said, hey, brother, can the format of that date be changed...
Normally we will modify it and submit a version again, so just some simple changes , we have submitted several versions, yes, but there is a more elegant and simple solution: commit --amend
"amend" means "amend" . When submitting, if you add the --amend parameter, Git will not add a commit to the current commit, but will merge the contents of the current commit with the contents of the staging area to create a new commit. , replace the current commit with this new commit. So commit --amend does exactly what it sounds like: amends the latest commit.
Specifically, after you have made the changes, run directly:
git add . git commit --amend
The information you submitted before will appear:
You can modify or keep it, then save and exit. Your latest commit will be updated, as shown in the figure below:
2. What needs to be modified is not the latest commit, but the second to last
Suddenly, our penultimate submission needs some minor adjustments, so at this time we cannot use the above processing method, but there is a way.
rebase -i
: It is the abbreviation of rebase --interactive
, which means "interactive rebase".
After git log, we found that the penultimate commit needs to be modified, so at this time we can run:
git rebase -i HEAD ^^
, the following interface will pop up.
The top of this editing interface lists all the commits that will be "rebase", that is, the penultimate commit "modify testgit" and the latest commit " mofify .gitignore". It should be noted that this arrangement is in positive order, with old commits at the top and new commits at the bottom.
At this time, if we need to modify which commit, we can change the pick before the commit to edit, and then exit this interface.
The above interface tells us that the rebase process has stopped at the commit that needs to be modified, and then we can make modifications. After the modification:
git add . git commit --amend
After uploading the modifications, you can run:
git rebase --continue
If there are no conflicts, then the rebase is perfect.
3. Emergency: stash storage
When you are typing on the keyboard and immersed in developing a new feature, someone suddenly comes over and follows you. You say, hey, brother, there is a bug in the function you developed before, please fix it quickly. At this time, your new branch function has just started. It is definitely OK to commit it directly, but there is a better solution. Method - git stash .
git stash will clear the changes in your working directory and store them in another place. It should be noted that git stash will ignore files that are not tracked. At this time, you need to add the parameter -u, that is, git stash -u.
After you fix the bug, switch back to the working branch. Then:
git stash pop
The things you stored before are back. Isn’t it very convenient?
This article comes from the git tutorial column, welcome to learn!
The above is the detailed content of You may not know these useful Git functions yet!. For more information, please follow other related articles on the PHP Chinese website!