Git Misinformation

How to lose code and infuriate people

James Taylor

This is not your first time

You’ve already set everything up, e.g.

    $ git config --global user.name "Block Norris"
    $ git config --global user.email conga@example.org
    $ _

🔖 Getting Started

Git is simple

It’s commits all the way down…

%%{ init: { 'themeVariables': { 'git0': '#ffffff', 'commitLabelColor': '#ffffff', 'commitLabelBackground': '#000000', 'commitLabelFontSize': '24px' }, 'gitGraph': { 'rotateCommitLabel': false, 'showBranches': false } } }%% gitGraph TB: commit id: "C1" commit id: "C2" commit id: "C3"

And branches…

%%{ init: { 'themeVariables': { 'git0': '#ffffff', 'gitBranchLabel0': '#000000' }, 'gitGraph': { 'showCommitLabel': false } } }%% gitGraph TB: commit id: "C1" commit id: "C2" branch much_ico checkout much_ico commit id: "C3" checkout main commit id: "C4" branch so_fixes checkout much_ico commit id: "C5" branch wow checkout main commit id: "C6" checkout so_fixes commit id: "C7" checkout wow commit id: "C8" checkout much_ico commit id: "C9" checkout wow commit id: "C10"

And a HEAD…

%%{ init: { 'themeVariables': { 'git0': '#ffffff', 'gitInv1': '#dede00' }, 'gitGraph': { 'showBranches': false, 'showCommitLabel': false } } }%% gitGraph TB: commit id: "C1" commit id: "C2" branch much_ico checkout much_ico commit id: "C3" checkout main commit id: "C4" branch so_fixes checkout much_ico commit id: "C5" branch wow checkout main commit id: "C6" checkout so_fixes commit id: "C7" checkout wow commit id: "C8" checkout much_ico commit id: "C9" type: HIGHLIGHT checkout wow commit id: "C10"

(Which can become detached!)

%%{ init: { 'themeVariables': { 'git0': '#ffffff', 'gitInv1': '#dede00' }, 'gitGraph': { 'showBranches': false, 'showCommitLabel': false } } }%% gitGraph TB: commit id: "C1" commit id: "C2" branch much_ico checkout much_ico commit id: "C3" type: HIGHLIGHT checkout main commit id: "C4" branch so_fixes checkout much_ico commit id: "C5" branch wow checkout main commit id: "C6" checkout so_fixes commit id: "C7" checkout wow commit id: "C8" checkout much_ico commit id: "C9" checkout wow commit id: "C10"

You can also tag things…

%%{ init: { 'themeVariables': { 'git0': '#ffffff', 'tagLabelColor': '#ffffff', 'tagLabelBackground': '#000000', 'tagLabelFontSize': '16px' }, 'gitGraph': { 'showBranches': false, 'showCommitLabel': false } } }%% gitGraph TB: commit id: "C1" commit id: "C2" branch much_ico checkout much_ico commit id: "C3" checkout main commit id: "C4" tag: "v1.0" branch so_fixes checkout much_ico commit id: "C5" branch wow checkout main commit id: "C6" checkout so_fixes commit id: "C7" checkout wow commit id: "C8" checkout much_ico commit id: "C9" checkout wow commit id: "C10"

Simple!

Time to code!

Those bugs aren’t going to write themselves…

