Git Troubleshooting


Force LF line endings instead of CR+LF

Although there tons of different opinions on stackoverflow, this is the one that works for me:

git config --global core.eol lf
git config --global core.autocrlf input

If you have an existing repo already checked out with the CRLF line endings, you can fix it easily:

git rm -rf --cached .
git reset --hard HEAD

Stage changes and new files

git add .   // Stage created/modified files and not those deleted
git add -u  // Stage deleted/modified files and not those created
git add -A  // Stage created/modified/deleted files

Commit changes

git commit -a -m "" // Stage changes to all tracked files

Fix last commit

Amending the commit message.

git commit --amend -m "New commit message"

Amending changed files.

git commit --amend -a

Pull before push

Sometimes a push failed because the remote branch contains commits which you do not have locally. You need to pull these in first. However doing this with a normal git pull command, it creates an extra merged branch commit. This is normal, but can get messy if it happens a lot. To prevent this, you need to rebase your commits behind the new commits made by others when pulling.

git pull --rebase

Push changes

Push commits in current branch.

git push

Push commits in all branches.

git push --all

Push [tag] to origin

git push origin [tag]

Forced merge

Force the merged branch as the new branch. This might prevent a lot of merge conflicts and force the release branch into the master branch.

git merge --no-ff -s recursive -Xtheirs [branch]

Handling merge conflicts

First you have to fix the merge conflicts. Use git status to check where the conflicts are. After that you can commit resolved conflicts.

git add -A
git commit -a -m "Merge branch '[branch-name]'"
git push

Cherry picking

Cherry pick a specific commit from another branch into the current branch.

git cherry-pick [commit]

Renaming tags

git tag [new] [old]
git tag -d [old]
git push origin :refs/tags/[old]
git push origin [new]

Deleting branches

Delete a remote branch.

git push origin :release/[x.x.x]

Delete a local branch.

git branch -d release/[x.x.x]

Stashing uncommitted changes

Stash all changes and get a clean working directory.

git stash
// .. Do some git stuff like checkout branch or update the repo
git stash pop

Clear the stash stack if you don’t need the changes after all.

git stash drop

Unstage files

Unstage files wwhich are not committed yet.

git reset HEAD
git reset HEAD [file]

Discard all changes

git checkout .

Resetting files

Reset files to the HEAD of the branch.

git reset --hard HEAD
git reset --hard HEAD [file]

Cleanup tracked files (after editing .gitignore)

git rm -r --cached .
git add .
git commit -am "Remove ignored files"

Trigger hooks and ci tests

Use an empty commit to trigger hooks and ci tests.

git commit --allow-empty -m "Trigger hooks"

Cleanup stale branches

# Show info for remote origin repo
$ git remote show origin

# Delete all stale tracking branches in remote repository
$ git remote prune origin

Push changes to PR

$ git push <remote> <local-branch>:<remote-branch>
$ git push hotfix/1:patch-1