Git - Get Ready To Use It

50 %
50 %
Information about Git - Get Ready To Use It
Technology

Published on February 28, 2014

Author: origamiaddict

Source: slideshare.net

Description

A workshop I'm holding to school my colleages in the usage of git

GIT GET READY TO USE IT DANIEL KUMMER SENIOR SOFTWARE ENGINEER NAMICS AG ZÜRICH WWW.NAMICS.COM

1 FORGET WHAT YOU KNOW ABOUT CVS/SVN GIT IS DIFFERENT EMPTY YOUR MIND – A FULL ONE CAN’T LEARN ANYTHING…

THE BASICS (NEARLY) EVERY OPERATION IS LOCAL YOU HAVE YOUR OWN REPO GIT STORES SNAPSHOTS, NOT DIFFERENCES GIT ONLY ADDS DATA

STANs REPO YOUR REPO ORIGIN CENTRAL REPO IRONs REPO DISTRIBUTED VCS

ORIGIN LOCAL STASH WORKSPACE INDEX / STAGE REPOSITORY REPOSITORY CLONE STATUS STASH SAVE STASH POP STASH APPLY ADD COMMIT COMMIT -A MERGE REVERT CHECKOUT PUSH PULL FETCH

ENVIRONMENT SETUP INSTALL GIT www.git-scm.com INSTALL SOURCETREE GUI - OR A GUI OF YOUR CHOICE www.sourcetreeapp.com

INITIAL CONFIG YOUR IDENITY – COMMIT AUTHOR INFORMATION $ git config --global user.name “Darth Vader” $ git config --global user.email darth@deathstar.emp ADDITIONAL USEFUL GLOBAL SETTINGS $ git config --global core.filemode false $ git config --global core.autocrlf true $ git config --global core.ignorecase false $ git config --global rerere.enabled true $ git config --global color.ui true $ git config --global format.pretty oneline

JOIN A PROJECT git.namics.com TELL YOUR MASTER TO ADD YOU TO THE PROJECT

IN 3 STEPS GENERATE SSH KEYS $ ssh-keygen –t rsa –C darth.vader@deathstar.emp UPLOAD YOUR SSH PUBLIC KEY TO YOUR ACCOUNT https://git.namics.com/keys CLONE THE REPOSITORY $ git clone git@git.namics.com:deathstar-v3.git

YOUR CLONE IS A WORKING COPY OF THE ORIGINS’ MASTER BRANCH – NONE OTHER! $ git status # On branch master nothing to commit, working directory clean CONTAINS EVERY VERSION OF EVERY FILE CONTAINS A .GIT FOLDER IN THE ROOT DIRECTORY WITH EVERYTHING GIT NEEDS

LET’S GET STARTED! 1. RECORDING CHANGES

ORIGIN LOCAL STASH WORKSPACE INDEX / STAGE REPOSITORY REPOSITORY CLONE STATUS STASH SAVE STASH POP STASH APPLY ADD COMMIT COMMIT -A MERGE REVERT CHECKOUT PUSH PULL FETCH

FILE STATUS LIFECYCLE REMOVE UNMODIFIED STAGE UNTRACKED ADD MODIFIED STAGED

CHECK THE WORKING COPY STATUS $ git status # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: README # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # exhaust_port_plans

LETS ADD A FILE TO THE STAGE – READY FOR COMMIT $ git add exhaust_port_plans $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: exhaust_port_plans # # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: README

MORE INFO THAN STATUS WITH DIFF $ git diff diff --git a/README b/README index e69de29..18ef097 100644 --- a/README +++ b/README @@ -0,0 +1 @@ +hello my empire minions - let's build a death star that works this time!

NOW, COMMIT EVERYTHING ADDED TO THE STAGE $ git commit -m 'add exhaust port plans’ [master e8c1e22] add exhaust port plans 1 file changed, 1 insertion(+) create mode 100644 exhaust_port_plans SKIP THE STAGE AREA IF YOU DON’T NEED IT $ git commit -a -m 'add hello message to readme’ [master 73ceaf6] add hello message to readme 1 file changed, 1 insertion(+) REMOVE/MOVE FILES WITH $ git rm $ git mv

