Last active
December 22, 2023 15:59
-
-
Save raineorshine/5128563 to your computer and use it in GitHub Desktop.
Cheatsheet: git commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # tagging | |
| git tag # list available tags | |
| git tag -l v1.4.2.* # search for specific tags | |
| git tag -a v1.4 -m 'version 1.4' # create an annotated tag | |
| git tag -a v1.2 9fceb02 # tag a specific commit (if you forgot) | |
| git show v1.4 # show the tag data of a specific tag | |
| git tag v1.4 # create a lightweight tag | |
| git push --tag # you have to explicitly push tags to remotes | |
| git log --date-order --graph --tags --simplify-by-decoration --pretty=format:'%ai %h %d' # show tags with creation dates | |
| # diff | |
| git diff --word-diff | |
| # remotes - pushing, pulling, and tracking | |
| git fetch # gets remote objects and refs. Needed if new branches were added on the remote. | |
| git remote -v # Lists all remotes | |
| git pull upstream master # Pulls commits from 'upstream' and stores them in the local repository | |
| git push -u origin master # sets ups tracking so that you can 'git push' without extra args | |
| git show :/^Merge # show the last commit whose message matches a regex | |
| # branches - creating, checking out, and merging | |
| git branch # list branches | |
| git branch -a # list branches including remotes | |
| git branch MYBRANCH # Creates a new branch called "MYBRANCH" | |
| git checkout MYBRANCH # Makes MYBRANCH the active branch | |
| git checkout -b MYBRANCH # Shortcut for the last two commands | |
| git branch -d <MYBRANCH> # delete a local branch | |
| git checkout --track origin/<BRANCH_NAME> # create a new local branch with the same name as the remote and set "upstream" configuration | |
| git merge upstream/master # merge the commits from the given branch into the current branch | |
| # reset | |
| git reset --hard HEAD # reset to latest of current branch | |
| git reset --hard 0c6de32 # reset to specific commit | |
| git clean -f # remove untracked files | |
| # create a new repo from a directory in an old repo, preserving history | |
| 1. git clone <old repo> | |
| 2. cd <old repo> | |
| 3. git remote rm origin | |
| 4. git filter-branch --subdirectory-filter <new repo> -- --all | |
| 5. cd <new repo> | |
| 6. curl -u '<username>' https://api.github.com/user/repos -d '{"name":"<new repo>"}' | |
| 7. git remote add origin <new repo> | |
| 8. git push origin master |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent, thanks for posting this!