You are on page 1of 8

Terminology

Version ---> The term version is normally used to refer to the external reference number allocated
by a human to the controlled files, or their output (e.g., in the case of source code).

It indicates change that is made to file which is under git

Working copy ---> Your copy of the file that you actually make your changes in.
Check-in ---> To save your working copy of the file into the repository. Referred to as Commit in
some version control systems.

Check-out ---> To take a copy of a file from the repository into your working folder (sandbox).
Typically you will be checking-out the latest revision, but you can also access all earlier revisions.
Depending on the VCS the file can be flagged as simply checked-out, or checked-out exclusively (locked).

Commit --->To save your working copy of the file into the repository. Referred to as Check-in in some
version control systems.

Conflict ---> The situation when two people try to commit changes that affect the same region of the
same file. These must be resolved, either using a Merge tool, or manually.

Repository ---> The master storage of all the files under version (or source) control, also known as
the database.

Revision ---> A committed change in the history of a file or set of files. This is the numerical
reference supplied by the VCS to track the different editions it is holding of the file.

Trunk ----> Trunk would be the main body of development, originating from the start of the project
until the present.

Branch ---> Branch will be a copy of code derived from a certain point in the trunk that is used for
applying major changes to the code while preserving the integrity of the code in the trunk.

If the major changes work according to plan, they are usually merged back into the trunk.

Tag or Label ---> It's name given to set of versions of files


Tag will be a point in time on the trunk or a branch that you wish to preserve. The two main reasons for
preservation would be that either this is a major release of the software, whether alpha, beta, RC or
RTM, or this is the most stable point of the software before major revisions on the trunk were applied.

Merging occurs when you want to bring the work back together.

Since the work has been done independently on different branches, some code files (and other changes)
may have occurred in some of the same files.
A merge conflict may occur as it might not be clear how to put together a file from different branches
correctly - this normally requires manual intervention and is what most coders refer to when talking
about "merge hell".

General workflow is as follows:

1) You clone the Git repository as a working copy.


2) You modify the working copy by adding/editing files.
3) If necessary, you also update the working copy by taking other developer's changes.
4) You review the changes before commit.
5) You commit changes. If everything is fine, then you push the changes to the repository.

After committing, if you realize something is wrong, then you correct the last commit and
push the changes to the repository.
Shown below is the pictorial representation of the work-flow.
GIT Hands-on (Use git bash for Practice)
mkdir GIT_TEMP ----> create empty directory

cd GIT_TEMP -------> go to dir

git init ------> initialize current directory

touch test1 ----> create empty file

git add test1 --------> add to staging area

git status ------> check current status of files and dires. It shows whether files under git, staging area or
workspace

git add * -----> add all (move all files and dirs to staging area from workspace)

git add . -----> add all (move all files and dirs to staging area from workspace)

git config

Sets configuration values for your user name, email

git config --global user.name "My Name"

git config --global user.email "user@domain.com"

git commit -m "comments " ---> commit (move all files and dirs to git from staging area)

git commit -F filename ---->force commit and you can commit only required files

edit test1 and add content "My first class and I am learning GIT"

git add test1

git commit -m "edited test1"

git log ------> shows commits.Recent commits are listed in begining

git log test1 ----> shows commit on only test1 (version history of test1). Recent commits are listed in
begining

git log -n ----> where n is numeric. It shows recent n commits

git log -2 ---> shows recent 2 commits (last commit and second last commit)

git checkout COMMIT_ID ----> checksout mentioned commit (shows content of previous version of a
file)

git checkout master ---> switch to lastest version of a file.

git diff file1 file2 ----> shows diff between two files
git rm file ---> remve file from git repo

==============================================================================

===================run below branch commands ==========================

git branch ----> lists branches. it shows "master" by default. Branch is mainly used for parallel
development

git branch branch_name -----> creates branch from master branch

git checkout branch_name ------> switch to branch_name and do ls to see files and dirs

vi test3 and add content "testing branching"

git add test3 and git commit -m "commit test3"

git checkout master -------> switch to master branch

git merge branch_name ------> merge branch_name to master branch. Merge will continue even there
are merge conflicts. Need to resolve if merging conflicts. Branch to be get merged should be checkout

git checkout -b branch_name2 ----> creates branch and switch branch to branch_name2

git branch -d branch_name2 ---> delete specified branch from git repo

===========================================================================

===========================GIT revert and reset==============================

git revert commit_id ---> undo commited changes. it will not remove from history also (it will not
remove from git log)

git revert HEAD ---> undo last commit.

git reset

three types of git reset

1) git soft

2) git mixed

3) git hard

git reset --soft commit --->will undo committed changes and moves files committed in that paticular
commit to staging area. It will remove from history (commit_ID of mentioned commit removed from git
log). git status will show files as staged
git reset --mixed commit ---> will undo committed changes and moves files to workspace from staging
area. It will remove from history (commit_ID of mentioned commit removed from git log). git status will
show files in the working directory.

It's default reset. git reset cmmit (default reset)

git reset --hard commit --> will undo committed changes and removes files from workspace, staging
area and git repo. It will remove from history (commit_ID of mentioned commit removed from git log).
you can permanently lose changes this way, you should always run git status before doing a hard reset
to make sure your working directory is clean.

git stash -->Temporarily saves changes that you don’t want to commit immediately. You can apply the
changes later.

