jinzhe

jinzhe

github
email

Git Advanced

rebase: Resetting the Base Point#

Move the base point of the current branch to the target branch. The benefit of doing this is to keep the main master clear.

git rebase <target branch>

However, after the rebase, the master branch does not follow, so it needs to be merged again:

git checkout master
git merge <just now branch>

This way, the master branch is merged with the branch.

Made a mistake in the recent commit, how to modify it?#

After making the file changes, use --amend, which will create a new commit and replace the last commit.

git add file
git commit --amend -m description

The mistake was in the second to last commit, not the last one, can it still be modified?#

Of course, it can be done by using interactive rebase, with the parameter: -i

git rebase -i HEAD^^

Here, ^^ means to go back a few commits, the number is the quantity of ^, it can also be written as ~2, where the number represents the quantity of steps to go back.

After pressing enter, we enter an editable page. It's worth noting that the order of the commits above is in reverse order. We can see that each commit has a pick in front of it. By pressing i to enter edit mode, change the pick in front of the commit you want to modify to edit. After exiting the interface, we can see that we are on the current commit. After completing the modification, use --amend to submit. After successful submission, we can skip the unnecessary commits using git rebase --continue, and the whole process will be completed.

diff: Comparison#

Used for comparing modifications.

git diff // Compare working directory and staging area
git diff --staged // Compare staging area and the last commit
git diff HEAD // Compare working directory and the last commit

Want to stash the working directory?#

Stash and clear the working directory.

git stash

[!WARNING]

  • git stash does not stash your untracked files. If you have created a new file and haven't added it yet before stashing, this file will not be stashed. To stash these files together, use -u.
  • To restore files, use the git stash pop command.

Want to rollback a commit?#

reset can reset the position of HEAD and the branch

git reset <branch name or SHA-1>
  • --mixed: Default, saves both working directory and staging area as working directory
  • --hard: Does not keep the working directory, your working directory will be cleared!!
  • --soft: Keeps the working directory and staging area
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.