diff --git a/2019/2019-09-24_advancedGitTraining/slides/amend.md b/2019/2019-09-24_advancedGitTraining/slides/amend.md index b419db3f0d2543c6a99980534a944930b0032b05..9f4974cac975de37472391b27204a8b1afeb316b 100644 --- a/2019/2019-09-24_advancedGitTraining/slides/amend.md +++ b/2019/2019-09-24_advancedGitTraining/slides/amend.md @@ -52,10 +52,13 @@ $ git status * With the changes staged use the following command to commit the changes into the previous commit: ```bash -$ cd ../ # change directory one up if required -$ git add attendees/myName.md +$ git add myName.md $ git commit --amend --no-edit ``` +* Check the commit content: +```bash +$ git show HEAD +``` + * This will create and commit a new commit with the staged changes added and the same commit message. -* (Force) push your changes to your branch `myBranch` diff --git a/2019/2019-09-24_advancedGitTraining/slides/chPick.md b/2019/2019-09-24_advancedGitTraining/slides/chPick.md index 63d3bdbc3016c660a8a08b6bb04430863d4d0a80..f865ef90cf65445ac76102a5573bfe95b20fea20 100644 --- a/2019/2019-09-24_advancedGitTraining/slides/chPick.md +++ b/2019/2019-09-24_advancedGitTraining/slides/chPick.md @@ -1,7 +1,7 @@ # Cherry-picking -* Cherry-pick allows to pick one (or more) specific commits from a list of commits. -* Cherry picking only picks the selected commit(s), not everything up to that commit. +* Cherry-picking allows to pick one (or more) specific commits from a list of commits. +* Only the chosen commit(s) are picked, not everything up to that commit. <div style="top: 8em; left: 25%; position: absolute;"> <img src="slides/img/cherryPick.png" height=500px> @@ -11,8 +11,9 @@ # Example (1) -* Create and commit two files in the `develop `branch +* Create and commit two files in the `develop ` branch ```bash +$ git checkout develop $ echo "# Venue details" > location.md $ # add and commit the file location.md $ echo "# Speakers" > speakers.md @@ -46,26 +47,37 @@ $ git push origin myBranch # Partial chery-picking * Partial cherry-picking allows you to unpack the changes from a commit. + * Imagine you committed many files, and you want to remove certain files. + * In practice: + - You commited all files, and you realize that there is your data inside! + - You have committed accidentally sensitive data, such as your password + - You committed hidden files, for instance `.DS_Store` files + - ... + # Example (1) +* Hard reset the `myBranch` branch: +```bash +$ git checkout myBranch +$ git reset --hard HEAD~2 # do not preserve files +``` + * Reset the `develop` branch: ```bash $ git checkout develop -$ git reset HEAD~2 +$ git reset HEAD~2 # preserve files ``` * Add the `location.md` and the `speakers.md` files as 1 commit: ```bash -$ echo "# Venue details" > location.md -$ echo "# Speakers" > speakers.md $ git add location.md speakers.md $ git commit -m "add location and speakers files" ``` @@ -83,7 +95,7 @@ $ git status ``` Now, remove the file `location.md`: ```bash -$ git restore --staged location.md +$ git restore --staged location.md # old version of git: $ git reset HEAD location.md $ rm location.md ``` Commit the changes: diff --git a/2019/2019-09-24_advancedGitTraining/slides/gettingStarted.md b/2019/2019-09-24_advancedGitTraining/slides/gettingStarted.md index b64e2b85ac5a4dbc8b939c48b807715742b4fcbd..bfaf86e10481897bda7a15e91de88bbe4edfa92d 100644 --- a/2019/2019-09-24_advancedGitTraining/slides/gettingStarted.md +++ b/2019/2019-09-24_advancedGitTraining/slides/gettingStarted.md @@ -1,37 +1,54 @@ -# Getting Started - -Fork and then clone the tutorial repository -<a href="https://git-r3lab.uni.lu/R3/school/git/advanced-practice">https://git-r3lab.uni.lu/R3/school/git/advanced-practice</a> +# Getting Started (1) +Make sure that your git is configured properly: ```bash -$ git clone ssh://git@git-r3lab-server.uni.lu:8022/<first.last>/advanced-practice.git +$ git config --global user.name "Firstname Lastname" +$ git config --global user.email "first.last@uni.lu" ``` -Add a remote upstream -```bash -$ cd advanced-practice +Test whether your username and email have been registered -# add upstream URL -$ git remote add upstream ssh://git@git-r3lab-server.uni.lu:8022/R3/school/git/advanced-practice.git -$ git fetch upstream -``` -Check the remotes ```bash -$ git remote -v +$ git config --list ``` -Create your own branch `myBranch` based on the `develop` branch from `usptream` using the `-b` flag + +Fork and then clone the tutorial repository +<a href="https://git-r3lab.uni.lu/R3/school/git/advanced-practice">https://git-r3lab.uni.lu/R3/school/git/advanced-practice</a> + ```bash -$ git checkout -b <mybranch> upstream/develop +$ git clone ssh://git@git-r3lab-server.uni.lu:8022/<first.last>/advanced-practice.git ``` -# Installing the mergetool `kdiff3` +# Getting Started (2) -* Download it here: http://kdiff3.sourceforge.net/ +* Please generate your SSH before with `$ ssh-keygen -t rsa` and set it in Gitlab! + +* Add a remote `upstream` + ```bash + $ cd advanced-practice + # add upstream URL + $ git remote add upstream ssh://git@git-r3lab-server.uni.lu:8022/R3/school/git/advanced-practice.git + $ git fetch upstream + ``` +* Check the remotes with: + ```bash + $ git remote -v + ``` + +* Create the `develop` branch and your own branch `myBranch` based on the `develop` branch from `upstream` using the `-b` flag + ```bash + $ git checkout -b develop upstream/develop + $ git checkout -b myBranch + ``` -* The downloadable file should match your OS + + +# Install the mergetool `kdiff3` + +* Download it here: http://kdiff3.sourceforge.net/ * Setting up `kdiff3`: @@ -42,6 +59,13 @@ $ git config --global --add mergetool.kdiff3.path "<kdiff3 path>" * omit `""` when setting up on Linux or macOS +**Note**: On UNIX, you can find the path of `kdiff3` by typing: +```bash +$ which kdiff3 +``` +On Windows, the path might be `C:/Program Files/KDiff3/kdiff3.exe`. + + # A note on common commands: diff --git a/2019/2019-09-24_advancedGitTraining/slides/rebase.md b/2019/2019-09-24_advancedGitTraining/slides/rebase.md index b4a262c6cdf8815fe578742ea39e7ab27922ac4c..8fc767e99eb86728f0b861896a443c9ff3078568 100644 --- a/2019/2019-09-24_advancedGitTraining/slides/rebase.md +++ b/2019/2019-09-24_advancedGitTraining/slides/rebase.md @@ -1,6 +1,6 @@ # Rebasing (1) -* Git rebase enables to forward your commits +* `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) @@ -26,10 +26,11 @@ Imagine the following situation: # Example (1): +* A merge request against `develop` is still open. Repository maintainer: review, and merge it. + * Create a file in your branch `myBranch` ```bash $ git checkout myBranch # if necessary -$ cd attendees $ echo "# List of attendees" > list.md $ # add and commit the file ``` @@ -68,6 +69,8 @@ $ git log # Interactive Rebasing - flag `-i` +* An interactive rebase is performed with the `-i` flag: + ```bash git rebase -i <branch> ``` @@ -88,22 +91,24 @@ git rebase -i <branch> # Example 1: Reword and fixup (1) -* Create and switch to your own branch +* Switch to your own branch `myBranch` * Add and commit two files to this branch: ```bash -$ cd attendees +$ # git checkout myBranch && 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 +$ git push origin myBranch ``` 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 + - Omit the commit message of the second commit. @@ -111,41 +116,32 @@ Now, we want to: # Example 1: Reword and fixup (2) -Perform an interactive rebase with the two last commits: +* Perform an interactive rebase with the two last commits: ```bash $ git rebase -i HEAD~2 ``` -The prompt shows up: +* The dialog shows up (example): ```bash $ pick 1234567 add william to attendee list $ pick abcdef0 add roberta to attendee list ``` -The keywords `pick` can now be changed to `reword` and `fixup` respectively: +* The keywords `pick` can now be changed to `reword` and `fixup` respectively: ```bash $ 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 -``` - -Then use force-push to push your changes: -```bash -$ git push origin myBranch -f -``` +* Edit by typing `i` +* Change the message of commit `1234567` to `Add William and Roberta to the attendee list` +* Save with `:wq` # Example 2: Pick and squash (2) -* If you wante to keep both commit messages in one commit, change the action to `squash` +* If you want to **keep** both commit messages in one commit, change the action to `squash` ```bash $ pick 1234567 add william to attendee list $ squash abcdef0 add roberta to attendee list diff --git a/2019/2019-09-24_advancedGitTraining/slides/reset.md b/2019/2019-09-24_advancedGitTraining/slides/reset.md index 4f72b8a4cb2b1f9650423f349a7f58ce3ad69842..02f423eb1dd99da0bf6bcdaf9908e853cef66c78 100644 --- a/2019/2019-09-24_advancedGitTraining/slides/reset.md +++ b/2019/2019-09-24_advancedGitTraining/slides/reset.md @@ -1,6 +1,6 @@ # Reset a branch -* Enables to reset back to a previous commit +* Enables to reset a branch back to a previous commit * Discards ALL commits made after the selected commit HEAD * This happens often in **practice**: @@ -12,10 +12,11 @@ * Start by committing two files: ```bash -$ cd attendees +# commit first file ... $ echo "# CV of Firstname Lastname" > myCV.md $ git add myCV.md $ git commit -m "add cv for Firstname Lastname" +# commit second file ... $ echo "# Biography of Firstname Lastname" > myBio.md $ git add myBio.md $ git commit -m "add biography for Firstname Lastname" @@ -31,25 +32,28 @@ $ git log # Example: Hard reset of a branch (2) -* Use the `reset --hard` command in order to undo the faulty commit: +* Use the `reset --hard` command in order to undo the commit with `<SHA1>: ```bash $ git reset --hard <SHA1> ``` * Check what happened in the log -* Force push your branch (overwrite the history) +* Force push your branch (overwrite the history) with `-f`: +```bash +$ git push origin myBranch -f +``` # Notes -Alternatively, you can also remove the last commit: +* Alternatively, you can also remove the last commit: ```bash $ git reset --hard HEAD~1 ``` -With a `--hard` reset, the index and the working tree are reset. +* With a `--hard` reset, the index and the working tree are reset. -If you omit the `--hard` flag, a mixed reset is made. This resets the index, but not the working tree +* If you omit the `--hard` flag, a mixed reset is made. This resets the index, but not the working tree ```bash $ git reset HEAD~1 ``` \ No newline at end of file diff --git a/2019/2019-09-24_advancedGitTraining/slides/revert.md b/2019/2019-09-24_advancedGitTraining/slides/revert.md index 1abc41c93f638f6452839710fed1d16c802d34c0..da6254678d153d10ae7321da9c1e9c2c86065ab5 100644 --- a/2019/2019-09-24_advancedGitTraining/slides/revert.md +++ b/2019/2019-09-24_advancedGitTraining/slides/revert.md @@ -1,6 +1,7 @@ # Reverting a commit * Enables the deletion of committed commits by reverting the changes. + * A trace is kept in history of the original commit and the reverted one. @@ -9,17 +10,15 @@ * On your branch, create and commit a file: ```bash -$ cd attendees $ echo "# Grades for Firstname Lastname" > grades.md $ git add grades.md $ git commit -m "File with grades for Firstname Lastname" ``` -* check the commit log and copy the SHA1: -```bash -$ git log -``` -* Use the `git revert` command to undo this selected commit: +* Note down the `SHA1` by checking the `log` + +* Use the `git revert` command to undo that commit: ```bash -$ git revert <sha1> +$ git revert <SHA1> ``` +* This will open a dialog (`vim`-like editor). Exit with `:wq` \ No newline at end of file