Dear future self, if you are reading this it looks like you have forgotten how to do something in Git again… Don’t worry, it happens; this page has been created just for you!
Creating and cloning a repository
Clone an existing repository from a URL
git clone https://urlToRepo.com/repoName.git
Clone an existing repository from a file and checkout a specific branch
git clone /path/to/repo/repoName.git -b branchName
Set up a new git repository in the current directory
Stage all new, modified, and deleted files
git add -A
Stage all modified and deleted files but not new (untracked) ones
git add -u
N.B. In git version 2.0 onwards git add -A and git add -u will operate on the entire working tree and not just the current path.
Stage a specific file or set of files
git add path/to/file1.txt file2.txt
N.B. In git version 1.x using git add <pathspec> will only add new and modified files within the specified path-spec. From version 2.0 onwards git add <pathspec> will also stage files that have been deleted within the path-spec.
Stage new and modified files in a specific directory (and deleted files in git version 2.x+)
git add path/to/dir
Stage new, modified, and deleted files in a specific directory (in version 1.x)
git add -A path/to/dir
Delete a file and stage the change in git
git rm path/to/file.txt
Delete a directory and its contents and stage the change in git
git rm -r path/to/dir
Commit staged files with a short commit message
git commit -m "Commit message"
Commit stages files with a custom author
git commit --author="John Doe <firstname.lastname@example.org>" -m "Commit message"
Viewing repository status
View status of repository
View abbreviated status of the repository
git status -s
The left column is the status of files in the staging area and the right column is the status of files in the working tree.
M = Modified, A = Added, D = Deleted, R = Renamed, ? = Untracked files.
Viewing repository history
Show the last 10 commit logs
git log -n 10
Show commit logs in a text graph format
git log --graph
Change the last commit message
git commit --amend
Change the author of the last commit
git commit --amend --author="John Doe <email@example.com>" -m "Commit message"
Create a new branch and switch to it
git checkout -b name_of_new_branch
List all branches (including remote branches)
git branch -a
Checkout an existing branch
git checkout name_of_existing_branch
Delete a branch
git branch -d name_of_branch
Remove local remote tracking branches that have been removed on the remote repository
git remote prune origin
The .gitignore file specifies files that you don’t what to include as part of your git repository
# Ignore .bak files in all directories *.bak # Ignore any files in a log/ directory log/ # Ignore log/*.txt (but not log/foo/*.txt log/*.txt # Ignore all .txt files in a log directory or any of its sub directories log/**/*.txt
Set the committer name and email address globally
git config --global user.name "John Doe" git config --global user.email firstname.lastname@example.org
Set the committer name and email address for the current repository
git config user.name "John Doe" git config user.email email@example.com
Set the default text editor to nano
git config --global core.editor "nano"