Git For Beginners

git

Guido Kroon

  • πŸ‘¨β€πŸ’Ό Information Security Manager

  • πŸ‘¨β€πŸ’» Using Git myself, at home and at work

  • 🌐 Open source

  • πŸ‡ΈπŸ‡ͺ Talar lite svenska

My Corporate Keys

In case you ever wish to verify my signatures, or grant me access to any systems πŸ‘¨β€πŸ’»

PGP fingerprint

26D3 D565 A520 CC89 4E45
7D0F 5922 172F 920C 075A

SSH public key

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICTtufd+r+Loakaa7Nj1bgbsthk0EPYeExak02KAFLNm

Why This Workshop?

git wtf

And: motivate you to use Git to your advantage. Such as, by using it to improve our template texts.

Addressing Common Confusions

  • Not insulting you (Brits will know what I mean)

  • Git is not your development pipeline

    • Git manages code - not deployment

  • Git vs. GitHub, GitLab, BitBucket?

    • Git is a core function the above services integrate

    • Those platforms then add their own handy-dandy features on top, such as a web interface

Git At The Core

git platforms

What Is Git?

  • A distributed version control system (DVCS)

  • Originally developed by Linus Torvalds

  • Now maintained by Junio Hamano

  • Originally for development of the Linux kernel

git

What Is A Version Control System (VCS)?

  • A repository to manage code contributions of multiple developers

  • Usually offering facilities to manage code merging conflicts (if any)

  • Mainly two flavours:

    • Centralised

    • Distributed

Why Would We Need A VCS?

  • Work simultaneously on the same code

  • Helps resolve sync conflicts

  • Previous file versions

  • Easily track and revert mistakes

devs

How Can A VCS Help Management?

It answers important questions related to development and project management:

  • Which changes were made?

  • Who made the changes?

  • When were the changes made?

  • Why were changes needed?

Why A DVCS?

  • The once popular centralised VCS (CVCS) required a constant connection to the central repository

  • A DVCS enables developers to work from anywhere, asynchronously, from any time zone

Why Learn Git?

  • Git is ubiquitous - it’s the most commonly used VCS around the globe

  • Most if not all of our clients use Git for DevOps work

  • We should at least have a basic understanding of Git if we want to help our clients in this field

  • We use it for our reporting

  • It’s easy and fun :D

How To Learn Git?

learn git

Getting Started

Installing Git (1/2)

On Gentoo-based systems:

$ sudo emerge dev-vcs/git

On Arch-based systems:

$ sudo pacman -S --needed git

On Debian-based systems:

$ sudo apt install git
unix beard

Installing Git (2/2)

On RHEL-based systems:

$ sudo dnf install git

On Windows (using Chocolatey):

> choco install git
unix beard

Setting Up Git

$ git config --global user.name "Larry the Cow"
$ git config --global user.email "larry@gentoo.org"
ltc

Larry the Cow is the unofficial mascot of Gentoo Linux - my preferred Linux distribution. :-)

Before the energy crisis…​

Cloning Your First Remote Git Repository

$ git clone https://github.com/pentoo/pentoo-overlay.git
Cloning into 'pentoo-overlay'...
remote: Enumerating objects: 168, done.
remote: Counting objects: 100% (168/168), done.
remote: Compressing objects: 100% (128/128), done.
remote: Total 72287 (delta 53), reused 128 (delta 33), pack-r
eused 72119
Receiving objects: 100% (72287/72287), 17.26 MiB | 2.44 MiB/s
, done.
Resolving deltas: 100% (42717/42717), done.

Pulling New Changes

If the remote repository has new changes, you can simply pull them in:

$ git pull

Creating Your Own First Git Repository

Initialising A New Local Repo

$ git init hello-world
Initialized empty Git repository in /home/larry/hello-world/.
git/
$ cd hello-world
serious

Creating Your First File

$ echo 'Hello, World!' > hello-world.txt
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committ
ed)

  hello-world.txt

