0% found this document useful (0 votes)
104 views5 pages

Git

This document summarizes common Git commands for configuring, initializing, cloning repositories, tracking changes, viewing history and amending commits. It describes commands for listing configurations, getting help, initializing and cloning repositories, adding, committing and removing files, viewing diffs and status, formatting and filtering the log, and amending the last commit. Key commands include git config, git init, git clone, git add, git commit, git rm, git diff, git log and git commit --amend.

Uploaded by

Saurabh Kothari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views5 pages

Git

This document summarizes common Git commands for configuring, initializing, cloning repositories, tracking changes, viewing history and amending commits. It describes commands for listing configurations, getting help, initializing and cloning repositories, adding, committing and removing files, viewing diffs and status, formatting and filtering the log, and amending the last commit. Key commands include git config, git init, git clone, git add, git commit, git rm, git diff, git log and git commit --amend.

Uploaded by

Saurabh Kothari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Git Command:-

1. To list the configuration:-


a. Git config --list it list all the configurations
b. Git config <key> it list the value of the given key

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.

You typically obtain a Git repository in one of two ways:

1. You can take a local directory that is currently not under version control, and turn it into a Git

repository, or

2. You can clone an existing Git repository from elsewhere.

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.

4. Git clone <repository url> To clone an existing repository. For ex:-

git clone https://github.com/libgit2/libgit2

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

git clone https://github.com/libgit2/libgit2 mylibgit

can specify the new directory name as an additional argument:

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.

A means file is new and added to the staging area

M means file is modified and added to the staging area

M means file is modified but not added to the staging area

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

your .gitignore file and accidentally staged it

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.

16. Git log –n it will show the last n commit entries

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.

Format option has following options:-

Option Description of Output

%H Commit hash

%h Abbreviated commit hash

%T Tree hash

%t Abbreviated tree hash

%P Parent hashes

%p Abbreviated parent hashes

%an Author name

%ae Author email

%ad Author date (format respects the --date=option)

%ar Author date, relative

%cn Committer name

%ce Committer email

%cd Committer date

%cr Committer date, relative

$ git log --pretty=oneline


ca82a6dff817ec66f44342007202690a93763949 changed the version number
085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test
a11bef06a3f659402fe7563abf99ad00de2209e6 first commit

$ git log --pretty=format:"%h - %an, %ar : %s"


ca82a6d - Scott Chacon, 6 years ago : changed the version number
085bb3b - Scott Chacon, 6 years ago : removed unnecessary test
a11bef0 - Scott Chacon, 6 years ago : first commit
%s Subject (commit message)

Common options to git log

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

Options to limit the output of git log -

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.

You might also like