Code Yarns β€πŸ‘¨β€πŸ’»
Tech Blog ❖ Personal Blog

My Git Cheatsheet

πŸ“… 2016-Jan-02 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cheatsheet, git ⬩ πŸ“š Archive

Git incantations that I need to lookup sometimes:

Revision information

$ git show 123456

This shows the revision’s hash, author, commit message and a full diff of all the files in the revision.

$ git show --name-only 123456
$ git show --name-status 123456
$ git show 123456
fatal: ambiguous argument '123456': unknown revision or path not in the working tree.

Above is an example of a revision that is not in the repo.

$ git show ":/foobar"

This chooses the most recent revision which has foobar in its commit message.

All other ways to refer to a revision are listed here in the documentation.

Working copy

$ git clone git@github.com:joe/foobar --branch branch123
$ git reset

This moves all the files in the index back into the working directory.

$ git reset HEAD file123

This moves the file out from the index back to the working directory.

$ git checkout -- file123
$ git reset --hard
$ git revert sha_hash_of_revision_123
$ git revert sha_hash_of_revision_700
$ git revert sha_hash_of_revision_99
$ git revert sha_hash_of_revision_1

Though you can revert revisions in any order, I like to revert in opposite order of history. That is, revert the latest revision first and so on.

$ git revert --no-commit sha_hash_of_revision_700
$ git revert --no-commit sha_hash_of_revision_99
$ git revert --no-commit sha_hash_of_revision_1
$ git commit

The --no-commit stages the reverted changes, but does not commit them. So, we follow them up with a commit.

$ git reset --hard HEAD~1
$ git reset --soft HEAD~1
$ git clean -fd

This does not touch files ignored by git (.gitignore).

$ git clean -fdx

This pretty much restores the repo directory to a pristine state with no extraneous files.

$ git checkout file123
$ mkdir empty_dir
$ touch empty_dir/.gitkeep
$ git add empty_dir/.gitkeep

Branches

$ git branch branch123
$ git branch branch123 revision_hash_678
$ git branch -m new_branch_name
$ git branch -m old_branch_name new_branch_name
$ git branch -d branch_name

Note that you cannot delete the current branch. Checkout to a different branch and then delete the branch you wanted to. This is because imagine chopping off the branch of a tree you are sitting on. You will fall down! 😁

$ git branch -vva --contains 01234
$ git diff branch1 branch2 -- path/to/file
$ git difftool branch1 branch2 -- path/to/file

Here the assumption is that branch2 has the updated changes, so is placed after branch1.

$ git diff to_branch from_branch | git apply --index -
$ git commit
$ git log --oneline master..branch1 | tail -1

Merge

$ git checkout current_branch
$ git merge other_branch
$ git fetch remote_123
$ git merge remote_123/remote_branch_678
$ git mergetool

Rebase

$ git checkout current_branch_123
$ git rebase another_branch_678
$ git fetch remote_123
$ git rebase remote_123/remote_branch_678

Remotes

$ git remote show remote_name
$ git remote rename old_remote_name new_remote_name
$ git checkout -b local_branch remote_name/remote_branch
$ git push remote_name remote_branch_name
$ git push remote_name local_branch_name:remote_branch_name
$ git push -u remote_name remote_branch_name
$ git push -u remote_name local_branch_name:remote_branch_name
$ git push remote_name --delete remote_branch
$ git checkout -t remote_name/remote_branch
$ git branch local_branch -u remote_name/remote_branch
$ git remote prune FOOBAR_REMOTE

Submodules

$ git submodule status --recursive
$ git fetch --all --recurse-submodules
$ git submodule update --recursive
$ git submodule update --init --recursive
$ git ls-tree 999792b foobar

Tags

Misc

$ git reflog
$ git log -S foobar
$ git log -L :funcname:/path/to/filename
$ git commit --amend
$ git blame -C -L 234,234 foobar.cpp
$ git diff --name-only old_rev new_rev
$ git diff --name-status old_rev new_rev
$ git archive -o out.tar HEAD
$ git archive -o out2.zip SOME_TAG
$ git archive -o out3.zip SOME_COMMIT_HASH
$ git archive -o foobar.zip SOME_REV $(git diff --name-only SOME_REV PREV_REV)

Β© 2023 Ashwin Nanjappa β€’ All writing under CC BY-SA license β€’ 🐘 Mastodon β€’ πŸ“§ Email