Git is
a version-control system for tracking changes in computer files and coordinating
work on those files among multiple people. It is primarily used for source-code
management in software development, but it can be used to keep track of changes
in any set of files.
The
Git feature that really makes it stand apart from nearly every other SCM out
there is its branching model.Git allows and encourages you to have multiple
local branches that can be entirely independent of each other. The creation,
merging, and deletion of those lines of development takes seconds.
Staging
Area
Unlike
the other systems, Git has something called the "staging area" or
"index". This is an intermediate area where commits can be formatted
and reviewed before completing the commit.One thing that sets Git apart from
other tools is that it's possible to quickly stage some of your files and
commit them without committing all of the other modified files in your working
directory or having to list them on the command line during the commit.
Let's
start with the commands to clone existing projects or create new projects.
Initializing
a Git repository
Usage
: git init
git-init
- Creates an empty Git repository or reinitialize an existing one (creates a
hidden .git subdirectory).Run 'ls -a' to view any hidden files . Always make
sure to run this command when you are creating a new project.This command
creates an empty Git repository - basically a .git directory with
subdirectories for objects, refs/heads, refs/tags, and template files. An
initial HEAD file that references the HEAD of the master branch is also
created. Running git init in an existing repository is safe.
It will not overwrite things that are already there. The primary reason for
rerunning git init is to pick up newly added templates
Clone
a repository
Usage:
git clone <ssh/https repository url>
git-clone
- Clones a repository into a new directory . This command creates and checks
out an initial branch that is forked from the cloned repository’s currently
active branch. After the clone, a plain git fetch without arguments will update
all the remote-tracking branches, and a git pull without arguments will in
addition merge the remote master branch into the current master branch.
Now
that we have cloned an existing project or initialized a new project ,let's
take a look at the commands that help in adding and committing new files to the
repository.Below are some basic snapshotting commands.
1.add
2.status
3.diff
4.commit
5.reset
6.rm
7.mv
Adding
New files
Usage
: git add <filename>
git add .
(Adds all the modified files to the index)
git-add
- Adds file contents to the index .This command updates the index using the
current content found in the working tree, to prepare the content staged for
the next commit.The "index" holds a snapshot of the content of the
working tree, and it is this snapshot that is taken as the contents of the next
commit. Thus after making any changes to the working tree, and before running
the commit command, you must use the add command to add any new or modified
files to the index.
Status
of the working tree
Usage
: git status
The
git status command can be used to obtain a summary of which files have changes
that are staged for the next commit.Displays paths that have differences
between the index file and the current HEAD commit, paths that have differences
between the working tree and the index file.
Difference
between commits
Usage
:
1. git diff
<commitid#1> <commitid#2> (This is to view the changes between two
arbitrary <commit>.)
2. git diff --cached
[<commitid>] (This form is to view the changes you staged for the next
commit relative to the named <commit>. )
3. git-diff - Show changes
between commits, commit and working tree, etc Shows changes between the working
tree and the index or a tree, changes between the index and a tree, changes
between two trees, changes between two blob objects, or changes between two
files on disk.
Commit
files to the repo
Usage
: git commit -m “<commit message here>”
git-commit
- Records changes to the repository . Stores
the current contents of the index in a new commit along with a log message from
the user describing the changes.The content to be added can be specified in
several way:
1. By using git add
2. By using git rm (to
remove any files from the project )
3. Issue commit with
filename to save changes to that particular file and ignore the staged changes.
4. By using -a make the
commit command to automatically add changes to all files and rm files from the
index that have been removed from the working tree.
5. There is also an
interactive mode where git asks you the decision for each modified file in the
working tree.
Reset the working tree
Usage :
1. git reset
2. git reset [--soft |
--hard |--merge | --keep] [-q] [commitid]
git-reset - Reset current HEAD to the
specified state . The first usage resets the index entries but doesn’t affect
the working tree whereas the second usafe sets the current branch head (HEAD)
to <commitid>, optionally modifying index and working tree to match.
--soft
Does not touch the index file or the working
tree at all (but resets the head to <commitid>, just like all modes do).
This leaves all your changed files "Changes to be committed",
as git status would put it.
--mixed
Resets the index but not the working tree
(i.e., the changed files are preserved but not marked for commit) and reports
what has not been updated. This is the default action.
--hard
Resets the index and working tree. Any
changes to tracked files in the working tree since <commitid> are
discarded.
--merge
Resets the index and updates the files in the
working tree that are different between <commitid> and HEAD, but keeps
those which are different between the index and working tree (i.e. which have
changes which have not been added). If a file that is different between
<commitid> and the index has unstaged changes, reset is aborted.
In other words, --merge does something like
a git read-tree -u -m <commitid>, but carries forward
unmerged index entries.
--keep
Resets index entries and updates files in the
working tree that are different between <commitid> and HEAD. If a file
that is different between <commitid> and HEAD has local changes, reset is
aborted.
Remove files from working tree
Usage : git rm [-f] [-r]
[--cached] <filename>
git-rm - Remove files from the working tree
and from the index . Remove files from the index, or from the working tree and
the index.
-f
--force
Override the up-to-date check and remove the
file.
-r
Allow recursive removal when a leading
directory name is given.
--cached
Use this option to unstage and remove paths
only from the index. Working tree files, whether modified or not, will be left
alone.
Move or rename a file, a directory, or a
symlink
Usage :
1. git mv [-v] [-f] [-n]
[-k] <source> <destination>
2. git mv [-v] [-f] [-n]
[-k] <source> ... <destination directory>
In the first form, it renames <source>,
which must exist and be either a file, symlink or directory, to
<destination>. In the second form, the last argument has to be an
existing directory; the given sources will be moved into this directory.The
index is updated after successful completion, but the change must still be
committed.
No comments :
Post a Comment