From 8859e24eed1dca29d669ad1682cea220bcd995ac Mon Sep 17 00:00:00 2001 From: Tobias Manske Date: Sat, 17 Apr 2021 02:35:42 +0200 Subject: [PATCH] More work on git introduction --- content/posts/2021-04-14-git-introduction.md | 57 ++++++++++++++++---- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/content/posts/2021-04-14-git-introduction.md b/content/posts/2021-04-14-git-introduction.md index 94cf426..6519539 100644 --- a/content/posts/2021-04-14-git-introduction.md +++ b/content/posts/2021-04-14-git-introduction.md @@ -1,9 +1,6 @@ --- -title: "Git Introduction" -date: 2021-04-14T18:54:16+02:00 - - - +title: A short introduction to Git +date: 2021-04-17T02:04:00+02:00 categories: - misc - git @@ -19,8 +16,8 @@ draft: true Currently, I'm working on my slides for this semester's tutorials for our Software Engineering course. I aim to give the students a good introduction to git, as it is an integral part of our toolchain. -So I decided to share my thoughts on what a beginner should know about git here. -I'm probably going to expand on this post in later posts. +So I decided to share a first *rough draft* of what one should know here. This post is by far not exhaustive. +I'm probably going to expand on this post in later posts. Also I'll revise this post a few times, in the next days. --- @@ -30,7 +27,7 @@ I'm probably going to expand on this post in later posts. ## What is it? So, what is Git? The xkcd already implies that git is a collaborative, distributed source control system. -But that is a rather abstract definition. Let's simplify a bit for now. Git tracks changes to your files. +But that is a rather abstract definition. Let's simplify a bit for now: Git tracks changes to your files. Essentially Git keeps different versions of your files around, so you can compare them and refer to old versions. This is really useful, as it makes it hard for you to lose working code, and easy to compare a broken version to a @@ -50,7 +47,7 @@ Initialized empty Git repository in $project_dir/.git Git now told you exactly what it did. It prepared a folder for the data it has to track. We now refer to the $project_dir as the working directory, and $project_dir/.git as the repository. Let's create some files, track some changes and take a -look at how we can interact with those. +look at how we can interact with them. ```bash $ echo "our first change" > file_one.txt @@ -135,7 +132,7 @@ Date: Wed Apr 14 19:37:00 2021 +0200 This is our first commit ``` -As we can see, this is our history. It can be rather cumbersome to look at it this way, let's use some options to make +As we can see, this is our history. It can be rather cumbersome to look at it this way, let's use an argument to make it easier to digest: ```bash @@ -147,7 +144,7 @@ cf04750 This is our first commit This is a lot more concise, but lacks quite a lot of information. I have a shorthand `git lg` in [my configuration](https://git.tobiasmanske.de/user/rad4day/public/dotfiles/tree/.gitconfig) -which would show (colored, not like it's shown here): +which would show the following: ```bash $ git lg @@ -177,3 +174,41 @@ Until now, we just stored data into git. That's nice to know, but how could we r For example: How do we get back our initial version of file_one.txt, how do we look at it? How can we replace our current version with it? + +There are multiple ways to achieve this. If we just wanted to restore the version of file_one.txt of the last commit, we could use + +```bash +$ git checkout HEAD~ -- file_one.txt +# or: (~ is a postfix meaning ancestor of) +$ git checkout 4db5410 -- file_one.txt +``` + +Which would restore the older version, thereby overwriting our local version. If we just needed that one line we accidentally deleted, we could also extract that from a diff. + +```bash +$ git diff HEAD~ # This will print the diff for all changed files +$ git diff HEAD~ -- file_one.txt # This will only print changes to file_one.txt +``` + +## Branches + +The last thing I want to introduce today, are branches. Branches make it possible to have multiple parallel change histories. +For example one branch could contain a feature you're working on, which is still to be completed, while another one could contain an important bugfix you had to write while developing that feature. + +One may wonder at this point, why we'd not just do all that on master, or at least on a single branch. We'll go into more detail about that when we talk about workflows. + +The basic workflow we'll think about right now is the following: + +1. Have idea for a new feature +2. Create a new feature branch +3. Develop feature on feature-branch +4. Merge feature-branch into master + +We'll leave point one and three as an exercise to the reader, and just work with hypothetical commits. +So first we create our branch: +```bash +$ git checkout -b feature-foobar +# if feature-foobar does not exist it will be created. +``` + +If you now type `git status` it will tell you that you're now working on `feature-foobar`