Ninja Git: Save Your Master

53
Ninja master Nicola Paolucci Developer Advocate / Evangelist with Save your

description

Dear Ninja Git Apprentice, your training will be short but it will be dense and fierce. I will teach you how to lock down your master's fortress from tampering and infiltration, how to become invisible and hide data in a repository, how to resist any attacks and recover your committed and uncommitted files, how to be fast as a fox to cover your tracks and resolve conflicts. Save the master!

Transcript of Ninja Git: Save Your Master

Page 1: Ninja Git: Save Your Master

Ninja

masterNicola Paolucci!Developer Advocate / Evangelist

with!

Save your

Page 2: Ninja Git: Save Your Master

pirateninjazombie

Page 3: Ninja Git: Save Your Master

Nicola Paolucci@durdn

Bio pictures: the subtle pleasure of embarrassing yourself in front of hundreds of people

Page 4: Ninja Git: Save Your Master

Lock your master’s fortress

1

2

Dear Ninja apprentice, here you’ll learn:

Powers of invisibility

Solve conflicts with power blows3

4 Cover your tracks

Page 5: Ninja Git: Save Your Master

1.Powers of invisibility

© http://www.amigosdosbichos.org/

Page 6: Ninja Git: Save Your Master

1.Powers of invisibility

© http://www.amigosdosbichos.org/

Page 7: Ninja Git: Save Your Master

Hide files from

git update-index \ --assume-unchanged

very useful with git-svn

Different than .gitignore, it hides commited files

Page 8: Ninja Git: Save Your Master

Hide files from

git update-index \ --no-assume-unchanged

Revert it with:

remember to add --no

Page 9: Ninja Git: Save Your Master

List assumed unchanged files

git ls-files -v | grep ^hUseful as alias (see alias list later)

Page 10: Ninja Git: Save Your Master

Hide files in raw objects

actually writes into the object db

git hash-object -w <file>

Page 11: Ninja Git: Save Your Master

CUSTOMARY WARNING!

if you get in trouble, don’t come to me :D

Treat this power with great care. !And if you’re unsure, please refrain from experiments!

Page 12: Ninja Git: Save Your Master

Delete branch or commits and retrieve it later

git branch -D <branch>

git reset --hard HEAD~20

Page 13: Ninja Git: Save Your Master

What is the reflog?It’s a log of all the places where your

HEAD has been

garbage collected every

90 days

Page 14: Ninja Git: Save Your Master

Recollect your goods

$ git reflog !

00af1b3 HEAD@{2}: reset: moving to refexp da5b154 HEAD@{3}: rebase finished: returning … da5b154 HEAD@{4}: pull: checkout da5b154dfa71… e10671f HEAD@{8}: pull origin master: checkout

Just list the HEAD moves using the reflog and pick the one to restore

Page 15: Ninja Git: Save Your Master

Don’t expire the reflog

[gc "refs/remotes/*"] reflogExpire = never reflogExpireUnreachable = never

If you hide stuff in objects not referenced, be sure they won’t be garbage collected!

Page 16: Ninja Git: Save Your Master

If some traitor deleted filesgit log -1 -- [path] lists where a file was deleted

git log --diff-filter=D --summary lists all deleted files

Page 17: Ninja Git: Save Your Master

2. Lock your master’s fortress

Page 18: Ninja Git: Save Your Master

Always a balancing act

Security DevSpeed

Page 19: Ninja Git: Save Your Master

Lock down your repo

# no rewriting history denyNonFastForwards = true !

# no deleting history denyDeletes = true !

# check object consistency fsckObjects = true

Edit .git/config in the [receive] section:

Page 20: Ninja Git: Save Your Master

Reject force push, Luke

atlss.in/update-paranoid

Git project has already an update hook ‘update-paranoid’ that is designed to reject history rewriting updates

Page 21: Ninja Git: Save Your Master

Reject force push, Luke

Page 22: Ninja Git: Save Your Master

Impersonating Authors is easy with .

$ git commit -m "I'm Luke" $ git commit --author "Elvis <[email protected]>" \ -m "I'm elvis" commit a9f0967cba236465d6cb68247.. Author: Elvis <[email protected]> Date: Mon Apr 22 18:06:35 2013 -0500 ! I'm Elvis !commit d6eb7572cbb4bdd8e2aaa5c90.. Author: Luke <[email protected]> Date: Mon Apr 22 18:04:54 2013 -0500 ! I'm Luke

Page 23: Ninja Git: Save Your Master

Harden up by signing thingsSample gpg commands to get you started:

gpg --gen-keyGenerate your GPG keys

gpg -kList your keys

gpg -a --export <keyid>Export your key

Page 24: Ninja Git: Save Your Master

Store your signature in Simple! Add a tag referencing your public key

gpg -a --export <keyid> | \ git hash-object -w --stdin !! Store your public key in a raw object

