Last active
May 17, 2023 00:34
-
-
Save garystafford/5565f0b54e917b5e8750 to your computer and use it in GitHub Desktop.
Helpful 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
############################################################################### | |
# Helpful Git/GitHub commands and code snippets | |
############################################################################### | |
#### Remove/Squash All History on Master - CAUTION! #### | |
# https://stackoverflow.com/a/26000395/580268 | |
git checkout --orphan latest_branch \ | |
&& git add -A \\ | |
&& git commit -am "Remove/squash all project history" \ | |
&& git branch -D master \ | |
&& git branch -m master \ | |
&& git push -f origin master | |
#### List all committed files being tracked by git | |
git ls-tree --full-tree -r HEAD | |
### Display remote origin ### | |
git remote --verbose | |
### Clone single branch from repo | |
git clone -b --single-branch <branch> <remote_repo> | |
### Get all branches from a repo | |
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done | |
git fetch --all | |
git pull --all | |
### Delete a single remote branch | |
git push origin --delete <branch> | |
### Change commit message ### | |
git commit --amend -m "Revised message here..." | |
git push -f # if already pushed | |
### Undo an Add File(s) | |
git reset <filename> | |
### Removing files from GitHub ### | |
# that should have been in .gitignore | |
git ls-tree -r master --name-only # list tracked files | |
git rm --cached <file> or git rm -r --cached <folder> #ie. git rm -r --cached .idea/ | |
git add -A && git commit -m "Removing cached files from GitHub" && git push | |
# works even better! | |
git rm --cached -r . && git add . | |
### Tagging repos ### | |
# http://git-scm.com/book/en/v2/Git-Basics-Tagging | |
git tag -a v0.1.0 -m "version 0.1.0" | |
git push --tags | |
git tag # list all tags | |
# Finding and tagging old commit points | |
git log --pretty=oneline # find hash of commit | |
git tag -a v0.1.0 -m 'version 0.1.0' <partial_commit_hash_here> | |
git push --tags #origin master | |
git tag # list all tags | |
git checkout tags/v0.1.0 # check out that tagged point in commit history | |
# Chaning a tag to a new commit | |
git push origin :refs/tags/<tagname> | |
git tag -fa <tagname> | |
git push origin master --tags | |
# Remove a tag both locally and remote | |
git tag -d <tagname>; # local | |
git push origin ::refs/tags/<tagname> # remote | |
### Committing changes to GitHub ### | |
git add -A # add all | |
git commit -m "my changes..." | |
git push | |
# Combined | |
git add . && git commit -am "Initial commit of project" && git push | |
### Adding an existing project to GitHub ### | |
# Create repo in GitHub first | |
git add -A # add all | |
git commit -m "Initial commit of project" | |
git remote add origin <https://github.com/username/repo.git> | |
git remote -v # verify new remote | |
git pull origin master # pull down license and .gitignore | |
# might get message like to merge manually: 'Error reading...Press Enter to continue starting nano.' | |
git push --set-upstream origin master | |
git status # check if add/commit/push required? | |
### Branching GitHub repos ### | |
# https://github.com/Kunena/Kunena-Forum/wiki/Create-a-new-branch-with-git-and-manage-branches | |
# Run from within local repo | |
NEW_BRANCH=<new_branch_here> | |
git checkout -b ${NEW_BRANCH} | |
git push origin ${NEW_BRANCH} | |
git branch # list branches | |
git add -A # add all | |
git commit -m "my changes..." | |
git push --set-upstream origin ${NEW_BRANCH} | |
git checkout master # switch back to master | |
# Merge in the <new_branch_here> branch to master | |
# https://www.atlassian.com/git/tutorials/using-branches/git-merge | |
git checkout master | |
git merge ${NEW_BRANCH} | |
# deletes branch!! | |
git branch -d ${NEW_BRANCH} # deletes branch!! | |
# show pretty history | |
git log --graph --oneline --all --decorate --topo-order | |
# Rollback commit | |
git log # find commit hash | |
git revert <commit_hash> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment