Last modified: July 10, 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. Staging involves selecting which changes in your working directory you want to include in your next commit. The staging area, also known as the index, acts as an intermediary between your working directory and your repository (commit history).

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

Here's how to stage files:

I. Stage a Specific File:

To stage a specific file, use the git add command followed by the file name. This tells Git to include the changes made to this file in the next commit.

git add filename

Example: If you modified a file named index.html, you would stage it with:

git add index.html

II. Stage All Changed Files:

If you want to stage all the changes you made across multiple files, you can use the git add . command. The period . is a wildcard that tells Git to stage changes from the current directory and all its subdirectories.

git add .

Example: If you modified several files and you want to stage all of them at once, simply run:

git add .

Unstaging Files

If you've staged changes but haven't committed them, you can unstage them. This can be useful if you change your mind about what should be included in the next commit.

I. Unstage a Specific File (but Keep Changes):

To unstage a specific file but keep the changes in the working directory, use the git reset command followed by the file name. This removes the file from the staging area.

git reset filename

Example: If you previously staged index.html but now want to unstage it, run:

git reset index.html

II. Revert Everything to the State of the Last Commit:

If you want to discard all changes and revert everything in the working directory and staging area to match the last commit, use the git reset --hard command. This will remove all uncommitted changes, so use it with caution.

git reset --hard

Example: To discard all changes and revert to the last commit, run:

git reset --hard

Committing Files

Commits in Git are akin to saving the state of your project at a particular point in time. A commit captures the current state of your project and associates it with a message that describes the changes made since the last commit. Each commit is uniquely identified by a SHA-1 hash, and you can use the git checkout command to revert to a previous state of your project.

+----------+
|  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.
+----------+

Here's how to commit files:

I. Commit All Staged Files with a Message:

To commit the changes you have staged, use the git commit command with the -m option followed by a descriptive message. This message should succinctly describe what changes have been made.

git commit -m "Your descriptive commit message"

Example: If you added a new feature, you might write:

git commit -m "Added new feature to enhance user interface"

II. Push Your Commits to a Remote Repository (like GitHub):

After committing your changes locally, you often need to push them to a remote repository to share them with others or back them up. Use the git push command to accomplish this.

git push

Example: To push changes to the main branch of your remote repository:

git push origin main

III. Go Back to a Previous State Captured in a Specific Commit:

If you need to revert your project to a previous state, use the git checkout command followed by the commit ID. This command allows you to explore the project as it was at that specific commit.

git checkout commit_id

Example: If the commit ID is a1b2c3d4, you would use:

git checkout a1b2c3d4

Undoing Commits

Mistakes happen, and Git provides several ways to undo commits:

I. Undo the Last Commit but Keep All Files Staged:

If you want to undo your last commit but keep the changes staged, use the git reset --soft HEAD~ command. This is useful if you realize you need to modify your commit message or make additional changes before committing again.

git reset --soft HEAD~

II. Undo the Last Commit and Unstage the Changes:

To undo the last commit and unstage the changes (but keep them in the working directory), use the git reset HEAD~ command. This will make the changes appear as if they were never staged or committed.

git reset HEAD~

III. Erase All Changes Made in the Last Commit:

If you want to completely undo the last commit and discard all changes made, use the git reset --hard HEAD~ command. This is a more destructive action, so use it with caution.

git reset --hard HEAD~

Editing Commit Messages

Sometimes, after committing changes, you realize that your commit message could be clearer or more descriptive. Fortunately, Git allows you to amend your commit messages. Here’s how to do it:

Edit the Message of the Most Recent Commit

To change the commit message of the most recent commit, use the git commit --amend command followed by the new message. After amending the commit message, you need to force push the changes to the remote repository.

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

Example: If you want to update the most recent commit message to "Fixed bug in user authentication," you would run:

git commit --amend -m "Fixed bug in user authentication"
git push --force

Modify an Older Commit Message

If you need to change the message of an older commit, follow these steps:

I. Start an Interactive Rebase for the Last n Commits:

Begin by initiating an interactive rebase. Replace n with the number of commits you want to go back from the current head.

git rebase -i HEAD~n

Example: To modify the last 3 commits, you would run:

git rebase -i HEAD~3

II. Change "pick" to "reword" for Commits You'd Like to Modify:

In the interactive rebase interface, change pick to reword for the commits whose messages you want to edit.

III. Save and Close:

Save the changes and close the editor. Git will then prompt you to provide new commit messages for each commit marked with reword.

IV. Push the Changes:

After editing the commit messages, you need to force push the changes to the remote repository.

git push --force

Removing Commits

If you need to undo one or more commits, Git provides commands to help you do so safely:

I. Remove the Last N Commits:

To remove the last N commits, use the git reset command followed by the number of commits you want to go back. Then, push the changes to the remote repository.

git reset HEAD~N
git push origin +HEAD

Example: To remove the last 2 commits, you would run:

git reset HEAD~2
git push origin +HEAD

Cleaning Up Your Commit History

If you need to remove sensitive data or a large file from your commit history, use the git filter-branch command. This command rewrites the commit history, so use it with caution.

I. Remove a File from the Commit History:

To remove a specific file from the entire commit history, use the following command:

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

Example: To remove a file named secret.txt from the commit history, you would run:

git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch secret.txt' HEAD

Table of Contents

    Staging, Committing, and Undoing Actions
    1. Staging Files
    2. Unstaging Files
    3. Committing Files
    4. Undoing Commits
    5. Editing Commit Messages
      1. Edit the Message of the Most Recent Commit
      2. Modify an Older Commit Message
    6. Removing Commits
    7. Cleaning Up Your Commit History