%%{ init: { 'themeVariables': { 'git0': '#ffffff', 'gitBranchLabel0': '#000000', 'gitInv0': '#ffffff', 'commitLabelColor': '#ffffff', 'commitLabelBackground': '#000000', 'commitLabelFontSize': '24px' }, 'gitGraph': { 'rotateCommitLabel': false } } }%% gitGraph TB: commit id: "ac0d89c Initial commit" commit id: "0e2bac0 Add code" commit id: "03b29cf Add moar code" commit id: "fa54742 Nick made me add tests!" type: HIGHLIGHT
$ git checkout -b pineapple
Switched to a new branch 'pineapple'
$ _
%%{ init: { 'themeVariables': { 'git0': '#ffffff', 'gitBranchLabel0': '#000000', 'gitInv0': '#dede00', 'commitLabelColor': '#ffffff', 'commitLabelBackground': '#000000', 'commitLabelFontSize': '24px' }, 'gitGraph': { 'rotateCommitLabel': false } } }%% gitGraph TB: commit id: "ac0d89c Initial commit" commit id: "0e2bac0 Add code" commit id: "03b29cf Add moar code" commit id: "fa54742 Nick made me add tests!" type: HIGHLIGHT branch pineapple checkout pineapple
console.log("Unexpected pineapple");
%%{ init: { 'themeVariables': { 'git0': '#ffffff', 'gitBranchLabel0': '#000000', 'gitInv0': '#dede00', 'commitLabelColor': '#ffffff', 'commitLabelBackground': '#000000', 'commitLabelFontSize': '24px' }, 'gitGraph': { 'rotateCommitLabel': false } } }%% gitGraph TB: commit id: "ac0d89c Initial commit" commit id: "0e2bac0 Add code" commit id: "03b29cf Add moar code" commit id: "fa54742 Nick made me add tests!" type: HIGHLIGHT branch pineapple checkout pineapple
$ git add -A
$ git commit -s -m "Add pineapple"
[pineapple 231525e] Add pineapple
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 pineapple.js
$ _
%%{ init: { 'themeVariables': { 'git0': '#ffffff', 'gitBranchLabel0': '#000000', 'gitInv1': '#dede00', 'commitLabelColor': '#ffffff', 'commitLabelBackground': '#000000', 'commitLabelFontSize': '24px' }, 'gitGraph': { 'rotateCommitLabel': false } } }%% gitGraph TB: commit id: "0e2bac0 Add code" commit id: "03b29cf Add moar code" commit id: "fa54742 Nick made me add tests!" branch pineapple checkout pineapple commit id: "231525e Add pineapple" type: HIGHLIGHT
$ git checkout main
Switched to branch 'main'
$ _

Time passes…

%%{ init: { 'themeVariables': { 'git0': '#ffffff', 'gitBranchLabel0': '#000000', 'gitInv0': '#ffffff', 'commitLabelColor': '#ffffff', 'commitLabelBackground': '#000000', 'commitLabelFontSize': '24px' }, 'gitGraph': { 'rotateCommitLabel': false } } }%% gitGraph TB: commit id: "03b29cf Add moar code" commit id: "fa54742 Nick made me add tests!" branch pineapple checkout pineapple commit id: "231525e Add pineapple" checkout main commit id: "733138e Fix tests!" type: HIGHLIGHT
$ git merge pineapple
Merge made by the 'recursive' strategy.
 pineapple.js | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 pineapple.js
$ _
%%{ init: { 'themeVariables': { 'git0': '#ffffff', 'gitBranchLabel0': '#000000', 'gitInv0': '#ffffff', 'commitLabelColor': '#ffffff', 'commitLabelBackground': '#000000', 'commitLabelFontSize': '24px' }, 'gitGraph': { 'rotateCommitLabel': false } } }%% gitGraph TB: commit id: "fa54742 Nick made me add tests!" branch pineapple checkout pineapple commit id: "231525e Add pineapple" checkout main commit id: "733138e Fix tests!" merge pineapple id: "972a3fa Merge branch 'pineapple'" type: HIGHLIGHT

Simple…

🔖 Basic Branching and Merging

…except when there are other people!

🐧 🐧 🐧

Git is distributed

Flexible, but this is pretty common…

upstream repository

🚀

Which you fork…

origin repository

🛰

Which you clone…

local repository

💻

Which you swear at!

$ git remote -v
origin  git@github.com:jt-nti/composer.git (fetch)
origin  git@github.com:jt-nti/composer.git (push)
dselman git@github.com:dselman/composer.git (fetch)
dselman git@github.com:dselman/composer.git (push)
upstream git@github.com:hyperledger/composer.git (fetch)
upstream git@github.com:hyperledger/composer.git (push)
$ _

🔖 Working with Remotes

Time to collaborate!

Share all the bugs…