IGNORE THINGS YOU CAN’T IGNORE ALREADY TRACKED FILES AN EXAMPLE OF THE .GITIGNORE TEXTFILE # a comment - this is ignored *.log .DS_store .idea/**/* **.classpath **.project **.settings (INSIDE THE PROJECTS ROOT DIRECTORY)

RECAP RECORDING CHANGES CHECK CURRENT WORKING COPY STATUS $ git status ADDING FILES TO THE INDEX $ git add <pathspec> SHOW CHANGES $ git diff COMMIT TO REPOSITORY $ git commit [-a] [-m] (WORKING COPY INDEX)

MESSED UP? 2. UNDOING THINGS

CHANGE YOUR LAST COMMIT –EX: ADD FORGOTTEN FILES $ git commit --amend NEVER AMEND AFTER YOU PUSHED YOUR CHANGES! UNSTAGE ADDED FILES $ git status # Changes to be committed: # # new file: alimony_for_luke $ git reset HEAD alimony_for_luke $ git reset HEAD alimony_for_luke # Untracked files: # # alimony_for_luke

REVERT CHANGES IN YOUR WORKING DIRECTORY $ git status # Changes not staged for commit: # # modified: README # $ git checkout –- README $ git status # On branch master # nothing to commit, working directory clean

REVERSE COMMIT $ git revert <commit> CREATES NEW COMMITS RECORDING THE REVERT CHANGES IF YOU GET MERGE CONFLICTS, RESOLVE AND CONTINUE $ git revert --continue CANCEL REVERT OPERATION $ git revert --abort

RECAP UNDOING THINGS ALTER LAST COMMIT $ git commit --amend UNSTAGE FILES $ git reset HEAD <file> REVERT CHANGES $ git checkout -- <file> REVERSE COMMIT $ git revert <commit>

JOIN THE TEAM EFFORT 3. WORK WITH REMOTES

ORIGIN LOCAL STASH WORKSPACE INDEX / STAGE REPOSITORY REPOSITORY CLONE STATUS STASH SAVE STASH POP STASH APPLY ADD COMMIT COMMIT -A MERGE REVERT CHECKOUT PUSH PULL FETCH

YOUR CLONED REPO ALREADY HAS A REMOTE - ORIGIN $ git remote -v origin git@git.namics.com:deathstar-v3.git (fetch) origin git@git.namics.com:deathstar-v3.git (push) ADDING A REMOTE TO YOUR LOCAL REPOSITORY $ git remote add origin git@git.namics.com:deathstar-v3.git YOU COULD HAVE MULTIPLE REMOTES

GETTING INFORMATION ABOUT A REMOTE $ git remote show origin * remote origin Fetch URL: git@git.namics.com:deathstar-v3.git Push URL: git@git.namics.com:deathstar-v3.git HEAD branch: master Remote branch: master tracked Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push': master pushes to master (fast-forwardable)

FETCH VS PULL GET DATA FROM THE REMOTE REPOSITORY $ git fetch <remote-name> FETCH ONLY MANUAL MERGING FETCH $ git pull FETCH AND MERGE TRACKED BRANCHES PULL

PUSH CHANGES PUSH BRANCH TO REMOTE $ git push <remote-name> <branch-name> ONLY WORKS IF NOBODY HAS PUSHED IN THE MEANTIME $ git push origin master Counting objects: 8, done. Compressing objects: 100% (5/5), done. Writing objects: 100% (6/6), 650 bytes | 0 bytes/s, done. Total 6 (delta 0), reused 0 (delta 0) To git@git.namics.com:deathstar-v3.git 5d31566..73ceaf6 master -> master

EXPLICITLY PUSH BRANCHES YOU WANT TO SHARE PUSH IS REJECTED IF CURRENT BRACH IS BEHIND $ git push origin master To git@git.namics.com:deathstar-v3.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'git@git.namics.com:deathstar-v3.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again.

RECAP REMOTES LIST REMOTES $ git remove -v ADDING REMOTES $ git remote add <shortname> <url> INFORMATION ABOUT REMOTE $ git remote show <shortname>

RECAP REMOTES FETCH FROM REMOTES $ git fetch <remote-name> PULL FROM REMOTES $ git pull PUSH CHANGES TO REMOTES $ git push <remote-name> <branch-name>

LEAVE YOUR MARK 4. TAGGING

