The 10 Most Common Git Problems and How to Solve Them

Git can often be tricky, but it doesn’t have to mean a new repo every time a problem arises. That’s why I’ve compiled the 10 most common Git problems in one article, so you can navigate Git even better.

post-thumb

BY Ali Mohammad / ON Apr 17, 2024

1. How to move a feature branch to main if it was branched incorrectly?


incorrectly branched feature branch move a feature branch to main if it was branched incorrectly

Solution:

You can easily move your feature branch by typing the following in the terminal. Use -f to move your feature branch to a new starting point, which in this case is main.

git branch -f feature/issue-42/create-new-user-form-component main

2. How to solve merge conflicts in Git?


It is assumed that a merge conflict occurs in the file index.js. In the event of a merge conflict, you will get an error similar to the one below, where files have a merge conflict are shown:

Auto-merging index.js
CONFLICT (content): Merge conflict in index.js
Automatic merge failed; fix conflicts and then commit the result.

Solution:

To solve the conflict, open the file that has a conflict in a text editor like vim and look for the conflict markers:

vim index.js

Choose whether you only want your changes to remain, the already existing code to remain, or a middle ground between these that incorporates parts from both. Finish by deleting the conflict markers <<<<<<<, =======, >>>>>>>, and saving the file.

To ensure the fix remains, remember to commit. Add the file to the staging area and then commit:

git add index.js
git commit -m "fix: resolved merge-conflict by replacing the existing code."

The above is performed iteratively until all files with merge conflicts are fixed and committed.


3. How to split a feature branch into two separate branches in Git?


split feature branch conflict split a feature branch into two separate branches

Solution:

Create a new feature branch from main and checkout to it:

git checkout main
git checkout -b feature/issue-43/create-new-table-component

Next, you can git cherry-pick the commits from your original feature branch that should be merged into this newly created feature branch:

git cherry-pick <commit1> <commit2> <...> <...>

Your newly created branch feature/issue-43/create-new-table-component should now have all its relevant commits. The only thing left is to remove the same commits from the old (original) feature branch. Start by checking out to your old feature branch:

git checkout feature/issue-42/create-new-user-form-component

Next, you can use git rebase -i HEAD~x to remove commits, where x is the number of recent commits you want to overview. The Git command will provide you with an interactive interface where your commits are listed in sequence.

In the interface, replace pick with drop next to the commits that should be deleted, which were previously moved to the new feature/issue-43/create-new-table-component branch.

drop 23dv1u0 "your commit message"
drop 8dh49o4 "your commit message"

Finally, save and close the interface. Your features should now be in their respective branches. This can be verified with git log.


4. How to move a new commit from main to a new feature branch in Git?


flyt commit fra main til ny feature branch i git sådan flyttes commit fra main til ny feature branch i git

Solution:

From main, create a new feature branch by typing in the terminal:

git checkout main
git branch feature/issue-44/create-new-pagination-component

While still in main, you can use git rebase -i HEAD~x, which provides you with an interactive interface to remove the commits that should not be in the main branch, where x is the number of recent commits you want to overview. Replace pick with drop next to the commits that should be deleted:

git rebase -i HEAD~1
drop 23dv1u0 "your commit message"

Save your changes and close the interface to finish the rebase process. Verify that your main branch looks correct with git log.


5. How to add changes to a previous commit in Git?


git problem of a previous commit how to add changes to a previous commit

Solution:

Make your addition and finish by staging the updated files with git add <filename>. Then, link this addition to your previous commit using --amend:

git commit --amend

This brings up an interactive interface where you can update your commit message. It would be a good idea to do this since your commit now includes a new addition. Finish by saving and closing the interface.


6. How to split a large commit into multiple smaller commits in Git?

Solution:

Use interactive git rebase to break down your commit into several smaller commits. Find the commit that needs to be broken down and type the following in the terminal:

git rebase -i HEAD^

This brings up an interactive interface that you can navigate. Replace pick with edit so the commit can be modified. Save your change and close the interface to start the rebase process.

edit 99ad4c0 "din commit-besked"

Reset your commit with git reset HEAD~. You can then stage and commit parts of your previous commit iteratively:

git add <fil1> <fil2> ...
git commit -m "feat: første task done"

Finish with git rebase --continue to complete the process. It is a good idea to enter git log to verify that the breakdown of your old commit was done correctly.


7. How to remove a password pushed to Git?

Solution:

Use git rebase -i HEAD^ to modify your latest commit. In this case, the latest commit needs to be modified to delete the password. Replace pick with edit in your interactive interface.

edit 5h57d81 "feat: added password and implemented function"

Save your change and close the interface to start the rebase process. Begin by deleting the password in the relevant file, then stage this change, commit, and finish the rebase process:

git add <filnavn>
git commit --amend
git rebase --continue

The above steps have deleted the password on your local branch. For these changes to be reflected in the corresponding remote branch, type the following in the terminal:

git push --force

Make sure to communicate to the rest of your team that you are force pushing to remote, as this is a significant, but necessary, action.


8. How to move commits mistakenly made in a merged branch to a new feature branch in Git?

Solution:

Create a new feature branch from main:

git checkout -b feature/issue-45/create-new-searchbar-component main

While in the newly created feature branch, use git cherry-pick to pick the commits from the old, merged branch to merge them into the new feature branch.

git cherry-pick <commit1> <commit2> <...> <...>

Finish by pushing your feature branch, now including your commits, to remote:

git push -u origin feature/issue-45/create-new-searchbar-component

9. How to change the latest Git commit message?

Solution:

Use git rebase -i HEAD^ to modify your latest commit. In this case, you need to change your latest git commit message. Replace pick with reword in your interactive interface.

reword 8d57675 "your commit message"

Save and close the interface afterward to start the rebase process. You can now correct your misleading commit message to be comprehensive. Finish by saving your new commit message and closing the interface.


10. How to restore the previous commit in remote after a failed force-push in Git?

Solution:

Use git log to find the commit hash of the latest commit you want to return to. Then, create a separate recovery branch from this commit hash:

git checkout -b recovery-branch <commit-hash>

Finally, you can push this recovery branch up to remote.

git push -u origin recovery-branch

Conclusion

Git can be a powerful tool, but it comes with its own set of challenges. By understanding how to handle common issues like moving commits, resolving merge conflicts, and managing branches, you can greatly improve your workflow and productivity. We hope this guide has helped you navigate some of the most common Git problems you might encounter.

If you found this article useful, please share it with your fellow developers. For more tips and tutorials on Git and other development tools, be sure to check out our other blog posts and follow us on LinkedIn. Happy coding!

Our Blog | Follow us on LinkedIn


Share: