Error Searching and Handling in Git
Last Updated :
26 Sep, 2021
With the rise of DevOps technologies, it has become unavoidable for any IT professional to work on many data sets at the same time, and the data is always changing. It’s also crucial to keep track of every update to the data and be ready to undo or reverse any unwanted changes if necessary.
Using Git to version data allows being more adventurous in the creation of projects. If a mistake is made, it is known that git will always allow undoing and/or revert that version of the project to the state it was in before the mistake was made. Before forwarding the data to the next level, each Git process layer is designed to allow the data changes to be reviewed, changed, and/or rectified. As a result, the following are the blunders that occur and are addressed in this article:
1. Remove files and folders from the Index: The default functionality of the ‘git add’ command is used to add all files and directories to the Index when adding and/or changing files. There may be a requirement to un-stage or change specific files one final time before committing them.

Errors Caused due to removal of files and folders
Syntax-
git reset <filename/dirname>

2. Forgot some changes in the last commit: If the user forgot to make some changes and previously committed the snapshot and the user doesn’t want to commit again to point out the error.
Syntax-
git commit –amend

Output 2. Output from amend command.
3. Edit the most recent message that was committed: It is possible to make changes to the most recent committed message without having to create a new one. Below is the sample command where an alias ‘hist’ is created to list the committed logs.
Syntax-
git config –global alias.hist ‘log –pretty=format:”%C(yellow)%h%Geeks%ad | %C(green)%s%Geeks%C(blue)%d%for Geeks %C(red)[%an]” –graph –decorate –date=short’x

Fixing the most recent message

Output 3. Output from config command.
4. Personal data has been committed to a local repository: Sometimes it may happen that the photo saved from the phone’s camera goes to the commit folder and gets pushed. Now it is required to delete certain files from the working directory but keep others in the local repository.
Syntax-
git reset –mixed HEAD~
git reset –mixed <commit-id>

Output 4. Output from local command.
5. Changes made locally should be ignored: Let’s take an example of an updated and staged ‘README’ file. Then a second edit is done to the same file. But it was not a required change.

Syntax-
git checkout — <filename> –local changes in a file
git checkout — <dirname> –local changes in all the files in the directoryÂÂ

Output 5. Output from checkout command.
6. Replace the previous commit with a new one: The ‘–soft’ option just removes committed files from the local repository while they remain staged in the Index, allowing one to re-commit them after a review. Let’s say there is a need to delete the SHA-1 of the snapshot from the local repo is commit-id. <Head -n> where n is the number of commits before the HEAD commit.
Syntax-
git reset –soft HEAD~1

Output 6. Output from reset soft head command.
7. Committed the wrong data: Sometimes it happens that the user commits the wrong data to the repository. Let’s see how to reset the project to the state before committing the wrong data.
Syntax-
git reset –hard HEAD~n –reset the project to ‘n’ commits before the latest committed snapshot
git reset –hard <commit-id> –reset the project to given commit id snapshot

Output 7. Output from reset hard command.
8. Getting back to the old project state: The fastest and the cleanest way to go back to the old project state is using the reset command in git along with the –hard flag so that everything comes back to its previous state.
Syntax-
git reset –hard <branch-number-here>

Output 8. Output from hard command.
9. Recovering a Deleted Local Branch: Sometimes, by the virtue of a small mistake, some important local branch might get deleted before it was pushed. In that case, the below command (checking out) will certainly help to recover that lost file.
Syntax-
git checkout -b <branch> <sha-keypair>

Output 9. Output from SHA command.
10. Fixing the wrong name given to a branch: Sometimes the branch that was just renamed had some typo like Geeks for Geeks became forGeeks, or maybe the user does not like the branch name enough and wants to rename it using a command line. The below command can be used to achieve the same:
Syntax- Targeting Old Name
git checkout <old_branch_name>
Syntax- Fixing and renaming now
git branch -m <new__branch_name>

