This is the third day of my participation in the August More text Challenge. For details, see: August More Text Challenge

preface

Reset Revert rebase: Reset Revert rebase: Reset Revert rebase Every time it is used, it is also careful, step-by-step operation of the document. Today I’ll summarize the scenarios and how to use these three commands.

The body of the

Reset revert rebase Reset revert rebase reset revert rebase reset revert rebase reset revert rebase reset revert rebase But there is a big difference in function.

Data preparation

Create git environment
mkdir demo; cd demo ; git init

# Create branch reset
git checkout -b dev

echo AAAAA > reset.txt
git add reset.txt
git commit -m "AAAAA"

echo BBBBB >> reset.txt
git add reset.txt
git commit -m "BBBBB"

echo CCCCC >> reset.txt
git add reset.txt
git commit -m "CCCCC"

echo DDDDD >> reset.txt
git add reset.txt
git commit -m "DDDDD"


# git log
$ git log --pretty=format:"%h %s" --graph

* d6212d8 DDDDD
* e807553 CCCCC
* 3609018 BBBBB
* a9fb808 AAAAA

# at this point the file is as follows
$ cat reset.txt
AAAAA
BBBBB
CCCCC
DDDDD
Copy the code

git reset

This command is used to roll back the version. You can specify a version to be rolled back. The common mode is as follows:

  • --mixedDefault parameters roll back the version and put the changes into the workspace
  • --softRoll back the version and put the changes into the staging area
  • --hardRoll back the version and discard the changes
# Back to previous version
$ git reset HEAD^ 
# specifies the file to be rolled back to the previous version
$ git reset HEAD^ hello.php 
# Rollback to the specified version
$ git  reset  3609018 
Copy the code

For example, the demo warehouse above now needs to go back to the BBBBB commit:

$ git reset --hard f64fbe6
HEAD is now at f64fbe6 BBBBB

$ cat reset.txt
AAAAA
BBBBB

$ git log --pretty=format:"%h %s" --graph
* f64fbe6 BBBBB
* 17a6e45 AAAAA
Copy the code

git revert

This command is consistent with the reset command. Note that the commit and history before and after this operation are retained, and the undo is treated as an up-to-date commit. For example, to implement the Demo repository, now you need to go back to the BBBBB commit and the corresponding operation is:

# 1. Perform undo to commit
$ git revert f64fbe6
error: could not revert f64fbe6... BBBBB

# 2. Resolve conflicts

# 3. Commit the changes
$ git commit -am "revert commit"

# Verify results
$ cat reset.txt
AAAAA
BBBBB

$ git log --pretty=format:"%h %s" --graph
* a6fa241 revert commit
* 7dedf32 DDDDD
* a513f6c CCCCC
* f64fbe6 BBBBB
* 17a6e45 AAAAA
Copy the code

Git Revert keeps the full commit history and does not change the project history. This is a safe operation for commits that have already been posted to a shared repository.

Of course, the revert can also specify a rollback record, such as two BBBBB and CCCCC commits:

Pay attention togit diff 17a6e45.. a513f6cOpen left and close right.

# Diff first to see the corresponding differencegit diff 17a6e45.. a513f6cCopy the code
# back revert
$ git reset --hard HEAD^

# Resolve conflicts

# View logs
$ git log --pretty=format:"%h %s" --graph
* 81a5d64 revert BC
* 7dedf32 DDDDD
* a513f6c CCCCC
* f64fbe6 BBBBB
* 17a6e45 AAAAA
Copy the code

git rebase

This command is a very powerful command with many functions. The main function is to modify the Commit history of Git. Common scenarios include merging commit records and modifying commit messages.

Git Revert uncommits BBBBB and CCCCC

# Diff first to see the corresponding differencegit diff 17a6e45.. a513f6cCopy the code
# rebase the first three commits
$ git rebase -i HEAD~3

# change the commit
d 515cfeb BBBBB
d 879218f CCCCC
pick 6a664ef DDDDD

Git add reset.txt

# Rebase (default save after popup, if you need to modify the commit log directly edit)
git rebase --continue 

# Check the commit log
$ git log --pretty=format:"%h %s" --graph
* 989618e DDDDD
* 17a6e45 AAAAA

Copy the code

conclusion

  • Git Reset Best scenario Local commit, usually to roll back to the specified version.

  • Git Rebase allows you to add, modify, and delete commit logs. It is generally used to show a perfect commit log and facilitate code review.

  • Git Revert can delete more than one commit and add a commit log. Commit records for remote repositories. Record the history of all changes for review of the commit log.