%%{ init: { 'themeVariables': { 'git0': '#ffffff', 'gitBranchLabel0': '#000000', 'gitInv1': '#dede00', 'commitLabelColor': '#ffffff', 'commitLabelBackground': '#000000', 'commitLabelFontSize': '24px' }, 'gitGraph': { 'rotateCommitLabel': false } } }%% gitGraph TB: commit id: "fa54742 Nick made me add tests!" branch pineapple checkout pineapple commit id: "231525e Add pineapple" type: HIGHLIGHT checkout main commit id: "733138e Fix tests!" commit id: "84a0d6a Add memes"
$ git rebase main
First, rewinding head to replay your work on top of it...
Applying: Add pineapple
$ _
%%{ init: { 'themeVariables': { 'git0': '#ffffff', 'gitBranchLabel0': '#000000', 'gitInv1': '#dede00', 'commitLabelColor': '#ffffff', 'commitLabelBackground': '#000000', 'commitLabelFontSize': '24px' }, 'gitGraph': { 'rotateCommitLabel': false } } }%% gitGraph TB: commit id: "fa54742 Nick made me add tests!" commit id: "733138e Fix tests!" commit id: "84a0d6a Add memes" branch pineapple checkout pineapple commit id: "a96c566 Add pineapple" type: HIGHLIGHT

No conflicts!

🎉

Except…

It’s not the same commit!

🔥

🔖 Rewriting History

$ git push --force-with-lease origin pineapple
Counting objects: 24, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (24/24), 1.91 KiB | 0 bytes/s, done.
Total 24 (delta 2), reused 0 (delta 0)
To https://github.com/jt-nti/composer
    * [new branch]      pineapple -> pineapple
$ _

🚨

⚠️ Don’t force push to main!

Tips

Get Tower…

Always start in a new branch…
and make sure it’s based on the latest upstream main!

$ git status
On branch main
Your branch is up-to-date with 'origin/main'.

nothing to commit, working tree clean
$ git pull upstream main
remote: Enumerating objects: 56, done.
...
32 files changed, 123 insertions(+), 123 deletions(-)
$ git checkout -b pineapple
Switched to a new branch 'pineapple'
$ _

🔖 Remote Branches

git status is your friend

$ git status
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
    (use "git push" to publish your local commits)

nothing to commit, working tree clean
$ _

git log is the enemy of your enemy

$ git log -n 5 --oneline
2f07943c0 (HEAD -> main, upstream/main, add-migration) bump to version 0.20.3 (#4459)
3b9de768d (tag: v0.20.2) Add cloud wallet implementations to docker images (#4456)
860536f52 (origin/main, origin/HEAD) [Main] Add caveats/advisory for scope of generators (Angular/CLI) plus Multi-Org Fabric v1.2.1 update (#4451)
5e2abe271 [Main] Upgrade Shim (#4453)
f635247c3 [main] remove info about community calls (#4450)
$ _

One commit is usually plenty…
and much easier to rebase!

$ git commit --amend
[pineapple 0acce17] Add pineapple
 Date: Wed Oct 17 13:52:04 2018 +0100
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 ham.js
 create mode 100644 pineapple.js
 create mode 100644 pizza.js
$ _

Oh, you already have more than one commit…

$ git log --oneline -n 4
c7005cd (HEAD -> pineapple) Code code code
475b463 Hacking is fun
f97caf5 Hack stuff
e62064c Initial commit
$ git rebase -i HEAD~3
...pick, squash, squash...
Successfully rebased and updated refs/heads/pineapple.
$ git log --oneline -n 2
64cbfb5 (HEAD -> pineapple) Add pineapple
e62064c Initial commit
$ _

That’s not my commit…
its changes do not need amending!

$ git commit --amend
Aborting commit due to empty commit message.
$ _

Prefer commits to stashing…
unless it’s something quick

$ git stash
Saved working directory and index state WIP on pineapple: f96a461 Fix stuff
$ _
...
$ git stash apply
On branch pineapple
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)

 new file:   upside-down-cake.js

$ _

🔖 Stashing

If you’re worried, create another branch!

$ git checkout -b pineapple-backup
Switched to a new branch 'pineapple-backup' 
$ _

Don’t panic…
your changes are probably somewhere!

$ git reflog show -n 2
073d926 HEAD@{0}: commit (amend): Add pineapple
733da01 HEAD@{1}: commit: Add pineapple
$ git reset --soft HEAD@{1}
$ _
  • HEAD~1
  • HEAD@{1}
  • 64cbfb5

Cherry pick for victory!

$ git cherry-pick 073d926
[pineapple f96a461] Fix stuff
 Date: Wed Oct 17 11:47:37 2018 +0100
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 fix.txt
$ _

🚨

⚠️ Don’t force push to main!

Useful Tools

Did I mention Tower?

Questions?

Try here…

PS.

🚨

⚠️ Don’t force push to main!