git initCreates a new Git repository in the current directory. In addition, a directory named .git is added to the folder. This folder holds all the Git history and information for the repo. To get rid of the repo, just delete the .git folder
git statusShow me the current status of the repository. Shows
git add .Adds all the files in the current directory to staging.
git add -AAdds all files in the current repo (even new files that are not yet tracked)
git add -uAdd all files that are already being tracked (ignore new files)
git add -pAdd just parts of changes to a file. "p" stands for patch and lets you go through each section of a file that has changed and add (stage) just parts of the file
git addAdds specified file to staging
git commit -m "Add file to repo"Commit staged files to the repo
git commit --amend -m "New Message"Changes the commit message for the last commit
git commit -am "New Message"Lets you add and commit all tracked, modified files in one step.
git commit --amend -m "New commit message"Replace last commit. Use this if you forgot something that you wanted to inlcude in the last commit. Creates a new Git SHA.
git logShow a log of all commits.
git log --onelineShow a log of all commits, one line per commit. Can make this the default by
git config format.pretty oneline
git diffShow differences between working tree and staging (or last commit)
git diff -- stagedShow differences between staged changes and repository
git diff --Show differences for specific file or directory
git rmRemove a file that's being tracked in the repo. If you haven't yet added the file to staging, this will produce an error. You may need to force the removal if the file is staged but not committed:
git rm -f
git rm -rRemove a directory's worth of files. Also removes the directory. Directories themselves aren't tracked in Git. You may need to force the remove if a file in the directory is staged but not committed:
git rm -rf
git mvMove a file that's been committed to the repo.
git mvMove a directory (and its files).
git checkout -bCreate a branch and check it out in one step
git branchCreate a branch, but stay in current branch.
git checkoutCheck out an already created branch
git branchSee a list of all branchs, highlights the currently checked out branch
git checkout masterCheckout master branch
git branch -dRemove branch locally, but only if you've merged branch.
git branch -DRemove branch even if you haven't merged changes.
git checkout master
git mergeReturn to master branch and merge changes from branch
git reset HEADUnstage a file. (HEAD represents the current commit)
git reset HEADUnstage all staged files.
git checkoutTo revert to the last committed version of a file but only if a) the file has been committed and b) is not currently in staging
git checkout HEAD^Revert to version of file from prior commit (HEAD^ represents the prior commit). Careful: this overwrites changes to files in your working branch.
git checkoutRevert to version of file from specific commit
Be careful with these commands when working on a shared repository -- for example with Github. Resetting commits changes the "history" of the repo -- so only use it to back out of local commits that haven not been pushed to a shared repository.
git branch Precaution: always create a backup branch before doing any undo commits. git merge If anything goes wrong during undo commits, you can always roll back to normal using the backup branch.
git reset --soft HEAD^Undo last commit of entire repo, but leave files staged.
git reset --hard HEAD^Completely blow away last commit. Changes files to state of previous commit.
git reset --hard HEAD^^Completely blow away last two commits. Changes files to state of previous commit.
git reset --hard HEAD^^^Completely blow away last three commits. Changes files to state prior to last third commit.
git reset --hardReturns files to state they were in after specificed commit
git diffView differences between current working files and staging area (or if files aren't staged compare working with last commit).
git diffView differences between current working file and staging area (or if file isn't staged compare working with last commit).
git diff --stagedView differences between staged files and last commit.
Don't work on the master branch.
Master branch should hold your working, production files. Don't mess with them. When you need to fix something, or add a new feature to your project, create a new working branch. Make changes to that branch, then merge them into the master branch when done. You can then deploy your master branch (push it up to a web server, for example)
git checkout -b
git statusJust to check and make sure that there are no outstanding changes that have yet to be committed. If there are, add and commit files.
git checkout master
git branch -d
If things gets TOTALLY messed up in your working branch, you can just switch back to the master branch and delete the working branch:
git checkout master
git branch -D
Then just follow steps 1-7 again.
git config --global alias.st statusLets you just type
git stwhenever you want to see the status of the repo
git config --global alias.co checkoutLets you just type
git cowhenever you want to checkout a branch
git config --global alias.ci commitLets you just type
git ciwhenever you want run a commit