CodeBlog.xyz

20 common Git problems and how to solve them

September 8, 2023 | by Meir Achildiev

Git problems and how to solve them

1. Merge Conflicts

Problem: Conflicting changes in the same file on different branches.

Solution: Manually edit the conflicted files to resolve differences, then add and commit them.

# Edit the files to resolve conflicts
git add [conflicted-file]
git commit -m "Resolved merge conflict"

2. Accidentally Committed to the main Branch

Problem: You made changes and committed them to the main branch instead of a feature branch.

Solution: Create a new branch and move the commits there, then reset the main branch.

git branch feature-branch
git reset --hard origin/main
git checkout feature-branch

3. Committing Large Files

Problem: You’ve accidentally committed a large file, making the repo hard to clone.

Solution: Remove the file from history using filter-branch or filter-repo.

git filter-branch --tree-filter 'rm -f path/to/large-file' HEAD
git push origin main --force

4. Detached HEAD State

Problem: You’ve ended up in a detached HEAD state.

Solution: Create a new branch to save your changes and then switch back to a known branch.

git branch temp-branch
git checkout main

5. Undoing the Last Commit

Problem: You need to undo the last commit.

Solution: Use git reset or git revert depending on whether you want to keep the changes in the working directory.

# Keep changes in the working directory
git reset HEAD~

# Create a new commit that undoes the last one
git revert HEAD

6. Forgotten to Add a File to a Commit

Problem: You committed but forgot to add a file.

Solution: Amend the previous commit to include the forgotten file.

git add forgotten-file.txt
git commit --amend --no-edit

7. Cannot Push Changes

Problem: You cannot push because someone else pushed changes before you.

Solution: Pull the latest changes and then push your changes again.

git pull --rebase
git push origin main

8. Lost Commits

Problem: You’ve lost commits due to a hard reset or other operation.

Solution: Use git reflog to find the commit hash and then reset to it.

git reflog
git reset 'HEAD@{n}'  # replace n with the position from reflog

9. Ignored Files Keep Showing Up

Problem: Files that should be ignored keep appearing in git status.

Solution: Make sure to add the file patterns to .gitignore and then remove the tracked files.

# Add patterns to .gitignore
git rm --cached -r .
git add .
git commit -m "Fixed .gitignore"

10. Mistyped Commit Author

Problem: You committed with the wrong user name and email.

Solution: Use an interactive rebase to amend the author information.

git rebase -i HEAD~n  # replace n with the number of commits to go back
# Change 'pick' to 'edit' for commits to amend
git commit --amend --author "New Author <email@example.com>"
git rebase --continue

11. Accidentally Deleted a Branch

Problem: You’ve accidentally deleted a branch that you need to restore.

Solution: Use git reflog to find the commit hash, then create a new branch pointing to it.

git reflog
git checkout -b restored-branch [commit_hash]

12. Cloning Extremely Slow

Problem: Git clone is taking too much time for large repositories.

Solution: Use shallow cloning to fetch only the latest snapshot of the repository.

git clone --depth 1 [repo_url]

13. Squashing Commits

Problem: You want to squash multiple commits into one to clean up your history.

Solution: Use interactive rebase.

git rebase -i HEAD~[number_of_commits]
# Change 'pick' to 'squash' for commits to squash

14. Changing a Remote’s URL

Problem: You need to change the URL of a remote repository.

Solution: Use git remote set-url.

git remote set-url origin [new_url]

15. Tagging a Previous Commit

Problem: You forgot to tag a version and want to tag a past commit.

Solution: Use git tag.

git tag [tag_name] [commit_hash]

16. Unintentional Modifying of Submodule

Problem: A submodule was unintentionally updated, and it needs to be reverted.

Solution: Reset the submodule to the commit stored in the superproject.

cd [submodule_path]
git reset --hard [commit_hash]
cd ..
git add [submodule_path]
git commit -m "Revert submodule changes"

17. Discarding Local Changes

Problem: You want to discard local changes to a specific file.

Solution: Use git checkout.

git checkout -- [file_path]

18. Updating a Forked Repository

Problem: You have a forked repository and want to update it with changes from the original repo.

Solution: Add a remote for the original repo, fetch changes, and merge or rebase.

git remote add upstream [original_repo_url]
git fetch upstream
git merge upstream/main

19. Verifying a Tag

Problem: You want to verify that a tag is from a trusted source.

Solution: Use git tag -v [tag_name].

git tag -v [tag_name]

20. Discarding History

Problem: Your repository has too much old and irrelevant history that you’d like to get rid of.

Solution: Create a shallow clone, and then force push to the original repository. Be cautious, as this will remove history.

git clone --depth 1 [repo_url] new-folder
cd new-folder
git push origin +main

RELATED POSTS

View all

view all