Git Cheatsheets¶
- Graphical git cheatsheet
- Git basic commands
- Git cheatsheet (visual)
- Git cheatsheet (interactive)
- Git full documentation
Repo hosting:
Common Commands¶
- Create a new Git repository in current directory:
- Or create an empty Git repository in the specified directory:
- Or copy an existing Git repository:
- Clone the repository located at
<repo>
into the folder called<directory>
on the local machine:
- Global Configuration:
git config --global user.name "Firstname Lastname"
git config --global user.email "your_email@youremail.com"
- Stage all changes in
<file>
for the next commit:
- Or stage all changes in
<directory>
for the next commit:
- Commit the staged snapshot to the project history:
- Or add and commit all in one:
- Fix up the most recent commit (don't do that if shared history):
- List which files are staged, unstaged, and untracked:
- 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:
- 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¶
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
<branch>
.
This does not check out the new branch. You need:
Or direcly create-and-check out <new-branch>
.
- Safe delete the branch:
- Merge the specified branch into the current 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
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.
- Create a new connection / delete a connection to a remote repository.
- 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 bygit merge origin/<current-branch>
.
- Put my changes on top of what everybody else has done. Ensure a linear history by preventing unnecessary merge commits.
- Transfer commits from your local repository to a remote repo.
- Pushes the current branch to the remote server and links the local branch to the remote so next time you can do
git pull
orgit push
.
Typical Workflows¶
Clone a Repo¶
Add a change in the working directory to the staging area¶
-A
, --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 user@domain.com
Short-lived topic branches¶
- Start a new feature:
- Edit some files:
- Edit some files
- Merge in the
new-feature
branch
Push and pull from a centralized repo¶
- To push the master branch to the central repo:
If local history has diverged from the central repository, Git will refuse the request.
Sync my local repo with the remote repo¶
Create a central Repo¶
The --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.