steps: git add file

git stash ---> will save changes temporarily

git stash list --> show you list of stashed

git stash pop --> brings stashed changes back.

git clean -f --> remove untracked files

git clean -fdx

-f - force

-d - directories too

===========================================================================

==============================Remote ===================================

mkdir CENTRAL_GIT ---> create empty dir

cd CENTRAL_GIT

pwd

output:/home.User_name/CENTRAL_GIT

git init --bare ---> convert current dir to bare repo. No git commands will work under this. You can only
clone, push and pull from bare repo.

cd ..

mkdir workspace1

cd workspace1
git clone /home/Abc /CENTRAL_GIT ---> copy repo from remote to local workspace

cd CENTRAL_GIT

touch file1

git add file1

git commit -m "added file1 to push"

git push /home/User_name/CENTRAL_GIT ----> push local changes from workspace to remote repo.

cd ../..

mkdir workspace1

cd workspace1

git clone /home/User_name/CENTRAL_GIT

cd CENTRAL_GIT

touch ws2

git add ws2 and git commit -m "added ws2"

git push /home/User_name/CENTRAL_GIT

go to /home/User_name/workspace1/CENTRAL_GIT

cd /home/User_name/workspace1/CENTRAL_GIT

git pull /home/User_name/CENTRAL_GIT ---> brings changes from remte repo and merges
automatically to local repo in workspace

git fetch /home/User_name/CENTRAL_GIT ---> brings changes from remote repo and stores in
separate repo. you can review changes and merge to your local repo using merge command if it's
required.

===========================================================================

Major GIT commands:


git config -----> Sets configuration values for your user name, email, gpg key, preferred diff algorithm,
file formats and more.

Example: git config --global user.name "My Name"

git config --global user.email "user@domain.com"


git init ---> Initializes a git repository – creates the initial ‘.git’ directory in a new or in an existing
project. Example: cd /home/user/my_new_git_folder/ git init

git clone ----> Makes a Git repository copy from a remote source. Also adds the original location as a
remote so you can fetch from it again and push to it if you have permissions.

Example: git clone git@github.com:user/test.git

git add --->Adds files changes in your working directory to your index. Example: git add .

git rm -----> Removes files from your index and your working directory so they will not be tracked.
Example: git rm filename

git commit ----> Takes all of the changes written in the index, creates a new commit object pointing to
it and sets the branch to point to that new commit.

Examples: git commit -m ‘committing added changes’

git commit -a -m ‘committing all changes, equals to git add and git commit’

git status ---> Shows you the status of files in the index versus the working directory.

It will list out files that are untracked (only in your working directory), modified (tracked but not
yet updated in your index), and staged (added to your index and ready for committing).

Example: git status # On branch master # # Initial commit # # Untracked files: # (use "git add
<file>..." to include in what will be committed) # # README nothing added to commit

but untracked files present (use "git add" to track)

git branch ----> Lists existing branches, including remote branches if ‘-a’ is provided. Creates a new
branch if a branch name is provided. Example: git branch -a * master remotes/origin/master

git checkout -----> Checks out a different branch – switches branches by updating the index, working
tree, and HEAD to reflect the chosen branch. Example: git checkout newbranch

git merge ----> Merges one or more branches into your current branch and automatically creates a
new commit if there are no conflicts. Example: git merge newbranchversion

git reset ---> Resets your index and working directory to the state of your last commit. Example:

git reset --hard HEAD

git stash ---> Temporarily saves changes that you don’t want to commit immediately. You can apply
the changes later.

Example: git stash Saved working directory and index state "WIP on master: 84f241e first
commit" HEAD is now at 84f241e first commit (To restore them type "git stash apply")

git tag ----> Tags a specific commit with a simple, human readable handle that never moves. Example:
git tag -a v1.0 -m 'this is version 1.0 tag'
git fetch --->Fetches all the objects from the remote repository that are not present in the local one.
Example: git fetch origin

git pull ----> Fetches the files from the remote repository and merges it with your local one. This
command is equal to the git fetch and the git merge sequence. Example: git pull origin

git push ---> Pushes all the modified local objects to the remote repository and advances its branches.
Example: git push origin master

git remote ----> Shows all the remote versions of your repository. Example: git remote origin

git log ---> Shows a listing of commits on a branch including the corresponding details.

Example: git log commit 84f241e8a0d768fb37ff7ad40e294b61a99a0abe Author: User


<user@domain.com> Date: Mon May 3 09:24:05 2010 +0300 first commit

git show ---> Shows information about a git object.

Example: git show commit 84f241e8a0d768fb37ff7ad40e294b61a99a0abe Author: User


<user@domain.com> Date: Mon May 3 09:24:05 2010 +0300 first commit diff -- git a/README
b/README new file mode 100644 index 0000000..e69de29

git ls-tree ---> Shows a tree object, including the mode and the name of each item and the SHA-1 value
of the blob or the tree that it points to.

Example: git ls-tree master^{tree} 100644 blob


e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 README

git cat-file --->Used to view the type of an object through the SHA-1 value. Example: git cat-file -t
e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 blob

git grep --->Lets you search through your trees of content for words and phrases. Example: git grep
"www.siteground.com" -- *.php

git diff ---> Generates patch files or statistics of differences between paths or files in your git
repository, or your index or your working directory. Example: git diff

===========================================================================

You might also like