Git Cheatsheet

To save myself searching for common it commands

Git Ignores

gitignore.io: Quick was to generate gitignore files. Source code

Debug why a file is being ignored (Source):

git check-ignore --verbose <file>

Good General Advice on Git Mistakes

Fixing Divereged Branches

Also see Git Rebase versus Git Merge below

# Rebase - preferred for cleaner history
git merge --rebase
git push
# If failed, and pushing to a branch NO ONE ELSE HAS DOWN
git push --

Accidently Committed to Wrong Branch!

# In bad branch
git log # Record commit id of correct commit you want to save
# Now in new branch
git checkout <correct-branch>
git cherry-pick COMMIT_ID
# Fix the branch with bad commit
git checkout <wrong-branch>
git reset --hard HEAD^

Source: https://wizardzines.com/comics/oh-shit-wrong-branch/

Git FF only

git config --global pull.ff only

Good discussion: Why use should use git pull –ff-only

Git Ignore keeps tracking files!

This Stackoverflow answer explains it, but need to remove any previously tracked files now in .gitignore with the command below:

git rm --cached <file>

Remove file permanently from git history

  1. Add the file to .gitignore so it doens’t happen again.

  2. Then run the following:

    git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch <filetoremove>" HEAD
    git push --force
    

Source: https://h.daily-dev-tips.com/removing-a-env-file-from-git-history

Push an Empty Commit

Mostly useful to retrigger CI/CD without making a “junk” change:

git commit --allow-empty -m “Message”

Source: https://devdojo.com/kodewithchirag/learn-to-push-an-empty-commit

Git Rebase versus Git Merge

Reset master to origin/master

Say…accidentally commit a change to the master branch that I did not intent too…

git checkout -B master origin/master

Reset branch to a specific commit

git checkout <goodbranch>
git log   #record commit hash, will look something like 617d1bc1870caaf36b65d5062b6e091437a44c5b
git checkout <badbranch>
git pull --rebase
git reset --hard <commit_hash>
git push --force

Sync local with upstream

Recording this as my previous method (git checkout master; git pull upstream/master) resulted in extra commits I didn’t want…

Assume you already have an upstream configured

git fetch upstream
git checkout master
git merge upstream/master

Source: Github

Refuse to Merge Unrelated Histories

# Since I don't usually enable pull merged
git fetch origin
git merge origin master --allow-unrelated-histories

From https://www.educative.io/answers/the-fatal-refusing-to-merge-unrelated-histories-git-error

Submodule init and update

Initialize submodules with new repo

git submodule init && git submodule update

Initialize submodules with new repo (during clone)

git clone --recurse-submodules git://myawesomeproject...

Update submodules

cd <submodule dir>
git checkout master
git pull

More submodule details at https://git-scm.com/book/en/v2/Git-Tools-Submodules

Remove submodule

git submodule deinit -f path/to/submodule
rm -rf .git/modules/path/to/submodule
git rm -f path/to/submodule

Source: StackOverflow

Corrupted git objects

If you see an error like the following:

fatal: loose object cdd75ee0cbac88baa81b31d22b2dbe9ae5108526 (stored in .git/objects/cd/d75ee0cbac88baa81b31d22b2dbe9ae5108526) is corrupt

Two options (both assume you have a repo clones remotely)

  1. If you don’t have anything you need to commit, just reclone the repo

    git remote -v   # Record current remote
    cd ..
    rm -rf <remoteclone>
    git clone <remoteref>
    
  2. If you have changes you wish to commit. Note…this will cause you to lose your local commit history, but the files changes themselves will remain intact.

    tofixgit=$(pwd)
    remote=$(git remote -v | grep origin | awk ' { print $2; } ' | head -1) # Get current remote
    cd ..
    cp -R $tofixgit ${tofixgit}-bak
    git clone ${remote} ${tofixgit}-clean
    rm -rf $tofixgit/.git
    cp -r ${tofixgit}-clean/.git ${tofixgit}
    rm -rf ${tofixgit}-clean
    

    Once done, should be able to recommit any changes.

Advance Fixes

General Understanding

Steve Miller BY-NC 4.0 | Rendered by Hugo | Subscribe