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
π¨βπΌ Information Security Manager
π¨βπ» Using Git myself, at home and at work
π Open source
πΈπͺ Talar lite svenska
In case you ever wish to verify my signatures, or grant me access to any systems π¨βπ»
PGP fingerprint |
|
SSH public key |
|
And: motivate you to use Git to your advantage. Such as, by using it to improve our template texts.
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
A distributed version control system (DVCS)
Originally developed by Linus Torvalds
Now maintained by Junio Hamano
Originally for development of the Linux kernel
A repository to manage code contributions of multiple developers
Usually offering facilities to manage code merging conflicts (if any)
Mainly two flavours:
Centralised
Distributed
Work simultaneously on the same code
Helps resolve sync conflicts
Previous file versions
Easily track and revert mistakes
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?
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
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
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
On RHEL-based systems:
$ sudo dnf install git
On Windows (using Chocolatey):
> choco install git
$ git config --global user.name "Larry the Cow"
$ git config --global user.email "larry@gentoo.org"
Larry the Cow is the unofficial mascot of Gentoo Linux - my preferred Linux distribution. :-)
Before the energy crisis…
$ 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.
If the remote repository has new changes, you can simply pull them in:
$ git pull
$ git init hello-world
Initialized empty Git repository in /home/larry/hello-world/.
git/
$ cd hello-world
$ 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)
$ git add
$ git commit
$ git push
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?
$ 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’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!
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
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.
$ git branch test
$ git checkout test
Switched to branch 'test'
$ 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
Not the full file
Only the incremental changes
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)
$ git checkout master
$ git merge test
Updating ccd976d..fbb367f
Fast-forward
greet.sh | 1 +
1 file changed, 1 insertion(+)
create mode 100644 greet.sh
$ 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
$ 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
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)
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
Use the Web interface of GitLab / GitHub / BitBucket for a more convenient and automated solution
Write a description and open a pull request
Problem:
"Woops, forgot to make a change before committing"
Solution:
$ nvim important-file.txt
$ git add important-file.txt
$ git commit --amend
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
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(+)
Problem:
"I want to merge my feature branch as a SINGLE COMMIT in my master branch"
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(+)
Problem:
"I’ve been making UNSTAGED and UNCOMITTED changes in the wrong branch…"
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")
Problem:
"I’ve made so many UNSTAGED and UNCOMMITED changes, but f!@# this, let’s start over…"
Solution:
$ git reset --hard
HEAD is now at 66186c5 cherry.txt: new line
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
Solution:
$ git checkout feature
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: test.txt: important edit
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
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
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!
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
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
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
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
$ curl http://cht.sh/git | less -r
Or check https://git-scm.com/book/
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?
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
Any questions?