menu

Git Cheat Sheet

Git Download • Sample Android .gitignore
Core
git initinitializes .git
git add -Aadds all the git files
git commit -m '[message]'creates a commit with a message (message is mandatory)
git push origin masterpushes to origin
git pull origin masterpulls from origin
git rm -r ---cached [input]removes [input] from the git cache (useful if you want to gitignore an already committed file)
add "-f" after "-r" to force the removal
Forceful Actions
git push -f origin masterforce pushes to origin (remote will be exactly like the local master; changes in origin will be lost)
master is usually used, but you can force push any commit, which will be the equivalent of removing all commits after it (eg "[commit name]:master")
git pull -f origin masterforce pull from origin
git reset --hard HEAD~[number]will remove the latest [number] commits (ie 1 will remove 1 commit) permanently
Remote
git remote -vview existing remotes
git remote add [name] [url] add new remote with given url
git remote rm [name]remove remote
git remote set-url [name] [url]overrides current url for existing remote
git remote set-url --add --push [name] [url]adds new push url to existing remote (one [name] can have multiple [url]s)
Branches
git branch -ashow list of existing branches
git fetch --allfetch all existing branches
git checkout [branch]switch to [branch]; add -b before it if you are making a new branch as well as switching to it
git branch -d [branch]deletes local [branch]
git push origin --delete [branch]deletes remote [branch]
git merge [other branch]merges different branch to active branch
git remote prune originremove listed remote branches (from git branch -a) that no longer exist
Tags
git push --tagspush local tags to remote
git tag -d [tag]delete local tag named [tag]
git push origin :refs/tags/[tag]after local deletion, deletes the remote tag
Other
git clone [url]clones the git url to local
git statusget some info on the file changes
git diffview merge issues
git tag [tag] [commitID]tag a specific commit
git reset --soft HEAD~[number]moves the HEAD [number] commits back without changing any files locally; useful for undoing commits or combining multiple commits into one
git branch --set-upstream-to origin/[my branch]sets default pull to go to [my branch]; instead of using git pull origin [my branch], git pull will suffice
git revert HEAD
git revert HEAD~[number]..HEAD
creates a commit that undoes the last commit, without deleting the commit
With [number], you can revert the last [number] commits
AliasesCustom Commands
git config --global alias.cmp '!f() { git add -A && git commit -m "$*" && git push origin master; }; f'git cmp [message] will add all changes, commit that message, and push it (cmp = commit merge push)
git config --global alias.fullinit '!f() { git init && git remote add origin "$*" && git pull origin master && git branch --set-upstream-to origin/master; }; f'git fullinit [link] will initialize a git, set the link as origin, pull to master, then set the upstream to origin.
Pull IssuesSolution
Your local changes to the following files would be overwritten by mergeAdd all your files and commit it; you can pull again afterwards
# Please enter a commit message to explain why this merge is necessaryPress insert to toggle between insert and replace in the editor
Press esc to exit to a command
While in the command section, type ":wq" and press enter to save and exit.
Automatic merge failed; fix conflicts and then commit the result.Search all your files for "<<<" or ">>>"
All conflicted lines will be encapsulated in those symbols; fix them, add all files, then commit again to resolve it