Last modified: June 11, 2024

This article is written in: 🇺🇸

Staging, Committing, and Undoing Actions

At the core of Git are a few fundamental actions: staging changes, committing those changes, and, when necessary, undoing certain actions. These notes provide a clear overview of these basic operations and some common scenarios where they are used.

Staging files

To prepare changes for a commit, we "stage" them.

+--------------+    +--------------+     +-------------------+
|              |    |              |     |                   |
| Working      |    | Staging      |     | Repository        |
| Directory    |    | Area (Index) |     | (Commit History)  |
|              |    |              |     |                   |
|  (Changes)   | -> | (Changes to  | ->  | (Commits)         |
|              |    | be committed)|     |                   |
+--------------+    +--------------+     +-------------------+

Here's how:

git add filename

git add .

Unstaging files

If you've staged changes but haven't committed them, you can unstage them:

git reset filename

git reset --hard

Committing files

Commits are similar to saves in that they capture the current state of your project and associate it with a message. The commit message should describe the changes made since the last commit. You can use the git checkout command to go back to a previous state of your project that was captured in a commit.

+----------+
|  commit  |
|   hash   | A unique identifier (SHA-1 hash) for the commit.
+----------+
|  author  | The person who authored the commit.
|  date    | The date and time the commit was made.
+----------+
| message  | A brief description of the changes made in the commit.
+----------+
| changes  | The actual changes made to the files.
+----------+

git commit -m "Your descriptive commit message"

git push

git checkout commit_id

Undoing commits

Made a mistake? Here's how you can undo it:

git reset --soft HEAD~

git reset HEAD~

git reset --hard HEAD~

Editing commit messages

Your commit message could be better? Let's fix that:

git commit --amend -m "New, improved message"
git push --force

git rebase -i HEAD~n

  1. Change "pick" to "reword" for commits you'd like to modify.
  2. Save and close. For each commit, provide a new message.
  3. Push the changes:

git push --force

Removing commits

Deleted more than you intended? Here's how to go back:

git reset HEAD~N
git push origin +HEAD

Cleaning up your commit history

Have sensitive data or a large file in your history? Here's how to remove it:

git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch file_to_remove' HEAD

Note: Always be careful with commands that alter commit history, especially if collaborating with others. If you're unsure, consult with your team or check Git documentation.

Table of Contents

    Staging, Committing, and Undoing Actions
    1. Staging files
    2. Unstaging files
    3. Committing files
    4. Undoing commits
    5. Editing commit messages
    6. Removing commits
    7. Cleaning up your commit history