Git
Git
2. For help:-
a. Git help <command name> It display the manual of the command
b. Man git -<command name> it also does the same thing i.e. display the
command manual
c. Git <command> -h or –help It will give the refresher on the available
options for a Git command.
1. You can take a local directory that is currently not under version control, and turn it into a Git
repository, or
3. Git init this will convert the local directory in to a repository. Now directory
contain .git subdirectory. For this command to work, we need to be in that directory and
directory should not be already version controlled.
That creates a directory named libgit2, initializes a .git directory inside it, pulls down all the
data for that repository, and checks out a working copy of the latest version.
If you want to clone the repository into a directory named something other than libgit2, you
5. Git add <file name or file patterns or directory name> To stage the given files. The git add
command takes a path name for either a file or a directory; if it’s a directory, the command
adds all the files in that directory recursively.
6. Git commit To commit the staged file. It will open an editor where we can type
commit message
7. Git commit –m “commit message” it will commit the staged file with the commit message
given
8. Git commit –a this will commit all the modified files. i.e. this will skip staging area.
Adding the -a option to the git commit command makes Git automatically stage every file
that is already tracked before doing the commit, letting you skip the git add part:
There are .gitignore file which can contain file names or file pattern which we want to ignore during
staging.
9. Git status it will show you which files are in which state. Any file is in two state
untracked files or tracked files. Tracked file is further divided in to modified, staged and
unmodified files. This command output has three sections:-
a. Untracked files: this section contains files which are newly created
b. Changes not staged for commit: files which are modified but not staged
c. Changes to be committed:- files which are staged and ready to go into next commit
10. Git status –s or git status --short this is used to display the short status
There are two columns to the output - the left-hand column indicates the status of the staging area
and the right-hand column indicates the status of the working tree.
?? : - indicates it is a new file. It has not tracked yet.
MM means file is modified and added to the staging area and then again file is modified.
11. Git diff this command show you the exact lines which are modified in the files. i.e. it
shows not only the file but what we have modified in that file. This command show you the
difference between working directory and staging area.
12. Git diff --staged (or --cached) this command show you the difference between
what you are going to commit and what is in the last commit. i.e. difference between the
staging area and last snapshot.
13. Git rm <file names> this will stage the file removal, so next time if we
committing, It remove the file from the git as well as from the working directory.
14. Git rm --cached <file name> to keep the file in your working tree but remove it
from your staging area. In other words, you may want to keep the file on your hard drive but
not have Git track it anymore. This is particularly useful if you forgot to add something to
$ git status -s
M README
MM Rakefile
A lib/git.rb
M lib/simplegit.rb
?? LICENSE.txt
15. Git log this will show the history of commit. By default, with no arguments, git log
lists the commits made in that repository in reverse chronological order — that is, the most
recent commits show up first. As you can see, this command lists each commit with its SHA-1
checksum, the author’s name and email, the date written, and the commit message.
17. Git log --p or --patch it will show the diff output as well with git log output.
18. Git log --stat this option show in addition to git log output, list of files that were
changed in the commit, how many lines are added and removed in each file and a summary
info at the end for each commit.
19. Git log --pretty this will be used to format the log output. The option used for pretty
is --oneline, --short, --full, --fuller, format. The oneline option prints each commit on a single
line.
%H Commit hash
%T Tree hash
%P Parent hashes
Option Description
-p. Show the patch introduced with each commit
--stat Show statistics for files modified in each commit.
--shortstat Display only the changed/insertions/deletions line from the --stat command.
--name-only Show the list of files modified after the commit information.
--name-status Show the list of files affected with added/modified/deleted information as
well.
--abbrev- Show only the first few characters of the SHA-1 checksum instead of all 40.
commit
--relative-date Display the date in a relative format (for example, “2 weeks ago”) instead of
using the full date format.
--graph. Display an ASCII graph of the branch and merge history beside the log output
--pretty Show commits in an alternate format. Options include oneline, short, full,
Fuller, and format (where you specify your own format).
--oneline Shorthand for --pretty=oneline --abbrev-commit used together
Option Description
--since, --after Limit the commits to those made after the specified date.
--author Only show commits in which the author entry matches the specified
string.
--committer Only show commits in which the committer entry matches the
specified string.
--until, --before Limit the commits to those made before the specified date.
--grep Only show commits with a commit message containing the string
-S Only show commits adding or removing code matching the string
--no-merges To prevent the display of merge commits
20. Git commit --amend this will amend your last commit. It means you can modify
or stage some files and then do the commit with amend option, so that in the commit
history only this commit will be shown. You can also amend the commit message. What does
this mean is that it looks like previous commit was never happen and all the changes you
have done in the last commit and this commit will go into one commit only.