You are on page 1of 4

Viewing history:

git log <options>

--oneline: simplified view in one line

--stat: show which files were changed in each commit

--patch: show detailed changes in each file in each commit

--all: show all commits, very useful in detached mode.

--graph: show a simple graph. Very useful to check merging.

Filtering:

git log <options>

-<n>: n is the number of commit. Example: -3 show the last 3 commits.

--author=”name”: filtering based on author of commit.

--before=”11-26-2023” or –after=”11-26-2023”: show commits before or after a specific date.

--grep=”text”: show commits that have messages containing text. This is case sensitive.

-S”<change>”: show commit that has the specified change. Example: -S”OBJECTIVES” shows commits
with files containing the word OBJECTIVES.

hash1..hash2: filter by range.

git log <file name> or git log -- <file name>: show all commits that touch a specific file.

Formatting:

git log –pretty=format”<out put>”

we use % as placeholder to contains all the data we want to show.

%h: hash, %an: author name; %cd: commit date….

Aliases:

git config –global alias.lg=”<long command>”

->This long command can be executed by typing its alias: git lg


Viewing commit

git show hash or git show HEAD~n <options>

--name-only: only names of files in the commit

--name-status: name and status of files in the commit

HEAD~n:<path to file>: show final version of the file.

Checking out:

git checkout <hashid>

See the snapshot at a specific point in time.

HEAD will point to a specific commit in detached mode, not master.

In detached mode, we should not make commit. Because new commit will be added on top of the
commit HEAD is pointing to, and when we exit detached mode, HEAD moves back to master. Therefore,
the new commit is not reachable by any commit -> dead commit and will be garbage collected -> we lost
that commit.

Bisecting:

Finding bug. Steps:

git bisect start

Assuming the first commit is good and last is bad -> we are in detached mode, and HEAD points to the
commit in the middle of good and bad. Then we run automated test in first half: no issue -> issue in 2 nd
half, and vice versa. Repeat until end.

git bisect reset.


Showing contributors:

git shortlog <options>

-n: number of commits made by author, -s: suppress message, -e: sow email

Viewing history of file:

git log <option> <filename> : --stat, --patch

Restore deleted file:

See all commits attached to that file and check out at the commit right before the commit to delete that
file.

git checkout hashid <filename> -> added file back to staging area.

Can make another commit to restore it.

Blaming:

git blame <option> <filename>

-e: email

-L 1,3: line 1-3

Tagging:

bookmark a specific commit:

git tag <tag name> <hash id>

git tag: show all tag

git tag –d <tag name>: delete tag

2 types of tag:

light weight tag: a pointer to a particular commit.

annotated tag: is a complete object with tagger name, email… -> git tag –a v1.0 –m <message>

You might also like