-
Notifications
You must be signed in to change notification settings - Fork 76
Quick Git Reference Guide
Quick references to frequently used git commands. For more information about any of them, click on their title and it will take you to the section further down on the page with a description.
-
Add all files to tracked staging area
git add -A
-
Commits all files tracked in the staging area with
git commit -m <message>
-
Commit all tracked and untracked files
git commit -a
-
Checkout a specific branch.
git checkout <branch name>
-
Checkout a previous version of a file. Turns file in working directory into exact copy of file from previous commit, then adds the file to the staging area.
git checkout <commit> <file>
-
Checkout all files from a previous commit
git checkout <commit>
Cherry-Pick (full description)
git cherry-pick <commit>
Stash (full description)
-
Save all untracked .
git checkout <branch name>
-
Checkout a previous version of a file. Turns file in working directory into exact copy of file from previous commit, then adds the file to the staging area.
git checkout <commit> <file>
-
Checkout all files from a previous commit
git checkout <commit>
To view entire commit history, with each commit listed on one line each:
``` git log --oneline```
-
Show the last commits
git log -n <number>
-
Search through commits for specific author
git log --author="<author name>"
-
Search through commits for specific commit message
git log --grep="<commit message>"
Creates a new commit that undoes all changes from <commit>
only:
```git revert <commit>```
-
Remove a file from the staging area.
git reset <file name>
-
Unstage all files.
git reset
-
Current head of the branch goes back to , but does not make any changes to code. All changes that have been made since will appear in the staging area. Useful when you forgot to add a file to your last commit. Reset to one below the head, then add missing file to staging area and re-commit.
git reset <commit>
-
Sets head of branch to , and removes all changes that have been taken since then - including uncommitted changes and committed changes. WARNING: USE CAUTION! YOU COULD LOSE YOUR LATEST WORK!
git reset --hard <commit>
Used to remove files from the staging area (does not remove folders / files in .gitignore).
-
Show files that will be removed:
git clean -n
-
Remove untracked files from the current directory
git clean -f
-
Remove untracked files from specified path
git clean -f <path>
-
Remove untracked files and untracked directories
git clean -df
-
Remove untracked files from current directory, and any files in .gitignore
git clean -xf
Setting up the scenario:
- User Beyonce has branch "dance"
- branch dance has most recent commit "move hips" and second most recent commit "stretch"
- User Batman has branch "flying"
- branch flying has most recent commit "flap arms" and second most recent commit "untangle cape"
Let's say that you are Batman on your flying branch, and you want to add the code changes that are in the commit "stretch", on Beyonce's dance branch.
-
Checkout Beyonce's dance branch.
-
gitk
in terminal -
Click on the commit you want to cherry-pick, i.e. the one with the comment "stretch"
-
Copy the SHA1 ID (with ctrl-c only)
-
KEEP THE
gitk
terminal window open!!! -
In a new terminal window, checkout your own flying branch - i.e. where you want to cherry-pick to.
-
Type
git cherry-pick <SHA1 ID>
, which you get the from ctrl-shift-v -
Check out the cherry-pick with
gitk
to make sure it is correct! -
You are done! You may close all terminal windows now.