How to Really Get Git
-
Upload
susan-tan -
Category
Technology
-
view
574 -
download
3
Transcript of How to Really Get Git
How to Really GetHow to Really GetGitGit
Sunday, February 8, 2015Susan Tan
@ArcTanSusan
Who am I?
Software Engineer @Piston who uses Python and gitA New-Yorker-who-moved-to-San-FranciscoAlso Piston is .hiring
I. Git SetupI. Git Setup
Git Configs: AliasesGit Configs: Aliases
[alias] st = status ci = commit br = branch co = checkout df = diff lg = log -p
$ git st# On branch master# Untracked files:# (use "git add <file>..." to include in what will be committed)## _posts/2009-02-06-helpful-command-aliases.textilenothing added to commit but untracked files present (use "git add" to track)
In ~/.gitconfig, put this here:
Git Configs: ColorsGit Configs: Colors
[color "status"] added = magenta changed = yellow untracked = cyan
In ~/.gitconfig, put this:
Git Configs:Git Configs:
Tab AutocompletionTab Autocompletion1. Follow instructions for your OS.2. Then you can use auto tab completion on long commands
here
$ git chec<tab>
Git Configs:Git Configs:
A Useful Bash PromptA Useful Bash Prompt
https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion
A useful prompt shows
current branchcurrent directorycurrent virtualenvstatus of working directory
Who is this talk for?You already know the basics of git, but you still makemistakes (well, so does everyone)
Common git commands you already know:git addgit status git push origingit fetch origin
You want to know how to deal with mistakesCommon git commands you'll learn about:
git rebasegit resetgit refloggit revert
This Should Look FamiliarThis Should Look Familiar
Source: http://git-scm.com/book/ch1-3.html
This should be familiar, tooThis should be familiar, too
This may be familiar, tooThis may be familiar, too
This should be familiar, too -- Collaboration with git.This should be familiar, too -- Collaboration with git.
Source: http://nvie.com/posts/a-successful-git-branching-model/
II. Mistakes and howII. Mistakes and howto fix themto fix them
"People think computers will keep them from making mistakes.They're wrong. With computers you make mistakes faster."--Adam Osborn
Source: http://justinhileman.info/article/git-pretty/git-pretty.pdf
git rebasegit rebaseWhy use rebase?
1. git rebase to keep a branch updated
2. git rebase --interactive flag to change history
git rebase: The Simplest Usagegit rebase: The Simplest Usage
Note: "master" here means remote upstream's master branch.
git rebase: The Simplest Usagegit rebase: The Simplest Usage
What if your personal branch "develop" needs to rebaseWhat if your personal branch "develop" needs to rebase
against a feature branch shared by your team members?against a feature branch shared by your team members?
Note: "master" here means remote upstream repo's master branch.
What if your personal branch "develop" needs to rebaseWhat if your personal branch "develop" needs to rebase
against a feature branch shared by your team members?against a feature branch shared by your team members?
Note: "master" here means remote upstream repo's master branch.
git rebase master/feature develop
git rebase --onto master feature develop
Note: "master" here means remote upstream repo's master branch.
OR
git rebasegit rebase
git rebase <remote_repo>/<remote_branch> <your_branch_name>
git rebase --onto <first_this> <then_this> <last>
Syntax for git rebase
What if the feature branch needs to rebase against aWhat if the feature branch needs to rebase against a
upstream repo's master branch shared by everyone?upstream repo's master branch shared by everyone?
Note: "master" here means remote upstream repo's master branch.
What if the feature branch needs to rebase against aWhat if the feature branch needs to rebase against a
upstream repo's master branch shared by everyone?upstream repo's master branch shared by everyone?
Note: "master" here means remote upstream repo's master branch.
Rebase ConflictsRebase Conflicts
Rebase Conflicts require manual fixesRebase Conflicts require manual fixes(because git isn't clever enough to fix conflicts)
1. Locate conflict markers (<<<<<<) andmake edits to resolve
2. Resolve a conflict in each file with gitadd <filename>
3. git rebase --continue4. Alternatively, you can undo the git
rebase with git rebase --abort
Fixing git rebase conflicts is notFixing git rebase conflicts is notalways the right solutionalways the right solutiongit checkout mastergit branch -D featuregit fetch mastergit checkout -b feature master/featuregit checkout -b develop_v2git merge develop_v1git branch -D develop_v1
git fetch mastergit checkout [YOUR BRANCH]git rebase --onto master/feature [commit]
OR
Different computers, sameDifferent computers, samebranchbranch
git fetch origin && git rebase origin/<MY_BRANCH>
Note: "origin" is your personal cloned copy of an upstream masterrepo
git pull --rebase
Or, equivalently
git rebase --interactive <HASH> orgit rebase --interactive HEAD^
Defn: To re-order, edit, squash, or remove manycommits at once
git rebase -i HEAD~5
Squashing commitsSquashing commits
git resetgit reset
Defn: Reset current HEAD to the specified state
git reset --soft HEAD^
Defn: Set files from previous commit onto staging area. HEADpoints to the previous commit. This leaves all your changed filesas "Changes to be committed".
git reset --soft HEAD^
git reset --hard HEAD^
Defn: Abandon everything since your last commit. HEAD pointsto the previous commit. Nothing is in staging. Use with greatcaution
git refloggit reflog
Defn: shows all the commits and actions on your machine that hasever happened such as branch switches, rebases, resets, branchdeletions, cherry-picks, reverts.
git reflog git reflog --grep="Revert"
How to make git reflog more usefulHow to make git reflog more useful
git reflog --all --date=iso
You can undo a hard reset with the help of git reflogYou can undo a hard reset with the help of git reflog
git reset --hard HEAD^^ to remove the last 3 commits
You can undo a hard reset with git reflog + git reset --hardYou can undo a hard reset with git reflog + git reset --hard
git reset --hard a64b0f2 to restore branch to previous stategit reset --hard a64b0f2 to restore branch to previous state
Why use git reflog?Why use git reflog?
You lost commits when you did a git reset --hardYou squashed too many commits into 1 commit, so youwant to undo git rebase -iYou accidentally deleted branches and want to restorethem
Summary: Use reflog to look up old lost commits
Use cases for git reflogUse cases for git reflogLook up a commit hash. Then either
create a new (un-named) branch out of this commitgit checkout -b <COMMIT_HASH>
reset your branch to a previous stategit reset --hard <COMMIT_HASH>git reset --soft <COMMIT_HASH>
cherrypick that lost commit on top of your branchgit cherry-pick <COMMIT_HASH>
How long does theHow long does theoutput of reflog last?output of reflog last?
Hanging commits are removed from the local repository bygarbage collection (gc) or by manual removal. Default is 30days.
[gc] reflogExpire = never reflogExpireUnreachable = never
git revert 638351801cd802d6e0c4e601db1eca801dd58936
Defn: Makes a revert commit with a message that states thespecified commit is reverted
git revert <HASH>
Defn: Revert changes specified by 4th last commit awayfrom HEAD and create a new commit with reverted changes.
git revert HEAD~3
git revert --continueContinue after resolving conflicts git revert --abortCancel operation and return to pre-sequence state git revert --quitCan be used to start over after a failed cherry-pick or revert
Git revert conflictsGit revert conflicts
When to use git reset vs. git revertWhen to use git reset vs. git revert
Permanent MistakesPermanent Mistakes
Permanent MistakesPermanent Mistakes
git push -f origin <REMOTE>
SummarySummaryIf you run into git problems, the solution is most likely touse one of these --
git rebasegit resetgit refloggit revert
III. Automate gitIII. Automate git
Problem: Repetitive branch deletingProblem: Repetitive branch deleting
vagrant@precise64:~/work/savage$ git push origin :73997184To [email protected]:onceuponatimeforever/savage.git - [deleted] 73997184vagrant@precise64:~/work/savage$ git push origin :76321732-on-masterTo [email protected]:onceuponatimeforever/savage.git - [deleted] 76321732-on-mastervagrant@precise64:~/work/savage$ git push origin :77910316 To [email protected]:onceuponatimeforever/savage.git - [deleted] 77910316vagrant@precise64:~/work/savage$ git push origin :77910316-bottleTo [email protected]:onceuponatimeforever/savage.git - [deleted] 77910316-bottlevagrant@precise64:~/work/savage$ git push origin :77910316-bottle-from-scratchTo [email protected]:onceuponatimeforever/savage.git - [deleted] 77910316-bottle-from-scratchvagrant@precise64:~/work/savage$ git push origin :2.0/master
IPython notebook supports gitIPython notebook supports git
Solution: IPython notebook scriptSolution: IPython notebook script
Problem: Fetch & rebase repetitionsProblem: Fetch & rebase repetitions
cd <PROJECT>git fetch upstreamgit rebase upstream/master
cd ../<ANOTHER_PROJECT>git fetch upstreamgit rebase upstream/master
# And again....
Solution: IPython notebook scriptSolution: IPython notebook scriptlist_of_projects = ['ipython', 'oh-mainline']
def update(project): %cd {project} !git checkout master !git fetch upstream !git rebase upstream/master !git push origin master %cd .. print "Done git rebasing this repoo: ", {project} for project in list_of_projects: update(list_of_projects[project]) print "---------------------------------"
Solution: IPython notebook scriptSolution: IPython notebook script
Thanks! Q&A Time.Thanks! Q&A Time.