When working with Git for version control, you might encounter a frustrating error: “git did not exit cleanly (exit code 128).” This error is a generic message indicating that Git encountered a problem while executing a command. Fortunately, resolving this issue is usually straightforward. In this article, we’ll explore some common causes and solutions to help you get Git working smoothly again.

I. Common Causes of the Error

The exit code 128 is a generic error code in Git, signaling that something went wrong during a command execution. Here are some common reasons for this error:

  1. Invalid SSH Key: If you’re using SSH to connect to a remote repository, an expired or improperly configured SSH key can trigger this error.
  2. Invalid Git Configuration: Issues with your Git configuration, such as an incorrect username or email address, might lead to this error.
  3. Git Lock File Issues: The .git/index.lock file might be corrupted or not properly removed, causing this error.

II. Solutions to Fix the Error

Solution 1: Configure Your Git Username and Email

Git requires a valid username and email address to perform certain operations. If these are not properly set, you might encounter the exit code 128 error. You can configure your Git username and email globally using the following commands:

1
2
git config --global user.email "your_email@example.com"
git config --global user.name "Your Name"

After configuring these settings, re-run the command that caused the error to see if the issue is resolved.

Solution 2: Re-add Your SSH Key

If you’re using SSH to connect to a remote repository, your SSH key might be the root of the problem. You can try regenerating your SSH key and adding it to your Git service account (such as GitHub, GitLab, or Bitbucket).

Here’s how you can generate a new SSH key:

  1. Open your terminal and run the following command to generate a new SSH key:
1
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  1. Follow the prompts. You can choose the default path to save the key or set a passphrase for added security.
  2. Add the public key to your Git service account. For example, for GitHub:
    • Go to the GitHub website and navigate to “Settings” > “SSH and GPG keys.”
    • Click “New SSH key,” paste the public key content into the “Key” field, and save it.

After completing these steps, try running the Git command again to see if the issue is resolved.

Solution 3: Remove the Git Lock File

The .git/index.lock file is used by Git to prevent multiple processes from modifying the repository simultaneously. If this file is corrupted or not properly removed, it can cause the exit code 128 error. You can manually delete this file to resolve the issue.

Navigate to your Git repository directory and delete the .git/index.lock file:

1
rm <path>/.git/index.lock

Be cautious when deleting files to ensure you don’t accidentally remove important data.

III. Final Thoughts

The Git exit code 128 error may seem confusing, but it is usually easy to resolve. The most common solution is to delete the .git/index.lock file, but sometimes you may need to check your SSH key or Git configuration. By following the troubleshooting steps above, you should be able to get Git working again in no time.