GIT (Version Control System)
1. What is VCS? 2
1.1. Version Control System 2
1.2. Centralized VCS 2
1.3. Distributed VCS 2
2. Getting Started 2
2.1. Installing GIT 2
2.2. Setting up a Repository 2
2.2.1. Git init 2
2.2.1.1. Steps: 3
2.2.2. Git clone 3
2.2.3. Git config 3
2.3. Saving Changes 3
2.3.1. Git add 3
2.3.2. Git commit 4
2.4. Inspecting a Repository 4
2.4.1. Git status 4
2.4.2. Ignoring Files 4
2.4.3. Git log 4
2.5. Viewing Old Commits 5
2.5.1. Git checkout 5
2.6. Undoing Changes 6
2.6.1. Git revert 6
2.6.2. Git reset 6
2.6.3. Git clean 6
2.7. Rewriting History 6
2.7.1. Git commit --amend 6
2.7.2. Git rebase 7
3. Collaborating 7
3.1. Remote 7
3.2. Pull 7
3.3. Push 8
3.4. Fetch 8
3.5. Merge 8
3.6. Branch 8
4. Advanced Tips 9
4.1. Merging vs Rebasing 9
4.2. Reset, Checkout, and Revert 9
5. Reference 9
1
GIT (Version Control System)
1. What is VCS?
1.1. Version Control System
It is a software that helps software developers to work together and maintain a complete
history of their work. VCS are two types:-
1. Centralized VCS
2. Distributed VCS
1.2. Centralized VCS
Uses a central server to store all files and enables team collaboration. But the major drawback
of CVCS is its single point of failure, i.e., failure of the central server. Unfortunately, if the
central server goes down for an hour, then during that hour, no one can collaborate at all. And
even in a worst case, if the disk of the central server gets corrupted and proper backup has not
been taken, then you will lose the entire history of the project.
1.3. Distributed VCS
DVCS clients not only check out the latest snapshot of the directory but they also fully mirror
the repository. If the server goes down, then the repository from any client can be copied
back to the server to restore it.
2. Getting Started
2.1. Installing GIT
sudo apt update
sudo apt i nstall git
2.2. Setting up a Repository
2.2.1. Git init
The git init command creates a new Git repository. It can be used to convert an existing,
unversioned project to a Git repository or initialize a new empty repository. Most of the other
Git commands are not available outside of an initialized repository, so this is usually the first
command you’ll run in a new project.
2.2.1.1. Steps:
mkdir project_name
cd project_name
git init
2
GIT (Version Control System)
Git init (creates .git directory that contains all the Git-related information for your project.)
2.2.2. Git clone
The git clone copies an existing Git repository to local machine/server. The original
repository can be located on the local filesystem or on a remote machine, accessible via
HTTP or SSH.
git clone <repo_url_of_your_project>
2.2.3. Git config
This is used to configure your git like name and email.
1. Configure our email and name
git config --global user.name 'vijay'
git config --global user.email '
[email protected]'
git config --global color.ui 'auto'
git config --global alias.<alias_name> <git command>
git config --global --edit
2. Example:-
onfig -
git c -global core.editor vim
git c onfig - -global alias.co checkout
2.3. Saving Changes
Before saving our changes to git we need to add the files first into VCS.
2.3.1. Git add
The git add command adds a change in the working directory to the staging area. It tells Git
that you want to include updates to a particular file in the next commit. However, git add
doesn't really affect the repository in any significant way—changes are not actually recorded
until you run git commit.
git add <file>
Stage all changes in <file> for the next commit.
git add <directory>
Stage all changes in <directory> for the next commit.
2.3.2. Git commit
The git commit command commits the staged snapshot to the project history. Committed
snapshots can be thought of as “safe” versions of a project—Git will never change them
unless you explicitly ask it to.
3
GIT (Version Control System)
git commit
Commit the staged snapshot. This will launch a text editor prompting you for a commit
message. After you’ve entered a message, save the file and close the editor to create the
actual commit.
git commit -m "<message>"
Commit the staged snapshot, but instead of launching a text editor, use <message> as the
commit message.
git commit -a
Commit a snapshot of all changes in the working directory. This only includes modifications
to tracked files.
2.4. Inspecting a Repository
2.4.1. Git status
The git status command displays the state of the working directory and the staging area. It
lets you see which changes have been staged, which haven’t, and which files aren’t being
tracked by Git. Status output does not show you any information regarding the committed
project history. The git log command displays committed snapshots. It lets you list the project
history, filter it, and search for specific changes.
2.4.2. Ignoring Files
Git lets you completely ignore files by placing paths in a special file called .gitignore. Any
files that you'd like to ignore should be included on a separate line, and the * symbol can be
used as a wildcard.
2.4.3. Git log
The git log command displays committed snapshots. It lets you list the project history, filter
it, and search for specific changes.
git log
Display the entire commit history using the default formatting. If the output takes up more
than one screen, you can use Space to scroll and q to exit.
git log -n <limit>
Limit the number of commits by <limit>. For example, git log -n 3 will display only 3
commits.
git log --oneline
Condense each commit to a single line. This is useful for getting a high-level overview of the
4
GIT (Version Control System)
project history.
git log --stat
Along with the ordinary git log information, include which files were altered and the relative
number of lines that were added or deleted from each of them.
git log --author="<pattern>"
Search for commits by a particular author. The <pattern> argument can be a plain string or a
regular expression.
git log --grep="<pattern>"
Search for commits with a commit message that matches <pattern>, which can be a plain
string or a regular expression.
git log <since>..<until>
Show only commits that occur between <since> and <until>. Both arguments can be either a
commit ID, a branch name, HEAD, or any other kind of revision reference.
git log <file>
Only display commits that include the specified file. This is an easy way to see the history of
a particular file.
git log --graph --decorate --oneline
A few useful options to consider. The —graph flag that will draw a text based graph of the
commits on the left hand side of the commit messages. —decorate adds the names of
branches or tags of the commits that are shown. —oneline shows the commit information on
a single line making it easier to browse through commits at-a-glance.
2.5. Viewing Old Commits
2.5.1. Git checkout
git checkout
The git checkout command serves three distinct functions: checking out files, checking out
commits, and checking out branches. In this module, we’re only concerned with the first two
configurations.
Checking out a commit makes the entire working directory match that commit. This can be
used to view an old state of your project without altering your current state in any way.
Checking out a file lets you see an old version of that particular file, leaving the rest of your
working directory untouched.
5
GIT (Version Control System)
2.6. Undoing Changes
2.6.1. Git revert
The git revert command undoes a committed snapshot. But, instead of removing the commit
from the project history, it figures out how to undo the changes introduced by the commit and
appends a new commit with the resulting content.
This prevents Git from losing history, which is important for the integrity of your revision
history and for reliable collaboration. More Explanation
Commands:
git revert <commit_id>
git revert --continue
git revert --quit
git revert --abort
2.6.2. Git reset
If git revert is a “safe” way to undo changes, you can think of git reset as the dangerous
method.
When you undo with git reset(and the commits are no longer referenced by any ref or the
reflog), there is no way to retrieve the original copy—it is a permanent undo. More
Explanation
Commands
eset <file>
git r
git r eset --hard
git r eset <commit>
2.6.3. Git clean
The git clean command removes untracked files from your working directory. More
Explanation
Commands:
git clean -n
2.7. Rewriting History
2.7.1. Git commit --amend
git commit --amend
(The git commit --amend command is a convenient way to fix up the most recent commit. It
6
GIT (Version Control System)
can be used to simply edit the previous commit message without changing its snapshot.)
2.7.2. Git rebase
Will be Explained better with Merge
3. Collaborating
3.1. Remote
The git remote command lets you create, view, and delete connections to other repository.
git remote
List the remote connections you have to other repositories.
git remote -v
Same as the above command, but include the URL of each connection
git remote add <name> <url>
Create a new connection to a remote repository.
git remote rm <name>
Remove the connection to the remote repository called <name>
git remote rename <old-name> <new-name>
Rename a remote connection from <old-name> to <new-name>
3.2. Pull
Merging upstream changes into your local repository is a common task in Git-based
collaboration workflows
git pull <remote>
Fetch the specified remote copy of the current branch and immediately merge it into the local
copy. This is same as
git fetch <remote>
git merge origin/<current-branch>
git pull --rebase <remote>
Same as the above command, but instead of using git merge to integrate the remote branch
with the local one, use git rebase.
7
GIT (Version Control System)
3.3. Push
Pushing is how you transfer commits from your local repository to a remote repo. It’s the
counterpart to git fetch, but whereas fetching imports commits to local branches, pushing
exports commits to remote branches.
git push <remote> <branch>
Push the specified branch to <remote>, along with all f the necessary commits and internal
objects. This created a local branch in the destination repository. To prevent you from
overwriting commits, git won’t let you push when it results in a non-fast-forward merge in
the destination repository.
ush <remote> ---force
git p
git p ush <remote> --all
git p ush <remote> --tags
3.4. Fetch
The git fetch command imports commits from a remote repository into your local repo. The
resulting commits are stored as remote branches instead of the normal local branches that
we’ve been working with.
git fetch <remote>
Fetch all of the branches from the repository. This also download all of the required commits
and files from the other repository.
git fetch <remote> <branch>
3.5. Merge
3.6. Branch
git branch
List all of the branches in your repository. This is synonymous with git branch --list
git branch <branch>
Create a new branch called <branch>. This does not check out the new branch.
git branch -d <branch>
Delete the specified branch. This is s “safe” operation in that Git prevents you from deleting
the branch if it has unmerged changes.
git branch -D <branch>
8
GIT (Version Control System)
Force delete the specified branch, event if it has unmerged changes. This is the command to
use if you want to permanently throw away all of the commits associated with a particular
line of development.
git branch -m <branch>
Rename the current branch to <branch>
git branch -a
List all remote branches.
4. Advanced Tips
4.1. Merging vs Rebasing
4.2. Reset, Checkout, and Revert
5. Reference
1. https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-config
2. http://www.vogella.com/tutorials/Git/article.html
3. https://guides.github.com/activities/hello-world/
4. http://www.sitepoint.com/git-for-beginners/