links:: Source Control for Test Automation with Git MOC

5.2 Cherry-picking, resetting, reverting

g

Cherry-picking in git

Task:

  1. Jessica shall create new branch and implement new test.
  2. Lisa shall create new branch and implement new test.
  3. Lisa shall integrate new test from commit made by Jessica in a separate branch into her own branch.

Pasted image 20230104221010.png|500

Pasted image 20230105124917.png|500
Pasted image 20230105125009.png|500

Cherry-picking

Take some specific changes in a commit from one branch and apply them to another branch. That is exactly what cherry-picking allows us to do, either forcing a commit or a series of commits.

Jessica commit:

Create New remote Branch and Make Changes "feature/test10-jessica-remote-cherrypicking"

Create Local Branch "feature/test11-lisa "

git checkout -b feature/test11-lisa

Make Changes

git status git add -A git commit git log --oneline git fetch

Find Commit SHA 1) in Github 2) in VS Code Commit Graph

git cherry-pick 0dda7fccb01c30e9e8795e60add94eaf42386bf0 git log --oneline                                          

Resetting in git

Task:

  1. Create new branch.
  2. Commit new test.
  3. Practice resetting.

Pasted image 20230105125227.png|500

Resetting

We want to have back the first version of the README file.
Exactly for that purpose, we can use resetting, to reset the HEAD pointer of our active branch, so that it points to the commit with the first version of the README file (blue).

Reset Soft

  • Command: git reset --soft HEAD~1
  • Local repository (commits): move HEAD pointer to previous commit
  • Staging area: untouched
  • Working directory: untouched
  • Description: reset to previous commit and leave working and staging areas untouched. We still have our changes in working directory.

Reset Mixed

  • Command: git reset --mixed HEAD~1
  • Local repository (commits): move HEAD pointer to previous commit
  • Staging area: replace changes in staging area with previous commit
  • Working directory: untouched
  • Description: reset to previous commit (local repository and staging area) and leave working area untouched. We still have our changes in working directory.

Reset Hard

  • Command: git reset --hard HEAD~1
  • Local repository (commits): move HEAD pointer to previous commit
  • Staging area: replace changes in staging area with previous commit
  • Working directory: replace changes in working area with previous commit
  • Description: reset to previous commit (local repository, staging and working area). We still can find the commit existed before resetting using reflog command.

git checkout master                                                               git checkout -b feature/test12-jessica                                            git commit -am "add Test12-lisa-reset-commit1"                                    git commit -am "add Test12-lisa-reset-commit2"                                    git log                                                                           git reset --soft HEAD~1                                                           git log --oneline                                                                 git status                                                                        git commit -am "add Test12-lisa-reset-commit2"                                    git log --oneline                                                                 git reset --mixed HEAD~1                                                          git status                                                                        git add  README.md                                                                git commit -m "add Test12-lisa-reset-commit2"                                     git reset --hard HEAD~1  

Pasted image 20230105132545.png|500

Pasted image 20230105132553.png|500

Pasted image 20230105132601.png|500

Reverting in git

Task:

  1. Create new branch.
  2. Commit new test.
  3. Practice resetting.
git checkout master                                                                  git checkout -b feature/test13-lisa   
# add new file `test-reverting.md`
git add -A                                                                           git commit -m "add Test13-lisa-reverting"                                            git log                                                                              git revert 1bef5ba9718069f0f9ac3357fe1f4ce63f7d4811                                  git log   
# `test-reverting.md` is deleted

Pasted image 20230105133816.png|500