Output 10. Output from branch command.
11. Re-arranging the history log: It might be really great if some commits are pooled together and then concatenate the commits into one big commit and rearrange them further. To achieve that, git provides an easy way out.
Syntax-
git rebase –interactive <some_random_autogen_id> <your_order_here>

Output 11. Output from interactive command.
And that’s it, just keep placing the commit order in the place and the commits will keep arranging.
Note: The above method works on local commits, not the one which is pushed.
12. Mistakes with GitHooks: Git hooks are a fantastic method to check the code when the changes are committed. They are simple scripts that live in the.git/hooks directory of the local repository. Depending on the name of the hook, these scripts are done at certain moments during the local process. While working on a big collaborative project, sometimes one may encounter some errors. Let’s see how to avoid that. One could simply just redirect the output to stderr, which will alert and notify the user on the email as well as the desktop if some error creeps in, even better if the commit you are about to push could cause some error, it will alert you for that as-well.
Including this in your project is quite simple, just do:
# Performing some random task here
perform 1>&2
# Redirecting the output to stderr.
FORBIDDEN=’todo: fix this’
# A simple method to return what you need to do if you encounter this.
git diff –cached –name-only | \
xargs grep –with-filename -i -n “$FORBIDDEN” && echo “COMMIT REJECTED
Found ‘$FORBIDDEN’ references. Kindly alter the changes or remove them
before committing ” && exit 1
exit
# The program quits
No output as the githook executed successfully.
And just like that you took care of that possible githook mistake, and can continue on your project as earlier! All these were the most frequent errors which you can expect to happen in a day-to-day Git Experience Scenario. Handling them gets a breeze when you know the perfect syntax for it!
Similar Reads
How To Resolve "not something we can merge" error in Git?
The "not something we can merge" error in Git usually indicates that Git is unable to identify a valid branch, commit, or reference to merge. This can occur due to various reasons, such as typos in branch names, non-existent references, or issues with the repository's state. This article will guide
3 min read
Saving a File in Git
Git allows you to track changes, collaborate with others, and manage codebase efficiently. One of the fundamental tasks when working with Git is saving files, a process that involves several steps, including adding files to the staging area and committing them to the repository. In this article, we
2 min read
What is Improper Error Handling?
Error handling is a mechanism used to resolve/handle errors that arise during the execution of a program. Error handling deals with these events to avoid the program or system crashing; exceptions would disrupt the normal flow of an application without this process. Even if an application is complet
3 min read
Handling Git Pull Without Specifying a Warning
When using Git, for sure, at some point, you must be pulling in changes from a remote repository into your local repository. During such an operation, the local code base is kept updated with changes from other developers. But this operation is critical, and because of that, this kind of operation t
3 min read
Ignoring Files and Folders in Git
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific ap
11 min read
Merge Conflicts and How to handle Them in Git
Merge conflicts are a common challenge developers face when working with Git. Understanding what they are and how to resolve them effectively is important for smooth collaboration in any project. Understanding how to handle merge conflicts is important for maintaining a smooth workflow. This article
5 min read
Searching and Accessing a File Using JGit
JGit is an open-source library written in Java that can implement the Git version control system. It allows developers to interact with the JGit repositories programmatically within Java applications. This article will demonstrate how to search for a specific file in the Git repository and access it
3 min read
How to Fix "git ignore" Not Working Error?
The .gitignore file in Git, allows you to specify files and directories that should be ignored by Git. However, there are some cases where .gitignore might not work as expected, causing frustration among developers. This article will guide you through the common reasons why .gitignore might not be w
4 min read
How to abort merge in Git ?
In collaborative coding environments, Git's merge feature is invaluable for integrating changes from different branches. However, there are times when a merge operation encounters conflicts or isn't proceeding as expected, necessitating an abort. This article explores how to effectively abort a merg
3 min read
Using Refs And Reflogs In Git
Git, a popular version control system, helps developers track changes in their codebase. Understanding how to use refs and reflogs in Git can significantly enhance your ability to manage and troubleshoot your repositories. This article will learn about these concepts, providing you with the knowledg
3 min read