Professional Documents
Culture Documents
org/
1.3 Branches
All commits belong to a branch
o An independent line of development of the project
By default, there is a single branch and it is called master
1.4 Creating Branches
How do you maintain a stable project at the same time that you are working on it
o Create a separate branch
The master branch does not now about feature branch
1.6 Merging
When a branch is ready to become part of the master branch, it can be merged into the master branch.
A merge combines the work of separate branches.
Before the merge, the master branch has no knowledge of the featureX branch. After the merge, there's
a single master branch with the latest commit, including the code that implements featureX.
Before you merge content into the master branch, how do you know that your changes are good? A pull
request is a request to merge your branch into another branch. This request is usually made by
developer of the branch when the feature, bug fix or other change is complete.
During a pull request team members can discuss, review, and approve your changes. You can also
require that automated test pass before the merge is allowed to happen. This helps ensure that the
changes introduced by the merge don't cause problems for the customer. If the pull request is accepted,
your version of the project is merged and becomes the latest commit on the master branch. You can feel
good about the quality because the changes were reviewed and automated tests have passed.
2 Git Overview
2.1 What is Version Control
We can think of version control from the perspectives of content, teams, and agility.
Version control manages a collection of changing and improving files which we can call a project. The
complete history of the project is tracked and available at any time.
Version control also supports teams working on that collection of files. Teams work in many different
ways, and a good version control system will support many of their workflows or ways of getting work
done. Version control helps support collaboration on the project and improves quality through
facilitating team communication and reviews.
Finally, version control enables agility, the ability to adapt quickly and constructively to a changing
environment. It does this by managing small changes to a project and by allowing
. Each user has a local copy of the complete history of the project, which is known as a
repository.
Even though repositories are distributed among team members there's also a single remote
repository that is designated as the source of truth or official state of the project. This repository
is usually hosted in a data center or in the cloud.
Because each user has a local copy of the complete history of the project, they can continue to
work while offline.
Content is synchronized between repositories by pulling content from or pushing content to a
remote repository.
A Git repository contains a series of snapshots of the project over time which are known as commits.
Each commit contains all of the directories and files of the project at the time the snapshot was taken.
You can go back and view the project at earlier points by viewing the older commits.
2.5 Review
Version control enables teams to manage a collection of files in an agile qay
Git is a distributed version control system
o Each user has a local copy of a Git repository
A repository contains the project history as commits
o A commit is a snapshot of the entire project
You have the choice of working with Git using a command line and/or a graphical interface
3 Git Locations
We know that a commit is a single snapshot of the project. The working tree is the location on your
computer that contains the directories and files of a single commit. This is where you can view and edit
the files of the project, preparing them for the next commit. We will learn later that you check out a
commit to place its directories and files into the working tree.
The staging area contains a list of files that will be in the next commit.
The first three locations that we discussed, the working tree, staging area, and local repository are
commonly all contained in a single directory on your local computer. This is called the project directory.
The project directory contains the working tree. The working tree contains the directories and files of a
single commit or snapshot of your project. You can view and edit these files to prepare them for the
next commit.
The project directory also contains a hidden directory named.git. This is where the staging area and local
repository are located. Notice that if you delete your project directory, you are also deleting your local
repository and staging area, because they are in the.git directory.
3.5 Remote Repository
We have seen that the working tree, staging area, and local repository are all located in the project
directory on your local machine. The remote repository is usually located in a data center or in the
cloud. The remote repository contains the commits of the project, and is often considered the source of
truth or official state of the project. When the local and remote repositories are synchronized, they
contain exactly the same commits.
You can clone any repository into Sourcetree if you have the URL
git remote add can be used to add a remote repository using a command line
7.3 Push commits to remote repository
All commits belong to a branch
git push can be used to push commits to a remote repository using a command line
git remote –verbose Displays information about remote repositories associated with the local repository
The arrows point at a commit’s parent(s). The entire graph contains a project's history. Each node in Git
represents a commit. The arrows point at a commit's parents.
In this example, you can see that commit D has two parents, commit B and commit C. The commits and
the relationship between them is what forms the project's history.
This graph contains a branch because commit A has two children, commit B and commit C.
git log command with the graph option shows commit diagram
9 Git IDs
1. Commit object - A simple text file that contains information such as the commit user
information, commit message, a reference to the commits parent or parents and a reference to
the root tree of the project. That information is all that Git needs to rebuild the full contents of a
commit.
2. An annotated tag - A reference to a specific commit.
3. Tree - An object that contains a list of the file names and directories inside of a directory.
4. A blob - An object that stores the content of a file that is being managed by Git.
A typical Git user may only interact with commit objects and tags, letting Git worry about the details
related to trees and blobs. Git keeps these objects internally in something called the object store but
you typically don't directly interact with the object store.
Git ID
Git ID is the name of a Git object. All of the objects stored by Git are named with a 40-character
hexadecimal string. These strings are commonly known as Git IDs, but they are also known as object IDs,
SHA-1, hashes and checksums.
git hash-object <file> - Can be used to create SHA-1 values from file contents.
10 Git References
Commits can be associated with references. A reference is a user-friendly name that points to either
a commit's SHA-1 value or to another reference. If a reference points to another reference, it's called
a symbolic reference.
Here, the latest commit has two references associated with it, HEAD and master. Master is the default
name of the main branch in the repository. A branch is an independent path for a set of commits. If we
never create a branch in a repository, then by default, all commits are part of the master branch.
Branch label point s to the most recent commit in the branch. That commit is commonly
called the tip of the branch. Branch labels are implemented as references.
HEAD is a reference to the current commit. It usually points to the branch label of the current branch.
Because it only points to the current commit, and there can only be one current commit, there is only
one HEAD reference in your local repository.
In this example, we have three commits on the master branch. The master branch label reference points
to the most recent commit. The HEAD reference points to the master branch label.
A tag is a reference attached to a specific commit. It acts as a user-friendly label for the commit. These
are different from branch labels, which move along with each new commit. Tags are used to mark
important commits, like version 1.0.
Normally, SourceTree will create what is called an annotated tag. An annotated tag is one of the four
types of Git objects. However, if you check the lightweight tag box, you will create a simpler tag with less
features than an annotated tag. That is one of the reasons why this option is not recommended.
11 Git Checkout
In Git terms, a "checkout" is the act of switching between different versions of a target entity. The git
checkout command operates upon three distinct entities: files, commits, and branches. In addition
to the definition of "checkout" the phrase "checking out" is commonly used to imply the act of executing
the git checkout command.
11.1 Checking out branches
The git checkout command lets you navigate between the branches created
by git branch. Checking out a branch updates the files in the working directory
to match the version stored in that branch, and it tells Git to record all new
commits on that branch. Think of it as a way to select which line of
development you’re working on.
12 Merging vs. Rebasing
12.1 The Merge Option
The easiest option is to merge the master branch into the feature branch using
something like the following:
On the other hand, this also means that the feature branch will have an
extraneous merge commit every time you need to incorporate upstream
changes. If master is very active, this can pollute your feature branch’s history
quite a bit. While it’s possible to mitigate this issue with advanced git
log options, it can make it hard for other developers to understand the history
of the project.
12.2 The Rebase Option
As an alternative to merging, you can rebase the feature branch
onto master branch using the following commands:
The major benefit of rebasing is that you get a much cleaner project history.
First, it eliminates the unnecessary merge commits required by git merge.
Second, as you can see in the above diagram, rebasing also results in a
perfectly linear project history—you can follow the tip of feature all the way
to the beginning of the project without any forks. This makes it easier to
navigate your project with commands like git log, git bisect, and gitk.
But, there are two trade-offs for this pristine commit history: safety and
traceability. If you don’t follow the Golden Rule of Rebasing, re-writing project
history can be potentially catastrophic for your collaboration workflow. And,
less importantly, rebasing loses the context provided by a merge commit—
you can’t see when upstream changes were incorporated into the feature.
12.3 Interactive Rebasing
Interactive rebasing gives you the opportunity to alter commits as they are
moved to the new branch. This is even more powerful than an automated
rebase, since it offers complete control over the branch’s commit history.
Typically, this is used to clean up a messy history before merging a feature
branch into master.
13 Git stash
14 git stash temporarily shelves (or stashes) changes you've made to your
working copy so you can work on something else, and then come back
and re-apply them later on. Stashing is handy if you need to quickly switch
context and work on something else, but you're mid-way through a code
change and aren't quite ready to commit.
$ git status
On branch master
Changes to be committed:
new file: style.css
Changes not staged for commit:
modified: index.html
$ git stash
Saved working directory and index state WIP on master: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepage
$ git status
On branch master
nothing to commit, working tree clean
At this point you're free to make changes, create new commits, switch
branches, and perform any other Git operations; then come back and re-apply
your stash when you're ready.
Note that the stash is local to your Git repository; stashes are not transferred
to the server when you push.
Now that you know the basics of stashing, there is one caveat with git
stash you need to be aware of: by default Git won'tstash changes made to
untracked or ignored files.
git diff— See all file changes locally. A file name can be appended
to show changes for only one file. 📂
git log — See all commit history. Can also be used for a file with git
log -p my_file. Enter q to exit. 📓
git blame my_file— See who changed what and when in my_file. 👉
git reflog — Show a log of changes to the local repository’s HEAD.
Good for finding lost work. 🔎
If you are just dealing with your own local commits that haven’t
been merged into collaborative remote work, you can use any of
these commands.
If you are working collaboratively and need to neutralize a commit
in the remote branch, git revert is your tool.
git reset --hard HEAD— Discard staged and unstaged changes since
the most recent commit.
Specify a different commit instead of HEAD to discard changes
since that commit. --hard specifies that both the staged and
unstaged changes are discarded.
Make sure you don’t discard a commit from a remote branch that
your collaborators are depending upon!
If nothing is staged, this command just allows you to edit the most
recent commit message. Only use this command if the commit has
not been integrated into the remote master branch!
git push my_remote --tags —Send all local tags to the remote repo.
Good for versioning changes.
15.4 Change Default Editor