LIGHTWEIGHT VS ANNOTATED $ git tag v0.1 $ git tag –a v1.0 –m ‘first rel’ CHECKSUM IN A FILE FULL OBJECT INTERNAL PROJECTS CHECKSUM AUTHOR INFO GPG SIGNED (-s) OPEN SOURCE REPOSITORIES

TAGS MUST BE PUSHED SEPARATELY $ git push origin --tags Total 0 (delta 0), reused 0 (delta 0) To git@git.namics.com:deathstar-v3.git * [new tag] 1.0 -> 1.0 LIST TAGS WITH $ git tag 1.0 1.1

BREAK TIME BACK IN 15

THE KILLER FEATURE 5.BRANCHING

COMMITS AND BRANCHES COMMIT - OBJECT THAT CONTAINS A POINTER TO THE SNAPSHOT OF THE CONTENT THE GIT HISTORY IS A LINKED LIST OF POINTERS 98ca9 34ac2 f30ab Snapshot A Snapshot B Snapshot C

A BRANCH IS A LIGHTWEIGHT POINTER TO A COMMIT LOCAL POINTER TO YOUR CURRENT BRANCH MASTER BRANCH POINTER 98ca9 34ac2 TESTING BRANCH POINTER HEAD master f30ab testing

CREATING A BRANCH IS WRITING 41 BYTES TO A FILE (SHA-1 HASH PLUS NEWLINE) THAT’S WHY BRANCHING IS SUPER FAST

CREATE A NEW BRANCH $ git branch testing CHECKOUT EXISTING BRANCH $ git checkout testing SWITCHING TAKES A BLINK OF THE EYE EVERYTHING HAPPENS IN YOUR WORKING COPY 98ca9 34ac2 NOW YOU’RE ON THE TESTING BRANCH SHORT FORM FOR BRANCH AND CHECKOUT $ git checkout –b testing master f30ab testing HEAD

BASIC MERGING C0 I WANT TO MERGE ISS53 INTO MASTER Snapshot to Merge Into Common Ancestor C1 CHECKOUT THE TARGET BRANCH C2 master C4 C3 C5 $ git checkout master MERGE THE ISSUE BRANCH $ git merge iss53 iss53 Snapshot to Merge In

master MERGE MAGIC HAPPENED: C0 C1 C2 THE RESULT $ git checkout master $ git merge iss53 C4 C3 C6 C5 iss53 Auto-merging README Merge made by the 'recursive' strategy. README | 1 + 1 file changed, 1 insertion(+) FEATURE BRANCH NO LONGER NEEDED, DELETE IT $ git branch –d iss53

RESOLVING CONFLICTS WHAT IF MAGIC DOESN’T HAPPEN? CONFLICT (content): Merge conflict in README Automatic merge failed; fix conflicts and then commit the result. USE A MERGETOOL TO RESOLVE THE CONFLICTS $ git mergetool (DIFFMERGE FOR EXAMPLE)

COMMIT CHANGES AFTER RESOLVING THE CONFLICTS $ git commit Merge branch 'testing' Conflicts: README # # It looks like you may be committing a merge. # If this is not correct, please remove the file # .git/MERGE_HEAD # and try again. THE MESSAGE IS AUTOGENERATED YOU CAN STILL EDIT IT OF COURSE

RECAP MERGING CREATE BRANCH $ git branch <branchname> CHECKOUT BRANCH $ git checkout <branchname> MERGE BRANCHES $ git merge <commit> RESOLVE CONFLICTS $ git mergetool

BRANCH MANAGEMENT LIST BRANCHES -a: remote branches $ git branch [--merged] [-a] DELETE BRANCHES $ git branch [-d] [-D] -D: force delete unmerged

REMOTE BRANCH PUSHING TO REMOTE BRANCHES $ git push [remote] [branch] TRACKING REMOTE BRANCHES $ git checkout –b <branch> <remotename>/<branch> ALLOWS GIT PULL INSTEAD OF GIT FETCH DELETING REMOTE BRANCHES (NOTE THE : ) $ git push <remotename> :<branch>

