Git: The most frequent errors and issues, as well as simple solutions

Git: The most frequent errors and issues, as well as simple solutions

Git: The most frequent errors and issues, as well as simple solutions

Git Nov 8, 2022

This list is for you if you want to learn more about the aspects of Git that you were hesitant to inquire about before. Here are the most common issues and solutions, both from the my personal experience and from sources all over the Internet.


Error in commit comment

If the commit has not yet been delivered to the server (push), you can update the message content for the most recent commit using the following command:

git commit --amend

How to undo the last commit?

You can use git reset like this:

git reset --hard HEAD~1


HEAD~1 means one commit before HEAD, i.e. to the current position. It is worth noting that this is a "nuclear" method that will undo all changes. if you need to preserve everything you've done but haven't yet committed it, use:

git reset --soft HEAD~1

Delete branch on server

git push origin --delete branchname

What is the difference between "git pull" and "git fetch"?

git pull is really git fetch immediately followed by git merge. git fetch downloads changes from the server and puts them in refs/remotes/. This has no bearing on local branches or ongoing adjustments. And git pull consolidates all of these changes into a local copy.


How to undo "git add" before commit?

You used git add filename and wish to remove the file. If the commit has not yet been made, the following will be useful:

git reset filename

How to resolve merge conflicts?

Use a program git mergetool that provides a user-friendly interface for conflict resolution.


Remove any non-Git-tracked local files and folders from your current copy.

Carefully! Make a backup before you do it.

git clean -f -d

Clone all branches from the server

You've probably already done this, and the branches are merely buried. The command to show them is as follows:

git branch -a


To view the selected branch, run git checkout origin/branchname. Alternatively, use git checkout -b branchname origin/branchname to establish a local branch that corresponds to a remote one.


Rename local branch

git branch -m oldname newname

Revert to any commit

As previously demonstrated, git reset can be utilised. This implies that you wish to return to your previous condition permanently, rather than only observe it (for this you need to do a checkout). The commit ID must match the one found in the output of the command git log.

git reset --hard commit_id


Once again, the command will undo all current changes, so make sure you really need it. Or use --soft instead of --hard .


Delete a submodule

Submodules are rarely utilised, but they do appear on occasion. Here's what you'll require:

git submodule deinit submodulename
git rm submodulename
git rm --cached submodulename
rm -rf .git/modules/submodulename

Overwrite local files during git pull

It will assist you once again. git reset:

git fetch --all
git reset --hard origin/master

How to add an empty directory to a repository?

No way! Because this feature is just not supported. I don't believe you require it. However, there is one catch. You can put the following text in the file .gitignore in the chosen directory.

# Ignore everything in this directory
*
# Except the .gitignore file itself
!.gitignore

Exporting sources is similar to "svn export"

Use  git archive for example like this:

git archive --format zip --output /path/to/file/file.zip master

Revert all changes except those already added to the planned commit

git checkout -- .

Create a new branch on the server from the current local branch

git config --global push.default current
git push -u

Recover deleted file

To begin, locate the most recent commit where the file is still present:

git rev-list -n 1 HEAD -- filename


Then restore this file:

git checkout found_commit^ -- filename

Revert one specific file to the state it was in some commit

Almost identical to the last example, although a bit simpler:

git checkout commit id filename

Make Git ignore file permission changes

git config core.fileMode false

When interacting with https, make Git remember your password.

Remember for 15 minutes:

git config --global credential.helper cache


You can also provide a longer time frame:

git config --global credential.helper "cache --timeout=XXXX"


Thanks!


Tags

Anurag Deep

Logical by Mind, Creative by Heart