- Graphical git cheatsheet
- Git basic commands
- Git cheatsheet (visual)
- Git cheatsheet (interactive)
- Git full documentation
- Create a new Git repository in current directory:
- Or create an empty Git repository in the specified directory:
git init <directory>
- Or copy an existing Git repository:
git clone <repo URL>
- Clone the repository located at
into the folder called on the local machine:
git clone <repo> <directory> git clone username@host:/path/to/repository
- Global Configuration:
$ git config --global user.name "Firstname Lastname" $ git config --global user.email "email@example.com"
- Stage all changes in
<file>for the next commit:
git add <file>
- Or stage all changes in
<directory>for the next commit:
git add <directory> # usually '.' for current directory
- Commit the staged snapshot to the project history:
git commit # interactive git commit -m "<message>"
- Or add and commit all in one:
git commit -am "message"
- Fix up the most recent commit (don't do that if shared history):
git commit --amend
- List which files are staged, unstaged, and untracked:
git status git status -s # short format
- Show file diff:
git diff # git diff by itself doesn’t show all changes made since your last commit – only changes that are still unstaged. git diff --staged # Shows file differences between staging and the last file version
- Open GUI:
- Displays committed snapshots:
git log -n <limit> git log --graph --decorate --oneline
- Checking out commits, and checking out branches:
git checkout <commit> # Return to commit git checkout master # Return to the master branch (or whatever branch we choose)
- Check out a previous version of a file:
git checkout <commit> <file> # Check out the version of the file from the selected commit git checkout HEAD hello.py # Check out the most recent version
Branches are just pointers to commits.
- List all of the branches in your repository. Also tell you what branch you're currently in ('*' branch):
- Create a new branch called
git branch <branch>
This does not check out the new branch. You need:
git checkout <existing-branch>
Or direcly create-and-check out
git checkout -b <new-branch>
- Safe delete the branch:
git branch -d <branch>
- Merge the specified branch into the current branch:
git merge <branch>
- Undo any undesired changes
Generate a new commit that undoes all of the changes introduced in
<commit>, then apply it to the current branch
git revert <commit>
git revert undoes a single commit — it does not “revert” back to the previous state of a project by removing all subsequent commits.
- Reset (dangerous method - erases history):
- List the remote connections you have to other repositories.
git remote -v
- Create a new connection / delete a connection to a remote repository.
git remote add <name> <url> # often "origin" git remote rm <name> # delete
- Fetch the specified remote’s copy of the current branch and immediately merge it into the local copy. This is the same as
git fetch <remote>followed by
git merge origin/<current-branch>.
git pull <remote>
- Put my changes on top of what everybody else has done. Ensure a linear history by preventing unnecessary merge commits.
git pull --rebase <remote>
- Transfer commits from your local repository to a remote repo.
git push <remote> <branch>
- Pushes the current branch to the remote server and links the local branch to the remote so next time you can do
git push -u origin <branch>
Clone a Repo¶
$ mkdir repos $ cd ~/repos $ git clone https://<url> $ ls -al <repo dir>
Add a change in the working directory to the staging area¶
$ git status $ git add README
--all finds new files as well as staging modified content and removing files that are no longer in the working tree.
$ git add -A $ git commit -m "Add repo instructions" $ git push -u origin master $ git pull $ ssh -p 2222 firstname.lastname@example.org
Short-lived topic branches¶
- Start a new feature:
git checkout -b new-feature master
- Edit some files:
git add <file> git commit -m "Start a feature"
- Edit some files
git add <file> git commit -m "Finish a feature"
- Merge in the
git checkout master git merge new-feature git branch -d new-feature
Push and pull from a centralized repo¶
- To push the master branch to the central repo:
git push origin master
If local history has diverged from the central repository, Git will refuse the request.
git pull --rebase origin master
Sync my local repo with the remote repo¶
git pull origin master git add filename.xyz git commit . -m “comment” git push origin master
Create a central Repo¶
--bare flag creates a repository that doesn’t have a working directory, making it impossible to edit files and commit changes in that repository. Central repositories should always be created as bare repositories because pushing branches to a non-bare repository has the potential to overwrite changes.
$ git init --bare foobar.git $ git rev-parse --show-toplevel # print top-level directory $ git rev-parse --git-dir # print .git directory name