RECAP BRANCH MANAGEMENT LIST ALL BRANCHES $ git branch -a TRACKING REMOTE BRANCHES $ git checkout –b <branch> <remotename>/<branch> PUSHING TO REMOTE BRANCHES $ git push [remote] [branch] DELETING REMOTE BRANCHES (NOTE THE : ) $ git push <remotename> :<branch>

BRANCHING WORKFLOWS GIT FLOW STRUCTURE FOR LARGE PROJECTS FEATURE BRANCH NEW BRANCH FOR EVERY FEATURE CENTRALIZED SVN LIKE FORKING USED BY GITLAB

GIT FLOW MASTER OFFICIAL RELEASE HISTORY HOTFIX PRODUCTION RELASE PATCHING RELEASE POLISHING – NO NEW FEATURES DEVELOP FEATURE INTEGRATION FEATURE FEATURE DEVELOPMENT, CHILD OF DEVELOP

GIT FLOW

REBASE A MERGING ALTERNATIVE TAKE ALL COMMITED CHANGES ON ONE BRANCH AND REPLAY THEM ON ANOTHER ONE

SAME RESULT AS MERGE BUT CLEANER HISTORY IT APPEARS THAT ALL WORK HAPPENED IN SERIES B E F master C BEFORE A D experiment master AFTER A B E F C' D' experiment

DO NOT REBASE PUSHED COMMITS REBASING ABANDONS EXISTING COMMITS AND CREATES SIMILAR NEW ONES USE REBASE IF YOU KNOW WHAT YOU’RE DOING NEWBIES SHOULD USE MERGE – IT’S SIMPLER REBASE A BRANCH ONTOP OF THE CURRENT ONE $ git rebase <branch> REBASE ONTO TARGET BRANCHES $ git rebase --onto <branches>

MASHUP COMBINATIONS 6. REPOSITORY WITHIN REPO

SUBMODULES TRACKED BY THE EXACT COMMIT SPECIFIED IN THE PARENT REPOSITORY – LOCKED TO COMMIT GOOD FOR NOT TOO FREQUENT UPDATES THE WHOLE REPOSITORY IS INCLUDED AS SUBMODULE PARTIAL INCLUDE IS NOT POSSIBLE

MANY BUTS’ LOTS OF DISADVANTGES AND PITFALLS YOU CAN’T JUST CLONE THE PARENT REPOSITORY SUBMODULE NEEDS TO BE INITIALIZED AND UPDATED NEVER AUTOMATICALLY UPDATED WHEN THE SUBMODULE REPOSITORY IS UPDATES YOU CAN’T BRANCH EASILY ACCIDENTIAL SUBMODULE REVERT HAPPENS EASILY LOTS OF THINKING INVOLVED WHEN USING SUBMODULES

SUBTREE MERGE IT’S A MERGE STRATEGY (LIKE RECURSIVE FOR BRANCHES) NO NEW METADATA SUB-PROJECT CONTRIBUTION IS SLIGHTLY MORE COMPLICATED YOU ARE RESPONSIBLE FOR NOT MIXING SUPER AND SUB-PROJECT CODE

SOME CAKE 7. USEFUL TOOLS

INTERACTIVE MODE ENHANCED COMMAND LINE ADD –i TO COMMAND LINE; FOR EXAMPLE $ git add –i staged unstaged path 1: unchanged +0/-1 TODO *** Commands *** 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now>

THE STASH A “DIRTY LITTLE HELPER” STORE YOUR LOCAL MODIFICATIONS AWAY AND REVERT WORKING COPY TO HEAD COMMIT SAVE CHANGES TO STASH $ git stash APPLY STASH $ git stash pop

ORIGIN LOCAL STASH WORKSPACE INDEX / STAGE REPOSITORY REPOSITORY CLONE STATUS STASH SAVE STASH POP STASH APPLY ADD COMMIT COMMIT -A MERGE REVERT CHECKOUT PUSH PULL FETCH

CHERRY PICK APPLY CHANGES FROM ANOTHER COMMIT $ git cherry-pick <commit> BEFORE C0 C1 AFTER HEAD C2 C4 C0 C1 HEAD C2 C4 C6 git cherry pick C3 C3 C5 APPLY CHANGES FROM C3 AS NEW COMMIT ON TOP OF C4 C3 C5