nothing added to commit but untracked files present (use "git
 add" to track)

How To Save Your Changes In Git

$ git add
$ git commit
$ git push
in case of fire

Adding Your First File

Think of "adding" as "staging" your changes

$ git add hello-world.txt
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

  new file:   hello-world.txt

Q: Why stage before save? Why not save at once?

Committing Your First File

$ git commit -m "My first file"
[master (root-commit) 884dcc9] My first file
 1 file changed, 1 insertion(+)
 create mode 100644 hello-world.txt
$ git status
On branch master
nothing to commit, working tree clean

Q: Where are those changes saved? Local or remotely?

Suppose You Are A Developer

cat programmer

greet.sh

  • Suppose you’re developing a very basic application with a small team

  • The application asks the user for their name and then greets the user with their supplied name

$ ./greet.sh
What is your name?: Larry the Cow
Hello, Larry the Cow!

Suppose You Are Adding A New Feature

  • You’re developing a new feature so that it also prints the current date and time

  • However, you want to build this feature separately so that people can review it first before it’s rushed into production

$ ./greet.sh
What is your name?: Larry the Cow
Hello, Larry the Cow!
The current date and time is 2019-11-12 18:15:00

Introducing Branches

branches

Branching Strategy

For example (many more strategies exist):

  • Dedicated contributor branches

    • Separate branch for each contributor

  • Dedicated feature branches

    • Separate branch for each feature / hot-fix / sprint, etc.

Creating A New Branch Called "test"

$ git branch test
new branch

Switching To Your New "test" Branch

$ git checkout test
Switched to branch 'test'

Commit Your New Changes

$ nvim greet.sh
$ git add greet.sh
$ git commit -m "New feature: print date and time"
[test fbb367f] New feature: print date and time
 1 file changed, 1 insertion(+)
 create mode 100644 greet.sh

What Is Committed?

  • Not the full file

  • Only the incremental changes

changes

Committing Large Files

Git is not Dropbox

  • Don’t sync your N GiBs of videos or other large blobs across multiple systems

  • When you delete, it will remain in the Git history

    • Unless you manually rewrite Git history by squashing commits (advanced)

Merging Changes

$ git checkout master
$ git merge test
Updating ccd976d..fbb367f
Fast-forward
 greet.sh | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 greet.sh
branch merge

Checking The Log

$ git log
commit fbb367f8c3f4c40f2c5612474ff3a28b29b7e26f (HEAD -> mast
er, test)
Author: Larry the Cow <larry@gentoo>
Date:   Mon Sep 23 20:05:36 2019 +0200

    New feature: print date and time

commit ccd976d4282fd16300f7450bfc345f25b3fd70f7
Author: Larry the Cow <larry@gentoo>
Date:   Mon Sep 23 20:01:18 2019 +0200

    My first file

Interacting With Remote Repositories

Starting A New Repository And Pushing To GitHub

$ git init hello-world
$ cd hello-world
$ touch README.adoc
$ git add README.adoc
$ git commit -m "add README to initial commit"
$ git remote add origin https://github.com/larry-the-cow/hell
o-world.git
$ git push --set-upstream origin master

Choosing A License

Exercises: Basics

Before moving on, practice your new skills!

  • Create your first local Git repository

  • Choose a license (https://choosealicense.com/)

  • Add and commit your first few files

  • Push your changes to a remote Git server (GitHub and GitLab are both free to use)

Advanced Git Training

Continuing Interacting With Remote Repositories

Pull Requests (1/3)

  • Assume you’ve forked someone’s repository

  • You then developed a new feature in your fork

  • Now, it’s time to submit a pull request!

  • Can be done from the CLI, although rather inconvenient because it demands you to send a message manually to the remote origin

$ git request-pull master https://github.com/larry-the-cow/he
llo-world.git

Pull Requests (2/3)

Use the Web interface of GitLab / GitHub / BitBucket for a more convenient and automated solution

pull request 1

Pull Requests (3/3)

Write a description and open a pull request

pull request 2

Some Scenarios You Will Encounter Sooner Or Later

Amending Changes To Previous Commit

Problem:

"Woops, forgot to make a change before committing"

Solution:

$ nvim important-file.txt
$ git add important-file.txt
$ git commit --amend

Amending Changes To Previously Pushed Commit

Problem:

"Woops, forgot to make a change before committing AND pushing"

Solution:

$ nvim important-file.txt
$ git add important-file.txt
$ git commit --ammend
$ git push --force

Q: Why is this bad practice?

git push --force

push force

Cherry Picking Changes

Problem:

"Hmm, I only want to merge a SPECIFIC change from another branch"

Solution:

$ git checkout other-branch
$ git log
$ git checkout my-branch
$ git cherry-pick 5440f1d38772c5f084d580df05874ee9f8cc9104
[test 66186c5] cherry.txt: new line
 Date: Mon Oct 7 21:33:03 2019 +0200
 1 file changed, 1 insertion(+)

Squashing Commits (1/2)

Problem:

"I want to merge my feature branch as a SINGLE COMMIT in my master branch"

Squashing Commits (2/2)

Solution:

$ git checkout master
$ git merge --squash feature
Updating 76f9a3a..4576ace
Fast-forward
Squash commit -- not updating HEAD
 greet.sh | 2 ++
 1 file changed, 2 insertions(+)
$ git commit -m "new feature"
[master b4a11dc] new feature
 1 file changed, 2 insertions(+)

Stashing And Popping Changes (1/2)

Problem:

"I’ve been making UNSTAGED and UNCOMITTED changes in the wrong branch…​"

Stashing And Popping Changes (2/2)

Solution:

$ git stash
Saved working directory and index state WIP on master: b4a11dc new feature
$ git checkout feature
Switched to branch 'feature'
$ git stash pop
On branch feature
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  modified:   greet.sh

no changes added to commit (use "git add" and/or "git commit -a")

Resetting All Changes Since Last Commit (1/2)

Problem:

"I’ve made so many UNSTAGED and UNCOMMITED changes, but f!@# this, let’s start over…​"

Resetting All Changes Since Last Commit (2/2)

Solution:

$ git reset --hard
HEAD is now at 66186c5 cherry.txt: new line

Rebasing Changes (1/2)

  • Assume you made changes in the "feature" branch

  • But the master branch contains important changes since you branched

  • You wish to get those changes in the feature branch so that you can continue developing in the feature branch

Rebasing Changes (2/2)

Solution:

$ git checkout feature
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: test.txt: important edit
rebase

Resolving Merge Conflicts (1/3)

  • You have made changes in the feature branch

  • You wish to merge those changes in the master branch

  • Git complains you’ve got merge conflicts to solve before you can merge or rebase

Resolving Merge Conflicts (2/3)

Solution:

$ git checkout master
$ git merge feature
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the res
ult.
$ git mergetool

Resolving Merge Conflicts (3/3)

  • Git can assist you with resolving any merge conflicts

  • But you still have to resolve those conflicts yourself

  • Git cannot read your mind which changes to keep and which to discard!

Signing Your Commits (1/2)

  • You can sign your commits with your GPG key, adding non-repudiation to your commits

  • Some repositories require you to sign your commits

  • You can use the GPG agent to cache your private key password for any configured time

  • You can manually sign commits, or configure to sign by default

Signing Your Commits (2/2)

  • To manually sign a commit in the current branch:

$ git commit -S -m "My first signed commit"
  • To sign commits by default for the current local repository:

$ git config commit.gpgsign true
  • To sign commits by default for any local repository:

$ git config --global commit.gpgsign true

Exercises: Contribute To Your Neighbour

Practise your new skills

  • Fork your neighbour’s new remote repository

  • Create a new branch

  • Add, commit and push new changes in the new branch

  • Open a pull / merge request so that your neighbour can merge them

Bonus Points

  • Try merging changes from different neighbours

  • Try rebasing changes from different local branches

  • Try cherry picking individual commits only

  • Push new changes using your SSH key

  • Sign your commits with your GPG key

Need A Hint?

$ curl http://cht.sh/git | less -r

What Have We Learnt?

  • We should now know what Git is, how it works, and why it’s important.

  • We learnt how to use it in practise, and we should now have a new tool in our toolbox

  • What else have we learnt?

Conclusion

  • Git is ubiquitous, used not only by our clients

  • Git is a useful tool that can help you create better reports at Secura

  • This presentation was also created using Git

Thanks

Any questions?

tony stark eyeglass