git tag nicks-key 187ysgTag the raw object with a label

Page 25: Ninja Git: Save Your Master

git tag -s <tag_name> -m “message”

Sign a tag with your GPG key

Finally you can sign/verify tags

git tag -v <tag_name>Verifies that the signature is valid

Page 26: Ninja Git: Save Your Master

git cat-file -p tims-key | gpg --import

Import a GPG key from a tag

Import other public keys

Page 27: Ninja Git: Save Your Master

Always a balancing act

Security DevSpeed

Page 28: Ninja Git: Save Your Master

3. Solve conflicts with power blows

Page 29: Ninja Git: Save Your Master

A word on terminology

Current checked out branch

!!!--ours

What do ours and theirs mean when solving conflicts?

Any merge/rebase coming in

!!!--theirs

Page 30: Ninja Git: Save Your Master

Basics for easy conflict resolution

The common commands are:

$ git checkout --ours/--theirs <file> Check back out our own/their own version of the file

$ git add <file> Add the change to the index will resolve the conflict

Page 31: Ninja Git: Save Your Master

Aliases for easy conflict resolution

[alias] ours = "!f() { \ git checkout --ours $@ && git add $@;\ }; f” !

theirs = ...

Add these to .gitconfig

Page 32: Ninja Git: Save Your Master

Where do I get that awesome alias?atlss.in/git-aliases

Page 33: Ninja Git: Save Your Master

rerere resolve!Reuse Recorded Resolution will help you when dealing with repetitive and similar merge conflicts.

$ git config --global rerere.enabled true Turns it on and forget about it

Page 34: Ninja Git: Save Your Master

Sample output rerere$ git add hello.rb $ git commit Recorded resolution for 'hello.rb'. [master 68e16e5] Merge branch 'i18n'

Auto-merging hello.rb CONFLICT (content): Merge conflict in hello.rb Resolved 'hello.rb' using previous resolution.

Page 35: Ninja Git: Save Your Master

4. Cover your trackshttps://www.youtube.com/watch?v=D22gbXIx-CE

Page 36: Ninja Git: Save Your Master

MASTER

FEATURE

What is a rebase?

It’s a way to replay commits, one by one, on top of a branch

Page 37: Ninja Git: Save Your Master

MASTER

FEATURE

What is a rebase?

It’s a way to replay commits, one by one, on top of a branch

Page 38: Ninja Git: Save Your Master

MASTER

FEATURE

What is a rebase?

It’s a way to replay commits, one by one, on top of a branch

Page 39: Ninja Git: Save Your Master

MASTER

FEATURE

What is a rebase?

It’s a way to replay commits, one by one, on top of a branch

Don’t use!

Page 40: Ninja Git: Save Your Master

Correct way to use rebase to update a feature branch

What is a rebase?

MASTER

FEATURE

Page 41: Ninja Git: Save Your Master

Correct way to use rebase to update a feature branch

What is a rebase?

MASTER

FEATURE

Page 42: Ninja Git: Save Your Master

What is a --interactive rebase?

PICK!

SQUASH

REWORD!

FIXUP

EDIT !

EXEC

It’s a way to replay commits, one by one, deciding interactively what to do with each

Page 43: Ninja Git: Save Your Master

--autosquash

$ git config --global rebase.autosquash true Turns on the feature

Automatically modify the todo list of !rebase --interactive by annotating commits

Page 44: Ninja Git: Save Your Master

git commit -m “squash! …"

You can prepend commit messages with:

git commit -m “fixup! …"

git commit -m “reword! …"

etc…

Rebase task list will be then prepopulated

--autosquash

Page 45: Ninja Git: Save Your Master

CUSTOMARY WARNING!

rebase rewrites history!

Treat this power with great care. !Only rewrite history of local branches or…

Page 46: Ninja Git: Save Your Master

http://travisjeffery.com/b/2012/02/search-a-git-repo-like-a-ninja/!

Bonus: greppling like a Ninja

Page 47: Ninja Git: Save Your Master

git grep is amazingIt searches your whole project at blazing speed. Let’s make it more awesomer!

git config --global grep.extendRegexp true Turn on extended regular expressions

git config --global grep.lineNumber true Include line numbers

Page 48: Ninja Git: Save Your Master

git grep is amazing

And the final touch, pack it in an alias

git config --global alias.g \ ”grep --break --heading —line-number"

!

Make it group output like Ack

Page 49: Ninja Git: Save Your Master

Much more on gitatlassian.com/git

Page 50: Ninja Git: Save Your Master

Nicola Paolucci@durdn

Thank you!

Page 51: Ninja Git: Save Your Master

Q&A

Page 52: Ninja Git: Save Your Master

Git Repository Management for Enterprise Teams

Free Git Code Hosting for Small Teams

Free Git Desktop client for Mac or Windows

Page 53: Ninja Git: Save Your Master