diff --git a/2019/2019-09-24_advancedGitTraining/slides/rebase.md b/2019/2019-09-24_advancedGitTraining/slides/rebase.md index faa0439057e081dbaf2b39001268422a8638e4ee..7e8e9c1e050b7830afa86f458fb8029d5fb3bd5d 100644 --- a/2019/2019-09-24_advancedGitTraining/slides/rebase.md +++ b/2019/2019-09-24_advancedGitTraining/slides/rebase.md @@ -1,39 +1,45 @@ -# Theory: Git Rebasing +# Rebasing (1) -* Git rebase enables to keep up with changes made to a branch -* Straightens the workflow +* Git rebase enables to forward your commits +* Move/combine a sequence of commits to a new base commit * Avoid discrepancies when multiple people work on the same project +* Linear git history (no merge commits) +* Rebasing is like saying, “I want to base my changes on what everybody has already done.†Imagine the following situation: <div style="top: 10em; left: 30%; position: absolute;"> - <img src="slides/img/beforeRebase.png" height="400px"> + <img src="slides/img/beforeRebase.png" height="500px"> </div> -# Theory: Git Rebasing +# Rebasing (2) -* Develop branch is several commits ahead of master. -* Commits implemented into master that aren't in develop. +* `myBranch` is several commits ahead of `develop`. +* Commits implemented into `develop` that aren't in `myBranch`. <div style="top: 10em; left: 30%; position: absolute;"> - <img src="slides/img/afterRebase.png" height="400px"> + <img src="slides/img/afterRebase.png" height="500px"> </div> -# Practical: Git Rebasing +# Example (1): -* Create a file in your branch +* Create a file in your branch `myBranch` ```bash -$ git checkout myBranch -$ echo "# message" > yourName.md +$ git checkout myBranch # if necessary +$ cd attendees +$ echo "# List of attendees" > list.md +$ # add and commit the file ``` -* Then create a file in the develop branch +* Then, update your `develop` branch from the `upstream` remote: ```bash +$ git fetch upstream $ git checkout develop -$ touch startRebase.txt +$ git merge upstream/develop +$ git checkout myBranch ``` * Check the histories of both branches @@ -43,29 +49,31 @@ $ git log -# Practical: Git Rebasing +# Example (2): -* rebase the develop branch onto your branch +* Rebase `myBranch` on top of the updated `develop`: ```bash $ git checkout myBranch $ git rebase develop ``` -* check the history of your branch again +* Check the history of your branch again ```bash $ git log ``` +* If you pushed previously your branch `myBranch`, you need to rewrite its history remotely - you need to **force push**. -# Theory: Git Interactive Rebasing + +# Interactive Rebasing - flag `-i` ```bash git rebase -i <branch> ``` * Enables more precise control over the rebased commits -* Before committing many actions are at disposal +* Before committing many actions are available ```bash # p, pick = use commit @@ -78,54 +86,49 @@ git rebase -i <branch> -# Practical: Git Interactive Rebasing +# Example (1) * Create and switch to your own branch * Add and commit two files to this branch: ```bash -$ echo "# Michael" > Michael.md -$ git add Michael.md -$ git commit -m "Add Michael to the list of attendees" -$ git push origin yourBranch - -$ echo "# Kevin" > Kevin.md -$ git add Kevin.md -$ git commit -m "Add Kevin to the list of attendees" +$ cd attendees +$ echo "# William Odell" > william.md +$ # add and commit the file william.md with the message 'add william to attendee list' +$ echo "# Roberta Ross" > roberta.md +$ # add and commit the file roberta.md with the message 'add roberta to attendee list' $ git push origin yourBranch ``` +Now, we want to: + +- Reword the first commit's message to: `Add William and Roberta to attendee list` +- Combine the second and first commit into one, omitting the commit message of the second commit. -# Practical: Git Interactive Rebasing -* Perform an interactive rebase with the two last commits: +# Example (2) + +Perform an interactive rebase with the two last commits: ```bash $ git rebase -i HEAD~2 ``` -* reword the first commit's message to: -`These are the two squashed commits` -* combine the second and first commit into one, -omitting the second's commit message. - - - -# Practical: Git Interactive Rebasing - The prompt shows up: ```bash -$ pick 1234567 add Micheal to attendee list -$ pick abcdef0 add Kevin to attendee list +$ pick 1234567 add william to attendee list +$ pick abcdef0 add roberta to attendee list ``` -The keywords should be changed to: +The keywords `pick` can now be changed to `reword` and `fixup` respectively: ```bash -$ reword 1234567 add Micheal to attendee list -$ fixup abcdef0 add Kevin to attendee list +$ reword 1234567 add william to attendee list +$ fixup abcdef0 add roberta to attendee list ``` +**Note:** The SHA1s of each commit are different. + Upon confirming, change the message of commit 1234567: ```bash $ Add Micheal and Kevin to the attendee list