Skip to content

Instantly share code, notes, and snippets.

@ProcessEight
Last active April 29, 2021 14:30
Show Gist options
  • Save ProcessEight/1dbaf3016156201f9b2576f2986cc2ed to your computer and use it in GitHub Desktop.
Save ProcessEight/1dbaf3016156201f9b2576f2986cc2ed to your computer and use it in GitHub Desktop.
Git Cheatsheet: Common git commands

Git cheatsheet

Table of Contents

Created by gh-md-toc

Fixing common git problems

http://ohshitgit.com

Remove files from version control without deleting file:

git rm --cached foo.txt

Source: https://superuser.com/a/898168

Moving commits to a new branch

Unless there are other circumstances involved, this can be easily done by branching and rolling back[1]1 (You will only be "losing" commits from the master branch, but don't worry, you'll have those commits in newbranch!).

Note: Any changes not committed will be lost.

Starting from the branch which has the commits you want to move:

git branch newbranch      # Create a new branch, saving the desired commits
git reset --hard HEAD~3   # Move master back by 3 commits (GONE from master)
git checkout newbranch    # Go to the new branch that still has the desired commits

But do make sure how many commits to go back. Alternatively, you can instead of HEAD~3, simply provide the hash of the commit (or the reference like origin/master) you want to "revert back to" on the master (/current) branch, e.g:

git reset --hard a1b2c3d4

Setting a local branch to track a remote branch

git branch --set-upstream-to origin/dev dev

Comparing the differences between two branches

To show commits that exist in feature-branch but don't exist in dev:

git log feature-branch..dev

Setting defaults

# Ignore file case
project8@project8-aurora-r5:/var/www/vhosts/magento2.localhost.com$ git config --add core.ignorecase true
# Add an alias
project8@project8-aurora-r5:/var/www/vhosts/magento2.localhost.com$ git config --add alias.co checkout
project8@project8-aurora-r5:/var/www/vhosts/magento2.localhost.com$ git config --add alias.ci commit
project8@project8-aurora-r5:/var/www/vhosts/magento2.localhost.com$ git config --add alias.cm commit
project8@project8-aurora-r5:/var/www/vhosts/magento2.localhost.com$ git config --add alias.st status
project8@project8-aurora-r5:/var/www/vhosts/magento2.localhost.com$ git config --add alias.br branch
# Display current config options for this git repo
project8@project8-aurora-r5:/var/www/vhosts/magento2.localhost.com$ git config --list
user.name=ProjectEight
user.email=s.frost@frostnet.co.uk
core.filemode=true
core.bare=false
core.editor=nano
remote.origin.url=git@github.com:ProjectEight/Mage2Katas.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
...
# Set a config option in the global scope (will affect all git repos)
project8@project8-aurora-r5:/var/www/vhosts$ git config --global --add alias.br branch
project8@project8-aurora-r5:/var/www/vhosts$ git config -l
user.name=ProjectEight
user.email=s.frost@frostnet.co.uk
alias.br=branch

Cleaning unversioned files

# Dry run: Shows what would be deleted
$ git clean -n -d <path>

# Actually deletes files (no going back!)
$ git clean -d -f <path>

Show which local branches track which remote branches

# Lists all configured remotes
$ git remote show
origin
# Lists all local/remote branches for remote 'origin'
$ git remote show origin 
* remote origin
  Fetch URL: git@github.com:ProjectEight/magerun.git
  Push  URL: git@github.com:ProjectEight/magerun.git
  HEAD branch: master
  Remote branches:
    admin-login-check                         tracked
    dev-aoe-template-hints                    tracked
    dev-email-template-preview                tracked
    dev-env-set                               tracked
    dev-install-generate                      new (next fetch will store in remotes/origin)
    dev-module-create-better                  tracked
    dev-toggle-payment-method                 tracked
    dev-toggle-shipping-method                tracked
    eav-attribute-add                         tracked
    eav-attribute-add-refactor-frontend-input tracked
    eav-attribute-update                      tracked
    master                                    tracked
  Local refs configured for 'git push':
    admin-login-check                         pushes to admin-login-check                         (up to date)
    dev-aoe-template-hints                    pushes to dev-aoe-template-hints                    (up to date)
    dev-email-template-preview                pushes to dev-email-template-preview                (up to date)
    dev-env-set                               pushes to dev-env-set                               (up to date)
    dev-module-create-better                  pushes to dev-module-create-better                  (up to date)
    dev-toggle-payment-method                 pushes to dev-toggle-payment-method                 (up to date)
    dev-toggle-shipping-method                pushes to dev-toggle-shipping-method                (up to date)
    eav-attribute-add                         pushes to eav-attribute-add                         (up to date)
    eav-attribute-add-refactor-frontend-input pushes to eav-attribute-add-refactor-frontend-input (up to date)
    eav-attribute-update                      pushes to eav-attribute-update                      (up to date)
    master                                    pushes to master                                    (up to date)

Compare two branches

To view commits that are present in test but not in dev

git log dev..test
# List of commits from git log follows...

Create a tag

git tag <tag_name>

Push tags to remote

Tags are not automatically pushed with commits.

git push origin <remote_branch> --all

Checkout a tag

# git checkout tags/<tag_name> -b <tag_name>

# e.g. Checkout tag 1.13.0 as a new branch
git checkout tags/1.13.0 -b 1.13.0

Delete a local tag

# git tag -d <tag_name>

# e.g. Delete tag '1.0.0' locally
git tag -d 1.0.0

Delete a remote tag

# git push origin :<tag_name>

# e.g. Delete tag '1.0.0' from the remote repository
git push origin :1.0.0

Check which commits have been pushed to a remote

This will list all remote branches which contain the given commit SHA.

git branch -r --contains <commit_sha>

Generate an archive containing all files and folders which were changed in a given commit

git archive -o patch.zip a9359f9 $(git diff --name-only a9359f9^..a9359f9)

Source: https://stackoverflow.com/a/20686113

Search all commits in all branches for a string

Let's say you're looking for a string title the magic string in one of your many git branches in your repo. You can use:

git grep "the magic string" `git show-ref --heads`

If the string exists in your repo, anywhere, you'll get something like this:

***c5cd1d8:path/to/file.js:let a = "the magic string"

Once you have that data, it's as easy as finding the branch!

git branch --contains ***c5cd1d8

You might want to check out the options on the git log command as well, as it can be used to search across all history rather than just all current branches. For example to search all commit messages for the string "foo bar baz":

git log --grep="foo bar baz"

If you want to search file contents, you can use the "pickaxe". Not sure how you do multiple words, but for the single word "foo":

git log -Sfoo

This will search all commits for the word "foo" being added or removed.

Source: https://dev.to/dougblackjr/find-a-string-in-a-massive-git-repo-22h9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment