This document serve as a reference of git commands. Its organization follow a (kind of) logical order to help you to understand how git works from scratch, but notice that its just a reference. If you want to start from zero with git, I strongly recomend you to take a look in those links (In this order):
- Try Git Tutorial;
- Git-it;
- The document that you're reading right now;
- Roger Dudler - Git Guide;
- Bruno Passos - Git commands;
Now that you know it, feel free to go through this document. I hope it help! 😎
git config user.name (See how your name is displayed in commits)git config user.email (See what is your email showed in commits)
git config user.name "Your name here"git config user.email "myemail@mydomain.com"
This will set information to all repositories from your computer.git config --global user.name "Your name here"git config --global user.email "myemail@mydomain.com"
git config --global --edit
git init
[+] More info about init
git remote add origin <URL> (Set repo origin)git remote add upstream <URL> (Set upstream if you have it.)
git statusgit status -s (Short version)
[+] More info about status
git checkout "path/to/file.html" (Individual file)git checkout . (All files)
[+] More info about checkout
git add "path/to/file.html" (Individual file)git add . (All files)"
[+] More info about add
git reset HEAD "path/to/file.html" (Individual file)git reset HEAD . (All files)
git reset --hard HEAD "path/to/file.html" (Individual file)git reset --hard HEAD . (All files)
git reset --hard SHA
[+] More info about reset
git clean -f path/to/file (individual files)git clean -df (All files)
[+] More info about clean
git commit -m "A nice message about your commit"
git commit --amend (Modify just the message from last local commit)
Add a new file to stage area and include it to last local commit:
git add path/to-file.htmlgit commit --amend "A nice new message about your commit"
[+] More info about add
[+] More info about commit
git fetch --dry-run
git pull
[+] More info about pull
git push
[+] More info about push
git loggit log --oneline (Show a small version of your log)git log --graph (Show a graphical representation of commits/branches)git log --reverse (Show a reverse log)git log --since=yesterday (Show only commits from yesterday and before)git log --author "Name of someone" (Show only commits from that person)
You can concatenate parameters:git log --oneline --reverse --graph
[+] More info about log
git show (Show last commit, including diff of each file)git show SHA (Show a specific commit)git show --stat SHA (Show a commit but instead of show diff, shows only diffstat)
[+] More info about show
git diff (Long version)git diff --stat (Resumed status version)git diff --name-only (Show only list of diles that have changes)git diff --staged (Show difference in files that you already added to the stage area (git add))
git reflog (Short version)
git reflog --pretty (Detailed version)
[+] More info about reflog
git diff SHA
[+] More info about diff
git revert SHA (Creating a new commit)git revert -m SHA (Not creating a new commit. Keep revert files unstaged)
[+] More info about revert
git branch (Only local branches)git branch --remote or git branch -r (Only remote branches)git branch --all or git branch -a (Local and remote branches)
[+] More info about branch
git checkout name-of-branch (In this case the branch need to exist before checkout)git checkout -b name-of-new-branch (Create a new branch from the actual and checkout to it)
git diff branch-name..other-branch-name
git push -u origin name-of-branch
git branch -d name-of-branch (Delete local branch only if it's updated with remote version of it.)git branch -D name-of-branch (Delete local branch even if you have non pushed changes to remote version of it.)
git push -d origin name-of-branch (Delete remote branch)
To merge a branch you need to checkout to the branch that will receive the merge:
git checkout name-of-branch-will-receive-mergegit merge name-of-branch-to-be-merged
git pull --rebase
You need to be in the branch:git rebase master
[+] More info about rebase
You need to be in the branch that will receive the rebasegit rebase name-of-branch-to-be-rebased
You need to be in the branch:
git rebase mastergit checkout mastergit merge name-of-branch
git shortlog (Show commits description splited by user)git shortlog -s -n (Show name and number of commits per user ordered by More commits to less commits)
[+] More info about shortlog