SlideShare ist ein Scribd-Unternehmen logo
1 von 91
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Git more
Carl Jiang
April 2014
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.2
Git more
Slides Prerequisites:
Assume audiences have already
attended Dream’s previous session
about Git or they have already
mastered basic idea about Git.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.3
Git more
• Git can be powerful
• Git can be confusing
• Best Practice we should follow
• Gotchas
• Tips
• Workflow in SP
• References
Agenda
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.4
Git can be powerful
• Most operations are offline, extremely FAST. Branches are CHEAP.
• Want to refine commit message? No problem.
• Want to reorder commits? No problem.
• Want to merge certain commits into one? No problem.
• Committed too often, too fast, so you end up having some commits with typos, trivial bugs. Want to
fix them? No problem.
• Accidently lost some changes? Want to get them back? No problem, as far as it was committed,
stashed, or even “git added” before.
• After 10 commits, you noticed that you were working on the wrong branch? No problem.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.5
Git can be powerful
as far as you understand it
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.6
Git more
• Git can be powerful
• Git can be confusing
• Best Practice we should follow
• Gotchas
• Tips
• Workflow in SP
• References
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.7
#1 git push & pull
Git can be confusing
Beautiful work, let me try git push
now.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.8
git push & pull
Git can be confusing
I already committed something.
What happened? Let me try git
pull instead?
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.9
git push & pull
Git can be confusing
What’s wrong?
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.10
tracked, remote tracking branch, Upstream
Git can be confusing
• Git add/commit makes a file “tracked” by Git
• remote-tracking branch is a local cache of the state of a branch in a remote
repository (for example, origin/master)
• You can create a new local branch to track “remote-tracking branch”. And this
“remote-tracking branch” is now “Upstream” to your local branch.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.11
What does registering upstream bring us?
Git can be confusing
Having an upstream branch registered for a local branch will benefit:
• tell git to show the relationship between the
two branches in git status and git branch -v.
• directs git pull without arguments to pull from the upstream when the new branch is
checked out.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.12
Other ways to check upstream branch relationship
Git can be confusing
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.13
How to setup upstream branch?
Git can be confusing
To solve this problem,
Set upstream branch relationship like what Git has been trying to tell you:
git branch --set-upstream-to=origin/branch_name
Or
git branch -u origin/branch_name
Or
git pull with specific remote name and branch name
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.14
How to setup upstream branch automatically?
Git can be confusing
Good news is you already do..(sort of)
There is a git config named “branch.autosetupmerge” decides whether automatic setup
upstream when checkout starting point is a remote-tracking branch;
You all know what does “false” means here.
Default value is “true”
This means if you are checking out a new local branch from a remote-tracking branch you will always (by
default) have a new local branch with upstream setup correctly.
And there is another valid value “always”
automatic setup is done when the starting point is either a local branch or remote-tracking branch.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.15
What does setuping upstream do behind the scene?
Git can be confusing
After you setup upstream branch, if you check.git/config, you will find:
This means it’s part of git config, you can also setup upstream branch by using “git
config”
git config branch.branch_name.remote remote_repository
git config branch.branch_name.merge upstream_branch_name
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.16
git pull problem solved.
Git can be confusing
This is what Older version of Git has been
trying to tells us……
A little too complicated?
That’s why they simplified the messages
in newer version.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.17
git push problem solved?
Git can be confusing
Now, do you know why we failed to “git push”
last time? I bet you do.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.18
git push problem solved?
Git can be confusing
But you probably don’t.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.19
Another git config “push.default” (since Git 1.6.3)
Git can be confusing
• nothing: do not push anything
• matching: push all matching branches
All branches having the same name in both ends are considered to be matching. This is the default
in Git 1.x.
• current: push the current branch to a branch of the same name. (I consider it’s light version of “matching”)
• upstream: push the current branch to its upstream branch (they used to call it tracking, now it is a
deprecated synonym for upstream)
• simple: (since Git 1.7.11) like upstream, but refuses to push if the upstream branch's name is different from
the local one
This is the safest option and is well-suited for beginners. This will become the default in Git 2.0.
The simple, current and upstream modes are for those who want to push out a single branch after finishing
work, even when the other branches are not yet ready to be pushed out
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.20
git push problem solved?
Git can be confusing
So what you should do is
git push -u origin branch_name
for the first time. And you are welcomed to
use
git push
starting from 2nd time
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.21
git push problem solved?
Git can be confusing
Summary:
• git pull & push need “upstream” set to work properly(mindless).
• “git push” by default push all “matching” branch to remote, which is something 99.9% time we don’t
want. We should change it to “simple” or “upstream”
• Does it pull everything from remote? We don’t know that yet.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.22
#2 git pull generates empty merge commits
Git can be confusing
Why does git generate such a meaningless
empty merge commit?
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.23
git pull = git fetch + git merge/rebase
Git can be confusing
Default git pull = git fetch + git merge FETCH_HEAD
So git fetch (update) ALL remoting-tracking branch from remote repository
and
mark upstream of current branch in FETCH_HEAD
and
merge freshly fetched remote-tracking branch into local branch
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.24
git pull = git fetch + git merge/rebase
Git can be confusing
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.25
git pull = git fetch + git merge/rebase
Git can be confusing
What should we do?
We should use
git pull --rebase
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.26
Make git pull by default git pull –rebase
Git can be confusing
Setup git config ‘branch.autosetuprebase’
This option controls whether new branches should be set up to be rebased upon git pull, i.e. your
setting of always will result in branches being set up such that git pull always performs a rebase, not a
merge.
(Be aware that existing branches retain their configuration when you change this option.)
# make `git pull` on master always use rebase
$ git config branch.master.rebase true and Yes, it is stored as plain
text in the .git/config
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.27
git pull = git fetch + git merge/rebase
Git can be confusing
Or
What do I do is I DONT use git pull at all.
I usually use
git fetch origin -p or git remote update -p
then
use git status/git log/git diff to check my remote tracking branch.
Then I will decide whether I use git merge to fast-forward changes or git rebase my branch or email
someone asking about changes before I do anything.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.28
git pull = git fetch + git merge/rebase
Git can be confusing
Summary:
• git pull fetch all changes from remote branch and only update current branch based it’s
upstream branch.
• git pull by default merge upstream branch into current local branch
• We may want to use git pull --rebase or make it default setting.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.29
#3 git rebase squeeze “bubbles”
Git can be confusing
preview of those bubbles
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.30
#3 git rebase squeeze “bubbles”
Git can be confusing
Let’s squeeze it
with
git rebase -i HEAD~2
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.31
#3 git rebase squeeze “bubbles”
Git can be confusing
Bubbles gone! Pew!
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.32
#3 git rebase squeeze “bubbles”
Git can be confusing
git reset --hard 51c43b4
git reflog master
How to recover?
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.33
#3 git rebase squeeze “bubbles”
Git can be confusing
git rebase –i --preserve-merges HEAD~2
Or
git rebase -i @{u}
How to prevent?
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.34
#3 git rebase squeeze “bubbles”
Git can be confusing
How to prevent “git pull --rebase” from squeezing “bubble”?
git pull --rebase=preserve
Since Git 1.8.5
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.35
#4 git rebase
Git can be confusing
git checkout experiment
git rebase master
Pictures From “Pro Git”
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.36
git rebase
Git can be confusing
git rebase --onto master server
client
Pictures From “Pro Git”
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.37
git rebase
Git can be confusing
git checkout experiment
git rebase master
git rebase --onto master master experiment
Pictures From “Pro Git”
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.38
git rebase v.s cherry-pick
Git can be confusing
git rebase's task is to forward-port a series of changes a developer has in its private repository,
created against version X of some upstream branch,
git cherry-pick is for bringing an interesting commit from one line of development to another.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.39
git rebase v.s cherry-pick
Git can be confusing
Well…….
Since its first appearance, git cherry-pick learned to be able to pick several commits at once,
one-by-one.
e.g git cherry-pick 234d34a 3ak23akl 6u2k12k
Since Git 1.7.2+, it now supports ranges
e.g. git cherry-pick A..B
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.40
#5 git rebase -- conflict
Git can be confusing
I just invoked git rebase
otherbranch, and encounter
“CONFLICT”,
It tells me to run “git rebase –
continue” after resolve it.
no big deal!
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.41
git rebase -- conflict
Git can be confusing
I just resolved it by accepting
changes from one side.
And run “git rebase –continue”
Huh? Git seems still not happy.
Ok, I forget to use “git add” to mark
“resolved”, easy.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.42
git rebase -- conflict
Git can be confusing
What?
I just run “git add .”
And Git is still not satisfied.
What now?
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.43
git rebase -- conflict
Git can be confusing
Some times, conflicts occurs not just because both
branch modified on same places of same files. Someone
could have done exactly same thing like you did in this
single commit.
So after you resolved the conflict, you might end up
seeing Git tells you
“hey, nothing to commit, working directory clean”.
That’s totally ok, move on. Just skip this patch (git rebase
--skip)
It’s called “英雄所见略同” in Chinese.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.44
#6 git reset
Git can be confusing
• ALL reset moves HEAD to specified commit.
• git reset never touches untracked files. git clean will.
• git reset --hard resets the index and working tree (but not untracked files). Very
dangerous.
• git reset --soft does NOT reset index or working tree. BUT it will move all changes files
from resetted commits to index.(so git status could see it)
• git reset --mixed (this is default) resets the index but not the working tree (i.e., the changed
files are preserved but not marked for commit). You can call it “unstage”.
• git reset --merge save you from middle of merge conflict
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.45
git reset --hard HEAD
Git can be confusing
BEFORE AFTER
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.46
git reset --soft HEAD
Git can be confusing
BEFORE AFTER
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.47
git reset HEAD (--mixed)
Git can be confusing
BEFORE AFTER
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.48
git reset HEAD~1
Git can be confusing
• git reset --hard all committed or added file are GONE. Only untracked file are kept.
• git reset --soft move all changes files from commit 3 will be moved to index(stage). (as they have
been added, ready for commit)
• git reset --mixed move all changes files from commit 3 will be moved to working file. (as they have
been modified, ready for git add)
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.49
Commands & areas
Git can be confusing
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.50
Commands & areas
Git can be confusing
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.51
Commands & areas
Git can be confusing
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.52
Git more
• Git can be powerful
• Git can be confusing
• Best Practice we should follow
• Gotchas
• Tips
• Workflow in SP
• References
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.53
Best Practice we should follow
• Try to use latest version of git
• Commit Often, Perfect Later, Publish Once
• Do make useful commit messages
• Don't change published history
• Do choose a workflow
• Don’t Panic
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.54
Characters of commits
Best Practice we should follow
“As a general rule, you should try to split your changes into small logical
steps, and commit each of them. They should be consistent, working
independently of any later commits, pass the test suite, etc. This makes the
review process much easier, and the history much more useful for later
inspection and analysis, for example with git-blame(1) and git-bisect(1).”
From https://www.kernel.org/pub/software/scm/git/docs/gitworkflows.html
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.55
Best Practice we should follow
Do experiments(merge, rebase, cherry-pick, reset ….) on a new branch,
so you can switch back easily
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.56
Best Practice we should follow
Squash duplicates commits into one commit for sure
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.57
Best Practice we should follow
Write commit message more specific,
changed WHAT, for WHAT REASON, etc.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.58
Best Practice we should follow
Avoid creating “empty merge commit“
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.59
Best Practice we should follow
But try to keep history of feature
branch, which will be delete
after merge, in master by
creating “empty merge commit”
explicitly
Use git merge --no-ff
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.60
Git more
• Git can be powerful
• Git can be confusing
• Best Practice we should follow
• Gotchas
• Tips
• Workflow in SP
• References
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.61
Gotchas
Easy one,
Just git branch new_branch_name
And
git reset --hard
HEAD~HOW_MANY_COMMITS_YOU_HAVE_MADE
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.62
Git stash lost
Gotchas
If you have only just popped it and the terminal is still
open, you should be able to see the SHA hash value
when you popped it
Or
gitk --all $( git fsck --no-reflog | awk '/dangling commit/
{print $3}' )
Try to find poped stash as a dangling commit in the
window
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.63
git merged while should have rebased
Gotchas
git reset --hard HEAD
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.64
Git more
• Git can be powerful
• Git can be confusing
• Best Practice we should follow
• Gotchas
• Tips
• Workflow in SP
• References
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.65
Tips
@{-1} or - represent last branch
For example, git checkout - to checkout last branch, git merge - to merge last branch
@{u} upstream
git reset --hard @{u}
- reset to upstream state
git log @{u}..HEAD
git add -u
- help you to “git rm” those files you have already manually removed.
git add .
git add -A .= git add . + git add -u .
git config merge.defaultToUpstream true
- so you can use git merge without any parameters directly merge upstream
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.66
git commit -a --fixup (commits you want to fix) Since Git 1.7
Tips
And then run git rebase -i @{u}
You will see
pick SHA message_of_commit_you_want_to_fix
fixup SHA fixup! message_of_commit_you_want_to_fix
pick SHA other commits
So the last commit will be automatically “merged” into your commit history
Assume you all know about
“git commit --amend“, and
“git rebase -i"
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.67
Delete branch from remote repository
Tips
Sicne Git v1.7.0, you can delete a remote branch using
git push origin --delete <branchName>
which is easier to remember than
git push origin :<branchName>
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.68
Git GUI client
Tips
• SmartGit -- not free
• GitExtensions
• Atlassian SourceTree
• gitk (from Git)
• tig (https://github.com/jonas/tig)
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.69
SmartGit
Tips
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.70
GitExtensions
Tips
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.71
Atlassian SourceTree
Tips
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.72
gitk
Tips
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.73
tig
Tips
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.74
Git more
• Git can be powerful
• Git can be confusing
• Best Practice we should follow
• Gotchas
• Tips
• Workflow in SP
• References
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.75
Branches
Workflow in SP
• Master branch (develop)
• Feature branches
• Bugfix branches
• Maintenance branches
• Hotfix branches
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.76
Branches
Workflow in SP
Master branch
We consider origin/master to be the
main branch where the source code
of HEAD always reflects a state with
the latest delivered development
changes for the next release.
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.77
Branches
Workflow in SP
Feature branch
Feature branches (or sometimes called topic
branches) are used to develop new features for
the upcoming or a distant future release. When
starting development of a feature, the target
release in which this feature will be incorporated
may well be unknown at that point. The essence
of a feature branch is that it exists as long as
the feature is in development, but will eventually
be merged back into develop (to definitely add
the new feature to the upcoming release) or
discarded (in case of a disappointing
experiment).
Naming convention: feature-AGM_STORY_NO
Forked from master, merged back to master
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.78
Branches
Workflow in SP
Bugfix branch
Bugfix branch is like feature branch, it’s
used to develop “bugfix” for any defect not
going to be a hotfix for any previous release.
Naming convention: bug-
AGM_DEFECT_NO
Forked from master, merged back to master
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.79
Branches
Workflow in SP
Maintenance branch
It acts like “master” branch for previous
release if any hot fix and new version for
previous releases are needed. Normally
unplanned, doesn’t have to be created
all the time. Used to group several hotfix
and prepare for newer version for
Naming convention: maintenance-
release_no
Forked from release tag, merged back to
master
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.80
Branches
Workflow in SP
Hotfix branch
Acts like “bugfix” branch for maintenance
branch, develop hotfix on this branch and
release them as planed.
Naming convention: hotfix-AGM-DEFECT
Forked from maintenance branch, merged
back to maintenance branch
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.81
Practice - working on feature branch
• (Create) Fork from master
– git fetch origin -p
– git checkout -b feature-AGM_STORY_NO origin/master
• Work/UT
– ….
– git commit
Workflow in SP
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.82
Practice - working on feature branch
• Push to remote to work with others
– git push -u origin feature-AGM_STORY_NO #first time
– git push
• Work on existing feature branch with other team members
– git checkout -t origin/master/feature-AGM_STORY_NO
Workflow in SP
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.83
Practice - working on feature branch
• Clean up/merge commits history BEFORE merging back to master
– git rebase -i @{u}
• Rebase with master
– git fetch origin -p
– git rebase origin/master
• Push back to origin
• Make sure you don’t do this without letting others know if it’s shared branch
– git push -f
Workflow in SP
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.84
Practice - working on feature branch
• Request code review
Feature owner need to do this before he merges completed(UT passed) feature branch
back to master
– git log origin/master --not HEAD
Or
- git log HEAD..origin/master
• Before we introduce GitLab, we use email
Workflow in SP
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.85
Practice - working on feature branch
• Merge back into master
• with explicitly no fast-forward, so where every feature
has its' own "bubble" with clear merge point)
• (ONLY features and bugfixs in the scope of very next
release)
– git fetch origin -p
– git rebase origin/master
– git checkout master
– git merge origin/master
– git merge --no-ff feature-AGM_STORY_NO
– git push origin master
• Delete local and remote branch
– git branch -d feature-AGM_STORY_NO
– git push origin :feature-AGM_STORY_NO
Workflow in SP
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.86
"Pre-RC/RC" Maintenance
If some bug were found by QA or developer for feature and bugfix before "RC Testing"
phase, no new AGM defect will be open. A new task will be added into original AGM story.
Recreate a feature branch from master, and fix the bug…
If some bug were found by QA or developer for feature and bugfix during "RC Testing"
phase, QA can create a new defect in AGM.
Create a defect branch from master, and fix the bug…
Workflow in SP
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.87
Release
Release from master, stable status after several round of RC test and bug fix. Properly
tagged (with Maven plugin)
Workflow in SP
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.88
Post-Release Maintenance
If some bugs were raised after release and need a hot fix, create a maintenance branch
from release tag, and then create hotfix branch from that. After hotfix branch passed UT,
cleanup and rebase
Workflow in SP
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.89
Git more
• Git can be powerful
• Git can be confusing
• Best Practice we should follow
• Gotchas
• Tips
• Workflow in SP
• References
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.90
References
• http://wholemeal.co.nz/blog/2010/06/11/no-changes-did-you-forget-to-use-git-add/
• http://mislav.uniqpath.com/2010/07/git-tips/
• http://felipec.wordpress.com/2013/09/01/advanced-git-concepts-the-upstream-tracking-branch/
• http://longair.net/blog/2009/04/16/git-fetch-and-merge/
• http://programmers.stackexchange.com/questions/114249/git-what-gotchas-should-newcomers-to-version-control-
be-aware-of
• http://stackoverflow.com/questions/948354/git-push-default-behavior
• http://johnkary.net/blog/git-add-p-the-most-powerful-git-feature-youre-not-using-yet/
• http://www.reviewboard.org/docs/codebase/dev/git/clean-commits/
• http://longair.net/blog/2012/05/07/the-most-confusing-git-terminology/
• http://stackoverflow.com/questions/2530060/can-you-explain-what-git-reset-does-in-plain-english/2530073#2530073
• http://cheat.errtheblog.com/s/git
• http://stackoverflow.com/questions/6406762/why-am-i-merging-remote-tracking-branch-origin-develop-into-develop
• http://stackoverflow.com/questions/7388278/you-asked-me-to-pull-without-telling-me-which-branch-you-want-to-
merge-with
• http://stackoverflow.com/questions/8170558/git-push-set-target-for-branch
• http://ndpsoftware.com/git-cheatsheet.html
• http://baozoumanhua.com
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Thank you

Weitere ähnliche Inhalte

Ähnlich wie Git more

Don't Let Git Get Your Goat!
Don't Let Git Get Your Goat!Don't Let Git Get Your Goat!
Don't Let Git Get Your Goat!CollabNet
 
git-commands-cheat-sheet-infopediya-com.pdf
git-commands-cheat-sheet-infopediya-com.pdfgit-commands-cheat-sheet-infopediya-com.pdf
git-commands-cheat-sheet-infopediya-com.pdfmurad khan
 
Introduction to Git for Artists
Introduction to Git for ArtistsIntroduction to Git for Artists
Introduction to Git for ArtistsDavid Newbury
 
Version Control ThinkVitamin
Version Control ThinkVitaminVersion Control ThinkVitamin
Version Control ThinkVitaminAlex Hillman
 
Getting Into Git
Getting Into GitGetting Into Git
Getting Into GitRick Umali
 
Git Merge, Resets and Branches
Git Merge, Resets and BranchesGit Merge, Resets and Branches
Git Merge, Resets and BranchesVictor Pudelski
 
Checkitmobile advanced git
Checkitmobile advanced gitCheckitmobile advanced git
Checkitmobile advanced gitGerrit Wanderer
 
Getting started With GIT
Getting started With GITGetting started With GIT
Getting started With GITGhadiAlGhosh
 
Git workflows presentation
Git workflows presentationGit workflows presentation
Git workflows presentationMack Hardy
 
Introduction to Git
Introduction to GitIntroduction to Git
Introduction to GitRick Umali
 
Brief tutorial on Git
Brief tutorial on GitBrief tutorial on Git
Brief tutorial on Git聖文 鄭
 
Getting Started with Git
Getting Started with GitGetting Started with Git
Getting Started with GitRick Umali
 

Ähnlich wie Git more (20)

Don't Let Git Get Your Goat!
Don't Let Git Get Your Goat!Don't Let Git Get Your Goat!
Don't Let Git Get Your Goat!
 
Github By Nyros Developer
Github By Nyros DeveloperGithub By Nyros Developer
Github By Nyros Developer
 
git-commands-cheat-sheet-infopediya-com.pdf
git-commands-cheat-sheet-infopediya-com.pdfgit-commands-cheat-sheet-infopediya-com.pdf
git-commands-cheat-sheet-infopediya-com.pdf
 
Introduction to Git for Artists
Introduction to Git for ArtistsIntroduction to Git for Artists
Introduction to Git for Artists
 
Git best practices workshop
Git best practices workshopGit best practices workshop
Git best practices workshop
 
Version Control ThinkVitamin
Version Control ThinkVitaminVersion Control ThinkVitamin
Version Control ThinkVitamin
 
Getting Into Git
Getting Into GitGetting Into Git
Getting Into Git
 
Git Merge, Resets and Branches
Git Merge, Resets and BranchesGit Merge, Resets and Branches
Git Merge, Resets and Branches
 
Git
GitGit
Git
 
Checkitmobile advanced git
Checkitmobile advanced gitCheckitmobile advanced git
Checkitmobile advanced git
 
Getting started With GIT
Getting started With GITGetting started With GIT
Getting started With GIT
 
Git-r-Done
Git-r-DoneGit-r-Done
Git-r-Done
 
Git workflows presentation
Git workflows presentationGit workflows presentation
Git workflows presentation
 
Git and Github workshop
Git and Github workshopGit and Github workshop
Git and Github workshop
 
Introduction to git & github
Introduction to git & githubIntroduction to git & github
Introduction to git & github
 
Introduction to Git
Introduction to GitIntroduction to Git
Introduction to Git
 
Brief tutorial on Git
Brief tutorial on GitBrief tutorial on Git
Brief tutorial on Git
 
GIT from n00b
GIT from n00bGIT from n00b
GIT from n00b
 
Getting Started with Git
Getting Started with GitGetting Started with Git
Getting Started with Git
 
Lets git to it
Lets git to itLets git to it
Lets git to it
 

Kürzlich hochgeladen

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 

Kürzlich hochgeladen (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Git more

  • 1. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. Git more Carl Jiang April 2014
  • 2. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.2 Git more Slides Prerequisites: Assume audiences have already attended Dream’s previous session about Git or they have already mastered basic idea about Git.
  • 3. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.3 Git more • Git can be powerful • Git can be confusing • Best Practice we should follow • Gotchas • Tips • Workflow in SP • References Agenda
  • 4. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.4 Git can be powerful • Most operations are offline, extremely FAST. Branches are CHEAP. • Want to refine commit message? No problem. • Want to reorder commits? No problem. • Want to merge certain commits into one? No problem. • Committed too often, too fast, so you end up having some commits with typos, trivial bugs. Want to fix them? No problem. • Accidently lost some changes? Want to get them back? No problem, as far as it was committed, stashed, or even “git added” before. • After 10 commits, you noticed that you were working on the wrong branch? No problem.
  • 5. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.5 Git can be powerful as far as you understand it
  • 6. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.6 Git more • Git can be powerful • Git can be confusing • Best Practice we should follow • Gotchas • Tips • Workflow in SP • References
  • 7. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.7 #1 git push & pull Git can be confusing Beautiful work, let me try git push now.
  • 8. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.8 git push & pull Git can be confusing I already committed something. What happened? Let me try git pull instead?
  • 9. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.9 git push & pull Git can be confusing What’s wrong?
  • 10. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.10 tracked, remote tracking branch, Upstream Git can be confusing • Git add/commit makes a file “tracked” by Git • remote-tracking branch is a local cache of the state of a branch in a remote repository (for example, origin/master) • You can create a new local branch to track “remote-tracking branch”. And this “remote-tracking branch” is now “Upstream” to your local branch.
  • 11. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.11 What does registering upstream bring us? Git can be confusing Having an upstream branch registered for a local branch will benefit: • tell git to show the relationship between the two branches in git status and git branch -v. • directs git pull without arguments to pull from the upstream when the new branch is checked out.
  • 12. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.12 Other ways to check upstream branch relationship Git can be confusing
  • 13. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.13 How to setup upstream branch? Git can be confusing To solve this problem, Set upstream branch relationship like what Git has been trying to tell you: git branch --set-upstream-to=origin/branch_name Or git branch -u origin/branch_name Or git pull with specific remote name and branch name
  • 14. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.14 How to setup upstream branch automatically? Git can be confusing Good news is you already do..(sort of) There is a git config named “branch.autosetupmerge” decides whether automatic setup upstream when checkout starting point is a remote-tracking branch; You all know what does “false” means here. Default value is “true” This means if you are checking out a new local branch from a remote-tracking branch you will always (by default) have a new local branch with upstream setup correctly. And there is another valid value “always” automatic setup is done when the starting point is either a local branch or remote-tracking branch.
  • 15. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.15 What does setuping upstream do behind the scene? Git can be confusing After you setup upstream branch, if you check.git/config, you will find: This means it’s part of git config, you can also setup upstream branch by using “git config” git config branch.branch_name.remote remote_repository git config branch.branch_name.merge upstream_branch_name
  • 16. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.16 git pull problem solved. Git can be confusing This is what Older version of Git has been trying to tells us…… A little too complicated? That’s why they simplified the messages in newer version.
  • 17. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.17 git push problem solved? Git can be confusing Now, do you know why we failed to “git push” last time? I bet you do.
  • 18. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.18 git push problem solved? Git can be confusing But you probably don’t.
  • 19. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.19 Another git config “push.default” (since Git 1.6.3) Git can be confusing • nothing: do not push anything • matching: push all matching branches All branches having the same name in both ends are considered to be matching. This is the default in Git 1.x. • current: push the current branch to a branch of the same name. (I consider it’s light version of “matching”) • upstream: push the current branch to its upstream branch (they used to call it tracking, now it is a deprecated synonym for upstream) • simple: (since Git 1.7.11) like upstream, but refuses to push if the upstream branch's name is different from the local one This is the safest option and is well-suited for beginners. This will become the default in Git 2.0. The simple, current and upstream modes are for those who want to push out a single branch after finishing work, even when the other branches are not yet ready to be pushed out
  • 20. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.20 git push problem solved? Git can be confusing So what you should do is git push -u origin branch_name for the first time. And you are welcomed to use git push starting from 2nd time
  • 21. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.21 git push problem solved? Git can be confusing Summary: • git pull & push need “upstream” set to work properly(mindless). • “git push” by default push all “matching” branch to remote, which is something 99.9% time we don’t want. We should change it to “simple” or “upstream” • Does it pull everything from remote? We don’t know that yet.
  • 22. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.22 #2 git pull generates empty merge commits Git can be confusing Why does git generate such a meaningless empty merge commit?
  • 23. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.23 git pull = git fetch + git merge/rebase Git can be confusing Default git pull = git fetch + git merge FETCH_HEAD So git fetch (update) ALL remoting-tracking branch from remote repository and mark upstream of current branch in FETCH_HEAD and merge freshly fetched remote-tracking branch into local branch
  • 24. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.24 git pull = git fetch + git merge/rebase Git can be confusing
  • 25. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.25 git pull = git fetch + git merge/rebase Git can be confusing What should we do? We should use git pull --rebase
  • 26. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.26 Make git pull by default git pull –rebase Git can be confusing Setup git config ‘branch.autosetuprebase’ This option controls whether new branches should be set up to be rebased upon git pull, i.e. your setting of always will result in branches being set up such that git pull always performs a rebase, not a merge. (Be aware that existing branches retain their configuration when you change this option.) # make `git pull` on master always use rebase $ git config branch.master.rebase true and Yes, it is stored as plain text in the .git/config
  • 27. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.27 git pull = git fetch + git merge/rebase Git can be confusing Or What do I do is I DONT use git pull at all. I usually use git fetch origin -p or git remote update -p then use git status/git log/git diff to check my remote tracking branch. Then I will decide whether I use git merge to fast-forward changes or git rebase my branch or email someone asking about changes before I do anything.
  • 28. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.28 git pull = git fetch + git merge/rebase Git can be confusing Summary: • git pull fetch all changes from remote branch and only update current branch based it’s upstream branch. • git pull by default merge upstream branch into current local branch • We may want to use git pull --rebase or make it default setting.
  • 29. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.29 #3 git rebase squeeze “bubbles” Git can be confusing preview of those bubbles
  • 30. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.30 #3 git rebase squeeze “bubbles” Git can be confusing Let’s squeeze it with git rebase -i HEAD~2
  • 31. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.31 #3 git rebase squeeze “bubbles” Git can be confusing Bubbles gone! Pew!
  • 32. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.32 #3 git rebase squeeze “bubbles” Git can be confusing git reset --hard 51c43b4 git reflog master How to recover?
  • 33. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.33 #3 git rebase squeeze “bubbles” Git can be confusing git rebase –i --preserve-merges HEAD~2 Or git rebase -i @{u} How to prevent?
  • 34. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.34 #3 git rebase squeeze “bubbles” Git can be confusing How to prevent “git pull --rebase” from squeezing “bubble”? git pull --rebase=preserve Since Git 1.8.5
  • 35. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.35 #4 git rebase Git can be confusing git checkout experiment git rebase master Pictures From “Pro Git”
  • 36. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.36 git rebase Git can be confusing git rebase --onto master server client Pictures From “Pro Git”
  • 37. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.37 git rebase Git can be confusing git checkout experiment git rebase master git rebase --onto master master experiment Pictures From “Pro Git”
  • 38. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.38 git rebase v.s cherry-pick Git can be confusing git rebase's task is to forward-port a series of changes a developer has in its private repository, created against version X of some upstream branch, git cherry-pick is for bringing an interesting commit from one line of development to another.
  • 39. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.39 git rebase v.s cherry-pick Git can be confusing Well……. Since its first appearance, git cherry-pick learned to be able to pick several commits at once, one-by-one. e.g git cherry-pick 234d34a 3ak23akl 6u2k12k Since Git 1.7.2+, it now supports ranges e.g. git cherry-pick A..B
  • 40. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.40 #5 git rebase -- conflict Git can be confusing I just invoked git rebase otherbranch, and encounter “CONFLICT”, It tells me to run “git rebase – continue” after resolve it. no big deal!
  • 41. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.41 git rebase -- conflict Git can be confusing I just resolved it by accepting changes from one side. And run “git rebase –continue” Huh? Git seems still not happy. Ok, I forget to use “git add” to mark “resolved”, easy.
  • 42. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.42 git rebase -- conflict Git can be confusing What? I just run “git add .” And Git is still not satisfied. What now?
  • 43. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.43 git rebase -- conflict Git can be confusing Some times, conflicts occurs not just because both branch modified on same places of same files. Someone could have done exactly same thing like you did in this single commit. So after you resolved the conflict, you might end up seeing Git tells you “hey, nothing to commit, working directory clean”. That’s totally ok, move on. Just skip this patch (git rebase --skip) It’s called “英雄所见略同” in Chinese.
  • 44. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.44 #6 git reset Git can be confusing • ALL reset moves HEAD to specified commit. • git reset never touches untracked files. git clean will. • git reset --hard resets the index and working tree (but not untracked files). Very dangerous. • git reset --soft does NOT reset index or working tree. BUT it will move all changes files from resetted commits to index.(so git status could see it) • git reset --mixed (this is default) resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit). You can call it “unstage”. • git reset --merge save you from middle of merge conflict
  • 45. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.45 git reset --hard HEAD Git can be confusing BEFORE AFTER
  • 46. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.46 git reset --soft HEAD Git can be confusing BEFORE AFTER
  • 47. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.47 git reset HEAD (--mixed) Git can be confusing BEFORE AFTER
  • 48. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.48 git reset HEAD~1 Git can be confusing • git reset --hard all committed or added file are GONE. Only untracked file are kept. • git reset --soft move all changes files from commit 3 will be moved to index(stage). (as they have been added, ready for commit) • git reset --mixed move all changes files from commit 3 will be moved to working file. (as they have been modified, ready for git add)
  • 49. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.49 Commands & areas Git can be confusing
  • 50. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.50 Commands & areas Git can be confusing
  • 51. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.51 Commands & areas Git can be confusing
  • 52. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.52 Git more • Git can be powerful • Git can be confusing • Best Practice we should follow • Gotchas • Tips • Workflow in SP • References
  • 53. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.53 Best Practice we should follow • Try to use latest version of git • Commit Often, Perfect Later, Publish Once • Do make useful commit messages • Don't change published history • Do choose a workflow • Don’t Panic
  • 54. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.54 Characters of commits Best Practice we should follow “As a general rule, you should try to split your changes into small logical steps, and commit each of them. They should be consistent, working independently of any later commits, pass the test suite, etc. This makes the review process much easier, and the history much more useful for later inspection and analysis, for example with git-blame(1) and git-bisect(1).” From https://www.kernel.org/pub/software/scm/git/docs/gitworkflows.html
  • 55. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.55 Best Practice we should follow Do experiments(merge, rebase, cherry-pick, reset ….) on a new branch, so you can switch back easily
  • 56. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.56 Best Practice we should follow Squash duplicates commits into one commit for sure
  • 57. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.57 Best Practice we should follow Write commit message more specific, changed WHAT, for WHAT REASON, etc.
  • 58. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.58 Best Practice we should follow Avoid creating “empty merge commit“
  • 59. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.59 Best Practice we should follow But try to keep history of feature branch, which will be delete after merge, in master by creating “empty merge commit” explicitly Use git merge --no-ff
  • 60. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.60 Git more • Git can be powerful • Git can be confusing • Best Practice we should follow • Gotchas • Tips • Workflow in SP • References
  • 61. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.61 Gotchas Easy one, Just git branch new_branch_name And git reset --hard HEAD~HOW_MANY_COMMITS_YOU_HAVE_MADE
  • 62. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.62 Git stash lost Gotchas If you have only just popped it and the terminal is still open, you should be able to see the SHA hash value when you popped it Or gitk --all $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' ) Try to find poped stash as a dangling commit in the window
  • 63. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.63 git merged while should have rebased Gotchas git reset --hard HEAD
  • 64. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.64 Git more • Git can be powerful • Git can be confusing • Best Practice we should follow • Gotchas • Tips • Workflow in SP • References
  • 65. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.65 Tips @{-1} or - represent last branch For example, git checkout - to checkout last branch, git merge - to merge last branch @{u} upstream git reset --hard @{u} - reset to upstream state git log @{u}..HEAD git add -u - help you to “git rm” those files you have already manually removed. git add . git add -A .= git add . + git add -u . git config merge.defaultToUpstream true - so you can use git merge without any parameters directly merge upstream
  • 66. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.66 git commit -a --fixup (commits you want to fix) Since Git 1.7 Tips And then run git rebase -i @{u} You will see pick SHA message_of_commit_you_want_to_fix fixup SHA fixup! message_of_commit_you_want_to_fix pick SHA other commits So the last commit will be automatically “merged” into your commit history Assume you all know about “git commit --amend“, and “git rebase -i"
  • 67. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.67 Delete branch from remote repository Tips Sicne Git v1.7.0, you can delete a remote branch using git push origin --delete <branchName> which is easier to remember than git push origin :<branchName>
  • 68. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.68 Git GUI client Tips • SmartGit -- not free • GitExtensions • Atlassian SourceTree • gitk (from Git) • tig (https://github.com/jonas/tig)
  • 69. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.69 SmartGit Tips
  • 70. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.70 GitExtensions Tips
  • 71. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.71 Atlassian SourceTree Tips
  • 72. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.72 gitk Tips
  • 73. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.73 tig Tips
  • 74. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.74 Git more • Git can be powerful • Git can be confusing • Best Practice we should follow • Gotchas • Tips • Workflow in SP • References
  • 75. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.75 Branches Workflow in SP • Master branch (develop) • Feature branches • Bugfix branches • Maintenance branches • Hotfix branches
  • 76. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.76 Branches Workflow in SP Master branch We consider origin/master to be the main branch where the source code of HEAD always reflects a state with the latest delivered development changes for the next release.
  • 77. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.77 Branches Workflow in SP Feature branch Feature branches (or sometimes called topic branches) are used to develop new features for the upcoming or a distant future release. When starting development of a feature, the target release in which this feature will be incorporated may well be unknown at that point. The essence of a feature branch is that it exists as long as the feature is in development, but will eventually be merged back into develop (to definitely add the new feature to the upcoming release) or discarded (in case of a disappointing experiment). Naming convention: feature-AGM_STORY_NO Forked from master, merged back to master
  • 78. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.78 Branches Workflow in SP Bugfix branch Bugfix branch is like feature branch, it’s used to develop “bugfix” for any defect not going to be a hotfix for any previous release. Naming convention: bug- AGM_DEFECT_NO Forked from master, merged back to master
  • 79. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.79 Branches Workflow in SP Maintenance branch It acts like “master” branch for previous release if any hot fix and new version for previous releases are needed. Normally unplanned, doesn’t have to be created all the time. Used to group several hotfix and prepare for newer version for Naming convention: maintenance- release_no Forked from release tag, merged back to master
  • 80. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.80 Branches Workflow in SP Hotfix branch Acts like “bugfix” branch for maintenance branch, develop hotfix on this branch and release them as planed. Naming convention: hotfix-AGM-DEFECT Forked from maintenance branch, merged back to maintenance branch
  • 81. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.81 Practice - working on feature branch • (Create) Fork from master – git fetch origin -p – git checkout -b feature-AGM_STORY_NO origin/master • Work/UT – …. – git commit Workflow in SP
  • 82. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.82 Practice - working on feature branch • Push to remote to work with others – git push -u origin feature-AGM_STORY_NO #first time – git push • Work on existing feature branch with other team members – git checkout -t origin/master/feature-AGM_STORY_NO Workflow in SP
  • 83. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.83 Practice - working on feature branch • Clean up/merge commits history BEFORE merging back to master – git rebase -i @{u} • Rebase with master – git fetch origin -p – git rebase origin/master • Push back to origin • Make sure you don’t do this without letting others know if it’s shared branch – git push -f Workflow in SP
  • 84. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.84 Practice - working on feature branch • Request code review Feature owner need to do this before he merges completed(UT passed) feature branch back to master – git log origin/master --not HEAD Or - git log HEAD..origin/master • Before we introduce GitLab, we use email Workflow in SP
  • 85. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.85 Practice - working on feature branch • Merge back into master • with explicitly no fast-forward, so where every feature has its' own "bubble" with clear merge point) • (ONLY features and bugfixs in the scope of very next release) – git fetch origin -p – git rebase origin/master – git checkout master – git merge origin/master – git merge --no-ff feature-AGM_STORY_NO – git push origin master • Delete local and remote branch – git branch -d feature-AGM_STORY_NO – git push origin :feature-AGM_STORY_NO Workflow in SP
  • 86. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.86 "Pre-RC/RC" Maintenance If some bug were found by QA or developer for feature and bugfix before "RC Testing" phase, no new AGM defect will be open. A new task will be added into original AGM story. Recreate a feature branch from master, and fix the bug… If some bug were found by QA or developer for feature and bugfix during "RC Testing" phase, QA can create a new defect in AGM. Create a defect branch from master, and fix the bug… Workflow in SP
  • 87. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.87 Release Release from master, stable status after several round of RC test and bug fix. Properly tagged (with Maven plugin) Workflow in SP
  • 88. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.88 Post-Release Maintenance If some bugs were raised after release and need a hot fix, create a maintenance branch from release tag, and then create hotfix branch from that. After hotfix branch passed UT, cleanup and rebase Workflow in SP
  • 89. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.89 Git more • Git can be powerful • Git can be confusing • Best Practice we should follow • Gotchas • Tips • Workflow in SP • References
  • 90. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.90 References • http://wholemeal.co.nz/blog/2010/06/11/no-changes-did-you-forget-to-use-git-add/ • http://mislav.uniqpath.com/2010/07/git-tips/ • http://felipec.wordpress.com/2013/09/01/advanced-git-concepts-the-upstream-tracking-branch/ • http://longair.net/blog/2009/04/16/git-fetch-and-merge/ • http://programmers.stackexchange.com/questions/114249/git-what-gotchas-should-newcomers-to-version-control- be-aware-of • http://stackoverflow.com/questions/948354/git-push-default-behavior • http://johnkary.net/blog/git-add-p-the-most-powerful-git-feature-youre-not-using-yet/ • http://www.reviewboard.org/docs/codebase/dev/git/clean-commits/ • http://longair.net/blog/2012/05/07/the-most-confusing-git-terminology/ • http://stackoverflow.com/questions/2530060/can-you-explain-what-git-reset-does-in-plain-english/2530073#2530073 • http://cheat.errtheblog.com/s/git • http://stackoverflow.com/questions/6406762/why-am-i-merging-remote-tracking-branch-origin-develop-into-develop • http://stackoverflow.com/questions/7388278/you-asked-me-to-pull-without-telling-me-which-branch-you-want-to- merge-with • http://stackoverflow.com/questions/8170558/git-push-set-target-for-branch • http://ndpsoftware.com/git-cheatsheet.html • http://baozoumanhua.com
  • 91. © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. Thank you

Hinweis der Redaktion

  1. So why would something like last few pages happens? It’s because you were checkouting from a local branch, so default value makes sure no upstream branch is set. Which I don’t think we should use… I don’t really want to track a local branch ever it’s automatically set up.