Git Cheat Sheet
Git Download • Sample Android .gitignore
Core | |
---|---|
git init | initializes .git |
git add -A | adds all the git files |
git commit -m '[message]' | creates a commit with a message (message is mandatory) |
git push origin master | pushes to origin |
git pull origin master | pulls 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 master | force 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 master | force pull from origin |
git reset --hard HEAD~[number] | will remove the latest [number] commits (ie 1 will remove 1 commit) permanently |
Remote | |
git remote -v | view 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 -a | show list of existing branches |
git fetch --all | fetch 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 push -u origin [branch] | sets the origin of the current branch and pushes |
git merge [other branch] | merges different branch to active branch |
git remote prune origin | remove listed remote branches (from git branch -a) that no longer exist |
Tags | |
git tag --sort=committerdate | show tags by date committed rather than alphabetically |
git push --tags | push 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 status | get some info on the file changes |
git diff | view 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 |
Aliases | Custom Commands |
git config --global alias.cmp '!f() { git add -A && git commit -m "$*" && git push; }; f' | git cmp [message] will add all changes, commit that message, and push it (cmp = commit merge push) |
git config --global alias.delete-merged '!f() { git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d; }; f' | git delete-merged will remove all branches that have been merged, excluding master and dev |
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. |
git config --global alias.copyHash '!f() { git log --pretty=format:"%h" -n 1 | clip.exe; }; f' | git copyHash will copy the latest commit hash in the pretty format; note that clip.exe is for windows. You may use `xclip -selection clipboard` for linux, and `pbcopy` for mac. |
git config --global alias.copyFullHash '!f() { git rev-parse HEAD | clip.exe; }; f' | git copyFullHash will copy the latest commit hash in its entirety |
Pull Issues | Solution |
Your local changes to the following files would be overwritten by merge | Add all your files and commit it; you can pull again afterwards |
# Please enter a commit message to explain why this merge is necessary | Press 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 |