I use git everyday and these are the commands that I cannot live without.
git status: This command must be used trillion times. Use it anytime to see where you are at. git log: gives you a quick overview of previous commit git log -p: gives you an overview of prevous commits + details of files changed. git log --stats: list of files commited. git log -p [hash]..[hash] [filename]: changes to a file between different commits, ie hashes. git diff [hash]..[hash] [file]: see the difference in files between version. git reset --hard [hash]: reset git to certain previous state, discarding all changes. leave hash out to reset to current HEAD. git reset dir/files: unstage files. git clean -df: remove all untracked files from working tree. git commit --amend: edit the last commit comments. git add [files]: stage the files to be committed. git commit: commit the files. git init --bare: create a new repository for files to be committed to it. gitk: simple git gui to track commit changes. My favourite GUI. git stash: save repo in a temp state. "git stash list" to check all the saved state you have.
Branching and Tagging
git checkout -b [new_branch_name] [origin_branch]: create a new branch from current branch and switch into it. if original branch is hosted remotely, can use origin/branch_name instead. git checkout [filename/branch]: checkout the HEAD version of the file or switch branch. git tag [your_own_version_number]: create a tag/snapshot of the current status. able to deploy based on the tag. git describe: good when using tags. let you know where you are. git branch -a: shows you all the branches and which branch you are in. git checkout [hash] [file]: checkout an older version of the file. git remote add origin email@example.com:something/foo.git: add an origin
supposed you forked someone’s repo in github.
git remote add upstream firstname.lastname@example.org:username/repo.git : add a remote host(upstream) to follow. After this command, do a "git fetch upstream" to get all the remote branches. git remote -v: gives you an overview of the remote hosts. git merge upstream/master : merge upstream host with your master.
Pulling and Pushing
It is a good habit to always pull before working on the files, ie committing and pushing. It reduces the chances of conflicts later. Always use git status to track which branch you are in.
git pull [remote] [branch]: pull from remote master and merge to current branch. Make sure you are in local master branch as well. A short cut for this would be just "git pull". git pull --rebase: pull changes but put your commits above the pulled commits. Good habit to prevent a merge commit upon pulling, again helping to maintain a clean git history. git push [remote] [branch]: push your changes upstream to remote branch. A short cut for this would be "git push". git merge [source_branch] [target_branch]: merge all changes from one branch to the another. Able to use the "no-ff" option to create a new commit upon merging. the no-ff option will also keep the branch history. git merge --squash [source_branch] [target_branch]: good if you want to move all changes in the source branch to target branch as one commit. git rebase [branch]: useful command to use in feature branch. when master branch is updated, we run this command in your feature branch, ie "git rebase master" to put your commits above the master commits - gives a clean history. git cherry-pick [hash]: Pick and apply commit from a previous commit on any branch. git rebase [hash]: compress commits in history. this is a dangerous move if you have multiple people working on the remote repo. git rebase -i [hash]~[x]: allows you to squash or change commit text on historical commits, starting from [hash] or HEAD and move x commits before, eg HEAD~4.