Skip to content
Snippets Groups Projects
Commit 54fa54bb authored by Laurent Heirendt's avatar Laurent Heirendt :airplane:
Browse files

Merge branch 'advanced-training' into 'develop'

review of advanced training slides

See merge request !45
parents 8b563ae0 4e5353e7
No related branches found
No related tags found
No related merge requests found
...@@ -52,10 +52,13 @@ $ git status ...@@ -52,10 +52,13 @@ $ git status
* With the changes staged use the following command to commit the changes into the previous commit: * With the changes staged use the following command to commit the changes into the previous commit:
```bash ```bash
$ cd ../ # change directory one up if required $ git add myName.md
$ git add attendees/myName.md
$ git commit --amend --no-edit $ 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. * 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`
# Cherry-picking # Cherry-picking
* Cherry-pick allows to pick one (or more) specific commits from a list of commits. * Cherry-picking 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. * Only the chosen commit(s) are picked, not everything up to that commit.
<div style="top: 8em; left: 25%; position: absolute;"> <div style="top: 8em; left: 25%; position: absolute;">
<img src="slides/img/cherryPick.png" height=500px> <img src="slides/img/cherryPick.png" height=500px>
...@@ -11,8 +11,9 @@ ...@@ -11,8 +11,9 @@
# Example (1) # Example (1)
* Create and commit two files in the `develop `branch * Create and commit two files in the `develop ` branch
```bash ```bash
$ git checkout develop
$ echo "# Venue details" > location.md $ echo "# Venue details" > location.md
$ # add and commit the file location.md $ # add and commit the file location.md
$ echo "# Speakers" > speakers.md $ echo "# Speakers" > speakers.md
...@@ -46,26 +47,37 @@ $ git push origin myBranch ...@@ -46,26 +47,37 @@ $ git push origin myBranch
# Partial chery-picking # Partial chery-picking
* Partial cherry-picking allows you to unpack the changes from a commit. * Partial cherry-picking allows you to unpack the changes from a commit.
* Imagine you committed many files, and you want to remove certain files. * Imagine you committed many files, and you want to remove certain files.
* In practice: * In practice:
- You commited all files, and you realize that there is your data inside! - You commited all files, and you realize that there is your data inside!
- You have committed accidentally sensitive data, such as your password - You have committed accidentally sensitive data, such as your password
- You committed hidden files, for instance `.DS_Store` files - You committed hidden files, for instance `.DS_Store` files
- ...
# Example (1) # Example (1)
* Hard reset the `myBranch` branch:
```bash
$ git checkout myBranch
$ git reset --hard HEAD~2 # do not preserve files
```
* Reset the `develop` branch: * Reset the `develop` branch:
```bash ```bash
$ git checkout develop $ 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: * Add the `location.md` and the `speakers.md` files as 1 commit:
```bash ```bash
$ echo "# Venue details" > location.md
$ echo "# Speakers" > speakers.md
$ git add location.md speakers.md $ git add location.md speakers.md
$ git commit -m "add location and speakers files" $ git commit -m "add location and speakers files"
``` ```
...@@ -83,7 +95,7 @@ $ git status ...@@ -83,7 +95,7 @@ $ git status
``` ```
Now, remove the file `location.md`: Now, remove the file `location.md`:
```bash ```bash
$ git restore --staged location.md $ git restore --staged location.md # old version of git: $ git reset HEAD location.md
$ rm location.md $ rm location.md
``` ```
Commit the changes: Commit the changes:
......
# Getting Started # Getting Started (1)
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>
Make sure that your git is configured properly:
```bash ```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 Test whether your username and email have been registered
```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
```bash ```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 ```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`: * Setting up `kdiff3`:
...@@ -42,6 +59,13 @@ $ git config --global --add mergetool.kdiff3.path "<kdiff3 path>" ...@@ -42,6 +59,13 @@ $ git config --global --add mergetool.kdiff3.path "<kdiff3 path>"
* omit `""` when setting up on Linux or macOS * 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: # A note on common commands:
......
# Rebasing (1) # 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 * Move/combine a sequence of commits to a new base commit
* Avoid discrepancies when multiple people work on the same project * Avoid discrepancies when multiple people work on the same project
* Linear git history (no merge commits) * Linear git history (no merge commits)
...@@ -26,10 +26,11 @@ Imagine the following situation: ...@@ -26,10 +26,11 @@ Imagine the following situation:
# Example (1): # Example (1):
* A merge request against `develop` is still open. Repository maintainer: review, and merge it.
* Create a file in your branch `myBranch` * Create a file in your branch `myBranch`
```bash ```bash
$ git checkout myBranch # if necessary $ git checkout myBranch # if necessary
$ cd attendees
$ echo "# List of attendees" > list.md $ echo "# List of attendees" > list.md
$ # add and commit the file $ # add and commit the file
``` ```
...@@ -68,6 +69,8 @@ $ git log ...@@ -68,6 +69,8 @@ $ git log
# Interactive Rebasing - flag `-i` # Interactive Rebasing - flag `-i`
* An interactive rebase is performed with the `-i` flag:
```bash ```bash
git rebase -i <branch> git rebase -i <branch>
``` ```
...@@ -88,22 +91,24 @@ git rebase -i <branch> ...@@ -88,22 +91,24 @@ git rebase -i <branch>
# Example 1: Reword and fixup (1) # 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: * Add and commit two files to this branch:
```bash ```bash
$ cd attendees $ # git checkout myBranch && cd attendees
$ echo "# William Odell" > william.md $ echo "# William Odell" > william.md
$ # add and commit the file william.md with the message 'add william to attendee list' $ # add and commit the file william.md with the message 'add william to attendee list'
$ echo "# Roberta Ross" > roberta.md $ echo "# Roberta Ross" > roberta.md
$ # add and commit the file roberta.md with the message 'add roberta to attendee list' $ # 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: Now, we want to:
- Reword the first commit's message to: `Add William and Roberta to attendee list` - Reword the first commit's message to: `Add William and Roberta to attendee list`
- Combine the second and first commit into one - Combine the second and first commit into one
- Omit the commit message of the second commit. - Omit the commit message of the second commit.
...@@ -111,41 +116,32 @@ Now, we want to: ...@@ -111,41 +116,32 @@ Now, we want to:
# Example 1: Reword and fixup (2) # 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 ```bash
$ git rebase -i HEAD~2 $ git rebase -i HEAD~2
``` ```
The prompt shows up: * The dialog shows up (example):
```bash ```bash
$ pick 1234567 add william to attendee list $ pick 1234567 add william to attendee list
$ pick abcdef0 add roberta 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 ```bash
$ reword 1234567 add william to attendee list $ reword 1234567 add william to attendee list
$ fixup abcdef0 add roberta to attendee list $ fixup abcdef0 add roberta to attendee list
``` ```
* Edit by typing `i`
**Note:** The SHA1s of each commit are different. * Change the message of commit `1234567` to `Add William and Roberta to the attendee list`
* Save with `:wq`
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
```
# Example 2: Pick and squash (2) # 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 ```bash
$ pick 1234567 add william to attendee list $ pick 1234567 add william to attendee list
$ squash abcdef0 add roberta to attendee list $ squash abcdef0 add roberta to attendee list
......
# Reset a branch # 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 * Discards ALL commits made after the selected commit HEAD
* This happens often in **practice**: * This happens often in **practice**:
...@@ -12,10 +12,11 @@ ...@@ -12,10 +12,11 @@
* Start by committing two files: * Start by committing two files:
```bash ```bash
$ cd attendees # commit first file ...
$ echo "# CV of Firstname Lastname" > myCV.md $ echo "# CV of Firstname Lastname" > myCV.md
$ git add myCV.md $ git add myCV.md
$ git commit -m "add cv for Firstname Lastname" $ git commit -m "add cv for Firstname Lastname"
# commit second file ...
$ echo "# Biography of Firstname Lastname" > myBio.md $ echo "# Biography of Firstname Lastname" > myBio.md
$ git add myBio.md $ git add myBio.md
$ git commit -m "add biography for Firstname Lastname" $ git commit -m "add biography for Firstname Lastname"
...@@ -31,25 +32,28 @@ $ git log ...@@ -31,25 +32,28 @@ $ git log
# Example: Hard reset of a branch (2) # 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 ```bash
$ git reset --hard <SHA1> $ git reset --hard <SHA1>
``` ```
* Check what happened in the log * 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 # Notes
Alternatively, you can also remove the last commit: * Alternatively, you can also remove the last commit:
```bash ```bash
$ git reset --hard HEAD~1 $ 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 ```bash
$ git reset HEAD~1 $ git reset HEAD~1
``` ```
\ No newline at end of file
# Reverting a commit # Reverting a commit
* Enables the deletion of committed commits by reverting the changes. * Enables the deletion of committed commits by reverting the changes.
* A trace is kept in history of the original commit and the reverted one. * A trace is kept in history of the original commit and the reverted one.
...@@ -9,17 +10,15 @@ ...@@ -9,17 +10,15 @@
* On your branch, create and commit a file: * On your branch, create and commit a file:
```bash ```bash
$ cd attendees
$ echo "# Grades for Firstname Lastname" > grades.md $ echo "# Grades for Firstname Lastname" > grades.md
$ git add grades.md $ git add grades.md
$ git commit -m "File with grades for Firstname Lastname" $ git commit -m "File with grades for Firstname Lastname"
``` ```
* check the commit log and copy the SHA1: * Note down the `SHA1` by checking the `log`
```bash
$ git log * Use the `git revert` command to undo that commit:
```
* Use the `git revert` command to undo this selected commit:
```bash ```bash
$ git revert <sha1> $ git revert <SHA1>
``` ```
* This will open a dialog (`vim`-like editor). Exit with `:wq`
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment