- Git cheatsheet
- Fixing common git problems
- Moving commits to a new branch
- Setting a local branch to track a remote branch
- Comparing the differences between two branches
- Setting defaults
- Cleaning unversioned files
- Show which local branches track which remote branches
- Compare two branches
- Create a tag
- Push tags to remote
- Checkout a tag
- Delete a local tag
- Delete a remote tag
- Check which commits have been pushed to a remote
- Generate an archive containing all files and folders which were changed in a given commit
Created by gh-md-toc
git rm --cached foo.txt
Source: https://superuser.com/a/898168
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
git branch --set-upstream-to origin/dev dev
To show commits that exist in feature-branch
but don't exist in dev
:
git log feature-branch..dev
# 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
[email protected]
core.filemode=true
core.bare=false
core.editor=nano
[email protected]: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
[email protected]
alias.br=branch
# Dry run: Shows what would be deleted
$ git clean -n -d <path>
# Actually deletes files (no going back!)
$ git clean -d -f <path>
# Lists all configured remotes
$ git remote show
origin
# Lists all local/remote branches for remote 'origin'
$ git remote show origin
* remote origin
Fetch URL: [email protected]:ProjectEight/magerun.git
Push URL: [email protected]: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)
To view commits that are present in test
but not in dev
git log dev..test
# List of commits from git log follows...
git tag <tag_name>
Tags are not automatically pushed with commits.
git push origin <remote_branch> --all
# 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
# git tag -d <tag_name>
# e.g. Delete tag '1.0.0' locally
git tag -d 1.0.0
# git push origin :<tag_name>
# e.g. Delete tag '1.0.0' from the remote repository
git push origin :1.0.0
This will list all remote branches which contain the given commit SHA.
git branch -r --contains <commit_sha>
git archive -o patch.zip a9359f9 $(git diff --name-only a9359f9^..a9359f9)
Source: https://stackoverflow.com/a/20686113
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