Troubleshooting
Table of contents
- Committed to Wrong Branch
- Wrong Commit Message
- Wrong File Added
- Mistake Realized After Committing
- Merge Conflict
Committed to Wrong Branch
If you commit an update to the wrong branch:
git reset HEAD~ --soft
git stash
git checkout correctBranchName
git stash pop
git add . # or add individual files
git commit -m "message here"
Wrong Commit Message
If you need to change the message on your last commit:
git commit --amend -m "new commit message"
Wrong File Added
If you need to remove a file from Git without removing it from your file system:
git reset fileName
echo file >> .gitignore
Mistake Realized After Committing
If you need to make a change right after you make a commit:
- Make that change.
- Enter:
git add <file-name> git commit --amend --no-edit
Merge Conflict
Merge conflicts occur when Git is unable to resolve differences in code between two commits from two different users.
If a merge conflict occurs:
- Find the local repository that has the conflict:
cd repo-name
- Determine what files have a merge conflict:
git status
- Open a text editor, and go to the file with the conflict.
- Search for the conflict marker:
<<<<<<< HEAD
- Then you’ll see
bash=====
which divides the two conflicting changes. - This is all followed by:
>>>>>>>BRANCH-NAME
- Decide if you want to keep your changes or delete them.
- Delete the following markers:
<<<< ==== >>>>>
- Add and commit your changes:
git add <file-name> git commit -m “commit message”