9. What is a branch?
A branch represents an independent line of
development.
10. • git init := Create an empty Git repository or
reinitialize an existing one
• git clone := Clone a repository into a new
directory from remote Git server
• git checkout := Switch branches or restore
working tree files
11. • git add := Add file contents to the index (staging
area)
• git commit := Record changes to the repository
12. Directory
Git doesn’t track directories, it tracks files, so to
acheive this you need to track at least one file
13.
14. • working directory := A folder that contains an
initialized GIT repository
• Git directory := It’s contained in working directory
and it’s called “.git”. In the Git directory there are
files and folders that compose our repository
• staging area := The staging area is a file, generally
contained in your Git directory, that stores
information about what will go into your next
commit. It’s sometimes referred to as the “index”,
but it’s also common to refer to it as the staging
area.
15.
16. • git blame := Show what revision and author last
modified each line of a file
• git log := Show commit logs
• git status := Show the working tree status
17. • git pull := Fetch from and integrate with another
repository or a local branch (fetch + merge)
• git fetch := Download objects and refs from
another repository
• git push := Update remote refs along with
associated objects
18. • git archive := Create an archive of files from a
named tree
19. .gitignore (1)
• A gitignore file specifies intentionally untracked
files that Git should ignore.
• Files already tracked by Git are not affected.
• Each line in a gitignore file specifies a pattern.
• Three levels: $GIT_DIR, $HOME,
$WORKING_DIR
24. HEAD o head?
• A head is simply a reference to a commit object. Each
head has a name (branch name or tag name, etc). By
default, there is a head in every repository called
master. A repository can contain any number of heads.
At any given time, one head is selected as the “current
head”. This head is aliased to HEAD, always in
capitals.
• Note this difference: a “head” (lowercase) refers to any
one of the named heads in the repository;
“HEAD” (uppercase) refers exclusively to the currently
active head. This distinction is used frequently in Git
documentation.
25. Fast-forward Git merge (1)
Merging a branch is a pretty common operation
when using Git.
In some circumstances, Git by default will try to
merge a branch in a fast-forward mode.
26. Fast-forward Git merge (2)
Let us assume that I created
a feature branch named test
from the current master. After
working on this branch for a
while, I finally decided that I
am done and then I pushed it
to my own remote.
Meanwhile, nothing else
happened in the master
branch, it remained in the
same state right before I
branched off.
master
feature/test
3 commit
27. Fast-forward Git merge (3)
Once my branch is ready to be
integrated, we might use the
usual steps of git fetch followed
by git merge (git pull). Because
master has not been changed
since the commit (gray circle)
which serves as the base for
the said topic branch, Git will
perform the merge using fast-
forward. The whole series of the
commits will be linear. The
history will look like the
diagram on the right.
28. Fast-forward Git merge (4)
Another variant of the merge is
to use -no-ff option (no fast-
forward). In this case, the
history looks slightly different,
there is an additional commit
(dotted circle) emphasizing the
merge. This commit even has
the right message informing
us about the merged branch.
The default behaviour of Git is
to use fast-forwarding
whenever possible.
merge
29. Introducing Tracked branches
(Local and remote branches)
• A local branch is a branch that only you (the local user) can
see. It exists only on your local machine.
• If you're using Git collaboratively, you'll probably need to sync
your commits with other machines or locations. Each machine
or location is called a remote, in Git's terminology, and each
one may have one or more branches. Most often, you'll just
have one, named origin.
• A remote branch was once a local branch that was pushed to
origin. In other words now every user has access to it.
• A local tracking branch on the other hand is a branch that is a
local image of the remote branch (think of it as your copy of the
remote branch).
30. Tracked branches (1)
(Remote tracking branch)
• Each branch has the concept of what it is
tracking. As well as the branches that will be
affected by a fetch/pull/push, tracking says
which branch is upstream of which.
• Normally, branches checked out of a remote
repository are automatically set up as tracking
branches.
31. Tracked branches (2)
(Remote tracking branch)
• They’re used to link what you’re working on
locally compared to what’s on the remote.
• They will automatically know what remote
branch to get changes from when you use git
pull/fetch/push.
• Even better, git status will recognize him how
many commits you are in front of the remote
version of the branch.
32. Tagging (1)
• Git has the ability to tag specific points in history as
being important. Typically people use this functionality to
mark release points.
• Git uses two main types of tags: lightweight and
annotated.
• A lightweight tag is very much like a branch that doesn’t
(shouldn’t) change. It’s just a pointer to a specific commit.
• Annotated tags, however, are stored as full objects in the
Git database. They contain the tagger name, email, and
date and they have a tagging message.
33. Tagging (2)
• By default, the git push command doesn’t
transfer tags to remote servers.
• By default, SourceTree transfers tags to remote
servers (Look at push all tags checkbox on
push window).
34. git commit --amend (1)
The git commit --amend command is a convenient
way to fix up the most recent commit.
It lets you combine staged changes with the
previous commit instead of committing it as an
entirely new snapshot.
It can also be used to simply edit the previous
commit message without changing its snapshot.
35. git commit --amend (2)
But, amending
doesn’t just alter the
most recent commit. It
replaces it entirely. To
Git, it will look like a
brand new commit,
which is visualized
with an asterisk (*) in
the diagram.
*
Initial history
Amended history
36. git commit --amend (3)
Never amend commits that have been pushed to a
public repository.