Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Helpful git commands
###############################################################################
# Helpful Git/GitHub commands and code snippets
###############################################################################
### Clone single branch from repo
git clone -b --single-branch <branch> <remote_repo>
### Change commit message ###
git commit --amend -m "Revised message here..."
git push -f #if already pushed
### 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
git tag -d <tagname>;
git push origin :<tagname>
### 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
git checkout -b <new_branch_here>
git push origin <new_branch_here>
git branch # list branches
git add -A # add all
git commit -m "my changes..."
git push --set-upstream origin <new_branch_here>
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_here>
# deletes branch!!
git branch -d <new_branch_here> # 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