The document provides an extensive overview of advanced Git techniques including merging vs. rebasing, the use of reset, checkout, and revert commands, and detailed logging options. It also discusses Git hooks for automating tasks and explains how to reference commits using refs and reflogs. Key practices such as maintaining a clean commit history and the implications of using force-push on rebased branches are highlighted.
Introduction to Git by Sergiu Ioan Ungur; Overview of VCS and DVCS; Origins of Git by Linus Torvalds.
Coverage of merging vs rebasing; Importance of incorporating new commits into the feature branch.
Instructions for merging branches in Git; Discusses merge commits and their impact on branch history.
Explanation of Git rebase; Moving entire feature branches for a cleaner project history.
Using interactive rebasing for commit history control; Ability to clean up histories before merging.
Golden rules on when not to use rebasing, especially on public branches.
Safe use of force-pushing; Guidelines for local cleanup of commits in feature branches.
Fetch and incorporate changes from upstream branches using merge or rebase techniques.
Best practices for integrating approved features into the main code base using rebase and merge.
Comparison of git rebase and git merge; Emphasizes history clarity and preserving commit history.
Overview of git reset, checkout, and revert; Key tools for undoing changes in Git.
Describing git reset flags and implications on branches and working directory.
Instructions for switching branches using git checkout.
Details on using git revert safely to undo commits without altering public history.
File-level operations for git reset and checkout with scope parameters.
Git log formatting techniques and filtering commit history information effectively. Different ways to filter commit history by date, author, message, and file.
Introduction to Git hooks; how they can automate processes during commit events.
Exploration of referencing commits using hashes, refs, their specs, and utilization of reflog.
Resources for using Git aliases effectively and additional references for Git learning.
Overview
Version Control System(VCS)
● Merging
● Time capsule
Distributed Version Control System (DVCS)
● Central repository
● Local copy of the repository
3.
Origins
Linus Torvalds (2005)
●Linux Kernel project.
● Meant to be distributed, fast and more
natural.
● Capable of handling large projects.
Merge option
● Resultsin a new
“merge commit”.
● Merge is “nice”
(non-destructive).
● But... it pollutes
your feature
branch’s history.
9.
Rebase option
$ gitcheckout feature
$ git rebase master
● Results in moving the entire feature branch
to begin on the tip of the master branch.
10.
Rebase option
● Rebasingre-
writes the
project history.
● A much cleaner
project history:
perfectly linear
project history.
11.
Interactive rebasing
● Completecontrol over the branch’s commit
history.
● Clean up a messy history before merging a
feature branch into master.
$ git checkout feature
$ git rebase -i master
12.
Interactive rebasing
pick 33d5b7aMessage for commit #1
pick 9480b3d Message for commit #2
pick 5c67e61 Message for commit #3
pick 33d5b7a Message for commit #1
fixup 9480b3d Message for commit #2
pick 5c67e61 Message for commit #3
Golden Rule ofRebasing
Learn when NOT to do rebasing:
Never use it on public branches.
15.
Golden Rule ofRebasing
Before you run git
rebase, always
ask yourself: Is
anyone else
looking at this
branch?
16.
Force-Pushing
# Be verycareful with this command!
$ git push --force
● Overwrite the remote master branch to
match the rebased one from your
repository.
● One of the only times to use force-pushing:
local cleanup on a private feature branch.
17.
Local Cleanup
● Makesure each commit in your feature is
focused and meaningful.
● Two options for the new base:
■ the feature’s parent branch (e.g. master)
■ an earlier commit in your feature
● For instance, we need to fix up the last few
commits.
18.
Local cleanup
$ gitcheckout feature
$ git rebase -i HEAD~3
To re-write the entire
feature, use:
$ git merge-base
feature master
Integrate a feature
Aftera feature has been approved by your
team:
● rebase the feature onto the tip of the
master
● then, use git merge to integrate the
feature into the main code base
Summary
$ git rebase
●clean, linear history free of unnecessary
merge commits
$ git merge
● preserve the complete history
● avoid the risk of re-writing public
commits
27.
2. Reset, Checkout,Revert
git reset, git checkout, and git revert
● some of the most useful tools in your Git
toolbox
● all let you undo some kind of changes
● the first two can be used to manipulate
either commits or individual files
Commit-level Operations
● Theparameters you pass to git reset and
git checkout determine their scope.
● When you don’t include a file path as a
parameter, they operate on whole commits.
● Note: git revert has no file-level
counterpart.
30.
Reset
● Move thetip of a branch to a different
commit.
● Usage: e.g. remove commits from the
current branch.
$ git checkout hotfix
$ git reset HEAD~2
Revert vs. Reset
$git revert
● undo committed changes
$ git reset HEAD
● undo uncommitted changes
43.
File-level Operations
git resetand git checkout
● accept an optional file path as a
parameter
● this dramatically alters their behaviour
● limit their operations to a single file
44.
Reset
● Update thestaged snapshot to match the
version from the specified commit.
$ git reset HEAD~2 foo.py
● Fetch the version of foo.py in the 2nd-to-
last commit and stage it for the next
commit.
Reset
Notes:
● --soft, --mixed,and --hard flags do not
have any effect on the file-level version
of git reset
● staged snapshot is always updated
● working directory is never updated
47.
Checkout
● Similar tousing git reset with a file path,
except it updates the working directory
instead of the stage.
$ git checkout HEAD~2 foo.py
● Make foo.py in the working directory
match the one from the 2nd-to-last
commit.
Summary
Command Scope Commonuse cases
git reset Commit-level Discard commits in a private branch or throw away
uncommited changes.
git reset File-level Unstage a file
git checkout Commit-level Switch between branches or inspect old snapshots
git checkout File-level Discard changes in the working directory
git revert Commit-level Undo commits in a public branch
git revert File-level N/A
50.
3. Advanced Gitlog
1. Formatting how each commit is displayed.
2. Filtering which commits are included in the
output.
=> go back into your project and find any
information that you could possibly need
51.
Formatting Log Output
Oneline
$git log --oneline
0e25143 Merge branch 'feature'
ad8621a Fix a bug in the feature
16b36c6 Add a new feature
23ad9ad Add the initial code base
52.
Formatting Log Output
Decorating
$git log --oneline --decorate
0e25143 (HEAD, master) Merge branch 'feature'
ad8621a (feature) Fix a bug in the feature
16b36c6 Add a new feature
23ad9ad (tag: v0.9) Add the initial code base
53.
Formatting Log Output
Diffs
$git log --stat
commit f2a238924e89ca1d4947662928218a06d39068c3
Author: John <john@example.com>
Date: Fri Jun 25 17:30:28 2014 -0500
Add a new feature
hello.py | 105 ++++++++++++++++++++++++-----------------
1 file changed, 67 insertion(+), 38 deletions(-)
54.
Formatting Log Output
Diffs
$git log -p
commit 16b36c697eb2d24302f89aa22d9170dfe609855b
Author: Mary <mary@example.com>
Date: Fri Jun 25 17:31:57 2014 -0500
Fix a bug in the feature
diff --git a/hello.py b/hello.py
index 18ca709..c673b40 100644
--- a/hello.py
+++ b/hello.py
@@ -13,14 +13,14 @@ B
-print("Hello, World!")
+print("Hello, Git!")
55.
Formatting Log Output
TheShortlog
$ git shortlog [-n]
Mary (2):
Fix a bug in the feature
Fix a serious security hole in our framework
John (3):
Add the initial code base
Add a new feature
Merge branch 'feature'
56.
Formatting Log Output
Graphs
$git log --graph --oneline --decorate
* 0e25143 (HEAD, master) Merge branch 'feature'
|
| * 16b36c6 Fix a bug in the new feature
| * 23ad9ad Start a new feature
* | ad8621a Fix a critical security issue
|/
* 400e4b7 Fix typos in the documentation
* 160e224 Add the initial code base
57.
Formatting Log Output
CustomFormatting
$ git log --pretty=format:"%cn committed %h on %cd"
John committed 400e4b7 on Fri Jun 24 12:30:04 2014 -0500
John committed 89ab2cf on Thu Jun 23 17:09:42 2014 -0500
Mary committed 180e223 on Wed Jun 22 17:21:19 2014 -0500
John committed f12ca28 on Wed Jun 22 13:50:31 2014 -0500
https://www.kernel.org/pub/software/scm/git/docs/git-log.
html#_pretty_formats
58.
Filtering Commit History
●By amount:
$ git log -3
● By date (--after/--since, --before/--until):
$ git log --after="2014-7-1"
$ git log --after="yesterday"
$ git log --after="2014-7-1" --before="2014-7-4"
Conceptual Overview
Git Hooks
●ordinary scripts that Git executes when
certain events occur in the repository
● local repository
● server-side repository
Conceptual Overview
Simple prepare-commit-msghook:
● Remove the .sample extension.
● Add
#!/bin/sh
echo "# Please include a useful commit message!" > $1
● Execute
chmod +x prepare-commit-msg
68.
Conceptual Overview
Scripting Languages
●built-in scripts: shell and PERL
● you can use any scripting language you
like as long as it can be run as an
executable
● shebang line #!/bin/sh
Summary
● The hookslet us plug in to the entire
development life cycle.
● Perform customizable actions at every
stage in the commit creation process, as
well as the git push process.
73.
5. Refs andReflog
Many ways of referring
to a commit
74.
Hashes
● The mostdirectly way to reference a
commit is via its SHA-1 hash.
$ git show 0c708f
● Resolve a branch, tag or another indirect
reference into the corresponding commit
hash.
$ git rev-parse master
75.
Refs
● A refis an indirect way of referring to a
commit (alias).
● Refs are stored in .git/refs directory:
.git/refs/
heads/
master
some-feature
remotes/
origin/
master
tags/
v0.9
76.
Specifying Refs
● Passinga ref to a Git command:
■ full name of the ref
■ short name of the ref
$ git show some-feature
● Git resolves some-feature to
refs/heads/some-feature
$ git show refs/heads/some-feature
77.
Packed refs
● Gitgarbage collection => compress refs into
a single file
● Force the compression:
$ git gc
● .git/packed-refs
00f54250cf4e549fdfcafe2cf9a2c90bc3800285 refs/heads/feature
0e25143693cfe9d5c2e83944bbaf6d3c4505eb17 refs/heads/master
bb883e4c91c870b5fed88fd36696e752fb6cf8e6 refs/tags/v0.9
78.
Special Refs
● HEAD- The currently checked-out
commit/branch.
■ HEAD can contain either a symbolic ref or a
commit hash.
● FETCH_HEAD - The most recently fetched
branch from a remote repo.
● ORIG_HEAD - A backup reference to HEAD
before drastic changes to it.
79.
Special Refs
● MERGE_HEAD- The commit(s) that you’re
merging into the current branch with git
merge.
● CHERRY_PICK_HEAD - The commit that you’
re cherry-picking.
80.
Refspecs
● A refspecmaps a branch in the local
repository to a branch in a remote
repository.
● Specified as [+]<src>:<dst>
● <src> - source branch
● <dst> - destination branch
● + - forcing the remote repository to perform
a non-fast-forward update
Refspecs
● Refspecs givesyou complete control over
how various Git commands transfer
branches between repositories.
● Rename/delete branches.
● Fetch/push to branches with different
names.
● Configure git push and git fetch to work
with only the branches that you want.
84.
Relative Refs
● Referto commits relative to another
commit:
$ git show HEAD~2
● ~ - Follow the first parent of a merge
commit.
● ^ - Specify the parent you want to follow.
$ git show HEAD^2
$ git show HEAD^2^1
Relative Refs
# Onlylist commits that are parent of the second parent
of a merge commit
$ git log HEAD^2
# Remove the last 3 commits from the current branch
$ git reset HEAD~3
# Interactively rebase the last 3 commits on the current
branch
$ git rebase -i HEAD~3
87.
The Reflog
● Git’ssafety net.
● Record almost every change you make in
your repository
$ git reflog
400e4b7 HEAD@{0}: checkout: moving from master to HEAD~2
0e25143 HEAD@{1}: commit (amend): Integrate some awesome
feature into `master`
00f5425 HEAD@{2}: commit (merge): Merge branch ';feature';
ad8621a HEAD@{3}: commit: Finish the feature
88.
The Reflog
● HEAD{<n>}- Reference commits stored in
the reflog.
● Revert to a state that would otherwise be
lost.
ad8621a HEAD@{0}: reset: moving to HEAD~3
298eb9f HEAD@{1}: commit: Some other commit
message
bbe9012 HEAD@{2}: commit: Continue the feature
9cb79fa HEAD@{3}: commit: Start a new feature
89.
The Reflog
● “Oups,I shouldn’t have done that!”
● “Don’t worry :) !”
$ git checkout HEAD@{1}
● Note: this puts you in a detached HEAD
state.
90.
Summary
● Git reflog- a way to reference commits
that are not available through any other
means.
● The point of all this was to be able to pick
out exactly the commit that you need in
any given development scenario.