links:: Source Control for Test Automation with Git MOC
5.2 Cherry-picking, resetting, reverting
g
Cherry-picking in git
Task:
- Jessica shall create new branch and implement new test.
- Lisa shall create new branch and implement new test.
- Lisa shall integrate new test from commit made by Jessica in a separate branch into her own branch.
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:
- 0dda7fc / 0dda7fccb01c30e9e8795e60add94eaf42386bf0
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:
- Create new branch.
- Commit new test.
- Practice 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).
- 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.
- 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.
- 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
Reverting in git
Task:
- Create new branch.
- Commit new test.
- 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