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...

And branches...

And a HEAD...

(Which can become detached!)

You can also tag things...

Simple!

Time to code!

Those bugs aren't going to write themselves...

							
								$ git checkout -b pineapple
								Switched to a new branch 'pineapple'
								$ _
							
						
							
								console.log("Unexpected 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
								$ _
							
						
							
								$ git checkout master
								Switched to branch 'master'
								$ _
							
						

Time passes...

							
								$ git merge pineapple
								Merge made by the 'recursive' strategy.
								 pineapple.js | 0
								 1 file changed, 0 insertions(+), 0 deletions(-)
								 create mode 100644 pineapple.js
								$ _
							
						

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:jthub/hyperledger-composer.git (fetch)
								origin  git@github.com:jthub/hyperledger-composer.git (push)
								sstone1 git@github.com:sstone1/composer.git (fetch)
								sstone1 git@github.com:sstone1/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...

							
								$ git rebase master
								First, rewinding head to replay your work on top of it...
								Applying: Add pineapple
								$ _
							
						

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/jthub/hyperledger-composer
									* [new branch]      pineapple -> pineapple
								$ _
							
						

Remote Branches

⚠️

Don't force push to master!

Tips

Get Tower...

Always start in a new branch...
and make sure it's based on the latest upstream master!

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

								nothing to commit, working tree clean
								$ git pull upstream master
								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 master
								Your branch is ahead of 'origin/master' 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 -> master, upstream/master, add-migration) bump to version 0.20.3 (#4459)
								3b9de768d (tag: v0.20.2) Add cloud wallet implementations to docker images (#4456)
								860536f52 (origin/master, origin/HEAD) [Master] Add caveats/advisory for scope of generators (Angular/CLI) plus Multi-Org Fabric v1.2.1 update (#4451)
								5e2abe271 [Master] Upgrade Shim (#4453)
								f635247c3 [master] 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
								$ _
							
						

Useful Tools

Did I mention Tower?

Questions?

Try here...