BISECT YOUR CODE FIND THE CHANGE WHICH INTRODUCED THE BUG $ git bisect start CURRENT VESION (HEAD) IS BAD $ git bisect bad LAST TESTED VERSION WHICH WAS GOOD $ git bisect good v0.4

GIT SVN BIDIRECTIONAL OPERATIONS BETWEEN GIT AND SVN TRACK STANDARD SVN REPOSITORY “TRUNK/BRANCHES/TAGS” UPDATE GIT WITH FETCH $ git fetch UPDATE SVN WITH DCOMMIT $ git dcommit

MOST COMMON PROBLEMS 8. AND SOLUTIONS

I CAN’T CLONE CHECK YOUR SSH CONNECTION $ ssh -T git@git.namics.com Welcome to GitLab, Daniel Kummer! VERIFY YOUR PUBLIC-KEY IN YOUR GITLAB PROFILE VERIFY YOUR PROJECT ACCESS IN GITLAB TRY RE-ADDING YOUR KEY TO SSH-AGENT $ ssh-add –D $ ssh-add ~/.ssh/id_rsa # deletes all identities in agent # add key to agent

I CAN’T PULL VERIFY THAT THERE ARE NO UNCOMMITED CHANGES VERIFY THAT YOUR LOCAL BRANCH TRACKS A REMOTE ONE (TRACKED ONES ARE BLUE) $ git branch -vv * master 4658814 [origin/master] SUBSEQUENT TRACKING OF REMOTE BRANCHES $ git branch --set-upstream-to <local-branch> origin/ <remote-branch>

I CAN’T PUSH CHECK IF YOUR LOCAL BRANCH IS BEHIND $ git push # Your branch is behind 'origin/master' by 5 commits CHECK IF YOUR LOCAL BRANCH IS TRACKING A REMOTE ONE

I CAN’T DELETE A FILE FROM REPO TRY DELETING THE CACHED FILE ONLY (LEAVES WORKING COPY INTACT) $ git rm --cached eclipse.properties IF THE FILE NEEDS TO BE REMOVED FROM THE HISTORY $ git filter-branch … https://help.github.com/articles/remove-sensitive-data

MORE MATERIAL GIT-PRO BOOK / REFERENCE http://git-scm.com/ TUTORIALS https://www.atlassian.com/git GIT FLOW CHEATSHEET http://danielkummer.github.io/git-flow-cheatsheet 15 MINUTES INTERACTIVE TUTORIAL http://try.github.io

GETTING HELP $ git help <command> ASK YOUR LOCAL GURU GOOGLE IT CONT@CT ME

CONGRATULATION YOU MADE IT !

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

How to Use Git: a Tutorial - Opentaps Wiki

Note that all of this do not mean that the branch has be local until ready, ... To get historical changes from git, you can use ... php/How_to_Use_Git:_a ...
Read more

Using Git for collaboration - Git - gittutorial Documentation

... A tutorial introduction to Git. ... bob$ git config --get remote.origin.url /home ... don’t use git reset on a publicly-visible branch that other ...
Read more

gitready/gitready · GitHub - GitHub · Where software is ...

Git Ready. There is only one way to get ready for immortality, ... and posts for gitready.com. Git Ready ... (remember to use the right branch name!): git ...
Read more

Get Started with Git · An A List Apart Article

... run apt-get install git ... you can use git-checkout to pluck a file out of another ... Pro Git—a free book on Git; Git Ready—a blog with tips and ...
Read more

gitready/es · GitHub - GitHub · Where software is built

gitready / es. Code; Pull requests; Pulse; ... There is only one way to get ready for immortality, ... Git Ready publishes ...
Read more

Git - Rebasing

In Git, this is called rebasing. ... onto origin/master when you were ready to submit your ... work and things will get messy when you try to ...
Read more

Git Reference

We can use git status to see ... some git tools can get confused if ... while we had both changes ready to commit and ready to stage, a git reset ...
Read more

git ready » learn git one commit at a time

git ready. learn git one commit ... find ancestor commits restoring a directory from history rolling back changes with revert pick out individual commits ...
Read more

How the Heck Do I Use GitHub? - Lifehacker - Tips and ...

When you're ready, tell git your ... Click on "Set Up Git" to get started. Step Two: Install Git. GitHub exists because of a ... you can actually use GitHub!
Read more