Git - Commit

About

A commit 1) in git is an object 2) that stores the information about:

  • who saved the file system snapshots,
  • when they were saved,
  • why they were saved.
  • the commiter
  • the author
  • the parent commit

It's added in commit log (or log) behind its parent commit.

For each commit, a new snapshots (tree) of the project directory is preserved.

Git Commit Tree Data Model

Property

Identifier

SHA

Commit identifier are the hash object calculated on the following inputs:

  • the SHA of the all files and directories
  • the SHA of all ascendant commit
  • the author name/email/timestamp
  • the committer name/email/timestamp

Name

To reference a commit by name and not by SHA, named called ref can be used.

HEAD is a special ref that refers to the current branch (ie last commit of the current branch)

Message

The commot message is given with the -m option.

git commit -m "My Commit"

Date

The date of a commit is the author date.

There is also a committer date.

See

Cat file

git cat-file -p fdf4fc3
# where fdf4fc3 is the first characters of the commit hash
# for the last commit
git cat-file -p HEAD
tree d8329fc1cc938780ffdd9f94e0d364e0ea74f579
parent xxxx
author Foo <[email protected]> 1243040974 -0700
committer Bar <[email protected]> 1243040974 -0700

First commit

where:

Last commit

Just for the last commit

git cat-file -p HEAD

Tree

The commit tree is the tree of the commit. It's an image of the file system (ie file and directory)

Get the hash of the commit tree

  • short hand notation that can be used in any commdn
commitHash^{tree}
# dos: commitHash^^{tree}

For example:

  • to print the hash:
git rev-parse commitHash^{tree}
  • the list the tree
git ls-tree commitHash^{tree}

List the files in a commit

git show --pretty="" --name-only commitHash
git ls-tree $(git rev-parse commitHash^{tree})

File in a commit

git rev-parse commitHash:path/to/file
  • Get the content
git show commitHash:path/to/file

Commit viewer : gitk

gitk the commit viewer

gitk file
gitk --follow file # for rename

Management

Creation

git commit

The commit-tree 3)is a low level command that permits to creates a commit from a tree

echo 'First commit Message' | git commit-tree d8329f
# d8329f is the tree hash

Visualization

To visualize the type of commit, you can use emoji. See a list of emoji by commit type at Emojis Commit

List

Git - Log (Commit History)

git log --format=fuller
git log --pretty=oneline

Undo

Soft

Undo the last commit without deleting the changed files - Reset

# --soft is the default
git reset --soft HEAD~

Hard

If you accidentally commit on master, it's not hard to fix things up. Assuming you've just made an errant commit on master:

  • “Backs up” your commit, creating a topic branch
git branch myNewTopicBranch
 
  • Reset your master branch to the same state as upstream/master
git reset --hard upstream/master

Modify

the Last Commit

git commit --amend args

Example:

  • last message
git commit --amend -m "another commit message"
  • the author's date
git commit --amend --date='Wed Feb 16 14:00 2037 +0100' -C HEAD

Delete the last local commit

  • The last commit if not pushed
git reset HEAD~

If you want to your working directory back to the status of upstream, see working directory purge

Diff

Command line

  • commits that branch-X has but master doesn't
git log master..branch-X

Options:

  • –oneline
  • –stat will show the files

DiffTool

Git - difftool

git difftool -y origin/master..origin/develop

Checkout

git checkout commit_hash
git checkout 56a4e5c08

You are then in a detached head state.

or

git checkout -b branchName commitHash

Documentation / Reference





Discover More
Github Burnout
Burnout

Signs you need a break: loosing interesting in things you’re usually passionate and bring you joy short temper and easy to provoke taking everything personally constant overthinking emotionally...
Git

is a content-addressable file system used to track directory tree content (as defined by its creator Linux Torvald) It's not a version control...
Git - Author

The author is the person who originally wrote the code or the patch. The author date is also referred to as the commit date. The default author in git is in order of precedence: the global user...
Git - Committer

The committer is the person who committed the code or applied the patch on behalf of the original author. It's generally a project maintainer or core member. The default Committer is the OS user. ...
Git - Database

Git - Database The git core database is a key store value where a key value entry is known as an object. (All data in Git are objects) The database is mostly composed: * of tree of object *...
Git - Detached HEAD

What means a DETACHED HEAD in git
Git - File

File management in Git (blob and not directory) blob object on the local file system or not (ie in the git file system database) See and short status A file (or blob) identifier is the...
Commit History Forked
Git - Forked commit management

This is a page a local fork (ie a local branch). remote fork To incorporate the new commits into your feature branch, you have two options: merging or rebasing. Forked commit history
Git - Getting Started

An article that takes the basic steps in order to set up a Git repository When you want to set up a repository, you face generally two options: you want to create a fresh install from a Git repository...
Git - Head (Head Branch | Branch Head)

The head is the last commit of a branch. while the HEAD (in uppercase) is the head of the current branch (active branch). headHEADcurrent branch page In the example below, the branch head of: ...



Share this page:
Follow us:
Task Runner