You are on page 1of 2

https://version.aalto.

fi/
GIT CHEATSHEET
1. GIT CONFIGURATION 3. DAY-TO-DAY WORK A. GIT INSTALLATION
$ git config --global user.name "Your Name" $ git status For GNU/Linux distributions Git should be available in the standard
system repository. For example in Debian/Ubuntu please type in
Set the name that will be attached to your commits and tags. See the status of your work. New, staged, modified files. Current branch.
the terminal:
$ git config --global user.email "you@example.com" $ git diff [file]
$ sudo apt-get install git
Set the e-mail address that will be attached to your commits and tags. Show changes between working directory and staging area.
$ git config --global color.ui auto $ git diff --staged [file] If you want or need to install Git from source, you can get it from
Shows changes in the staging area that haven't been commited.
https://git-scm.com/downloads.
Enable some colorization of Git output.
$ git checkout -- [file]
An excellent Git course can be found in the great Pro Git book by
2. STARTING A PROJECT Discard changes in working directory. This operation is unrecoverable. Scott Chacon and Ben Straub. The book is available online for free
$ git add [file] at https://git-scm.com/book.
$ git init [project name]
Add a file to the staging area. Use . instead of full file path, to add all
Create new local repository. If [project name] is provided, Git will create changes files from current directory down into directory tree. 4. GIT BRANCHING MODEL
a new directory named [project name] and will initialize a repository inside it. $ git reset [file]
If [project name] is not provided, then a new repository is initialized in current $ git branch [-a]
directory. Get file back from staging area to working directory.
List all local branches in repository. With -a: show all branches (with remote).
$ git clone [project url] $ git commit [-m "message here"]
$ git branch [name]
Downloads a project with entire history from the remote repository. Create new commit from changes added to the staging area. Commit must
have a message! You can provide it by -m. Otherways $EDITOR will be opened. Create new branch, referencing the current HEAD.

B. IGNORING FILES $ git rm [file] $ git checkout [-b] [name]

Remove file from working directory and add deletion to staging area. Switch working directory to the specified branch. With -b: Git will create the
$ cat .gitignore specified branch if it does not exist.
$ git stash
/logs/*
$ git merge [from name]
!logs/.gitkeep Put your current changes into stash.
/tmp
$ git stash pop Join specified [from name] branch into your current branch (the one you are
*.swp
on currenlty).
Thanks to this file Git will ignore all files in logs directory (excluding Apply stored stash content into working directory, and clear stash.
$ git branch -d [name]
the .gitkeep file), whole tmp directory and all files *.swp. Described file $ git stash
statusdrop
ignoring will work for the directory (and children directories) where .gitignore Remove selected branch, if it is already merged into any other. -D instead of
file is placed. Clear stash without applying it into working directory. -d forces deletion.

GitLab - Everyone can contribute https://about.gitlab.com/ v1.0.1


GIT CHEATSHEET
5. REVIEW YOUR WORK 6. TAGGING KNOWN COMMITS C. THE ZOO OF WORKING AREAS
$ git log [-n count] $ git tag Another remote repository.
Git is a distributed version conrol
Remote repository
system. You can have as many
List commit history of current branch. -n count limits list to last n commits. List all tags. named origin?
remote repositories as you want.
You've probably made
Just remeber to update them
git clone from here.
$ git log --oneline --graph --decorate $ git tag [name] [commit sha] frequently :)

An overview with references labels and history graph. One commit per line. Create a tag reference named name for current commit. Add commit sha to remote repo remote repo
tag a specific commit instead of current one. (name: origin)
$ git log ref.. (name: public)
$ git tag -a [name] [commit sha]
List commits that are present on current branch and not merged into ref. git fetch git push git push public master
A ref can be e.g. a branch name or a tag name. Create a tag object named name for current commit. or
git pull remote repositories
$ git log ..ref $ git tag -d [name]
local repository
List commit, that are present on ref and not merged into current branch. Remove a tag from a local repository.
repository git commit
$ git reflog
Changes commited here
List operations (like checkouts, commits etc.) made on local repository. 7. REVERTING CHANGES will be safe.
If you are doing backups!
You are doing it, right!?
index
(staging area)
8. SYNCHRONIZING REPOSITORIES $ git reset [--hard] [target reference] git reset HEAD

Switch current branch to the target reference, and leaves a difference as an stash git stash Only index will be
$ git fetch [remote] uncommited changes. When --hard is used, all changes are discarded. committed.
git add Choose wisely
$ git revert [commit sha]
A kind of a shelf git stash pop working what to add!
Fetch changes from the remote, but not update tracking branches. for the mess
you don't want to include. directory
$ git fetch --prune [remote] Create a new commit, reverting changes from the specified commit. It
generates an inversion of changes. You do all the
Remove remote refs, that were removed from the remote repository. hacking right here!

$ git pull [remote]

Fetch changes from the remote and merge current branch with its upstream. commit an object D. COMMITS, BRANCHES AND TAGS
$ git push [--tags] [remote] branch a reference to a commit; can have a tracked upstream

origin/fix/a
Push local changes to the remote. Use --tags to push tags. tag a reference (standard) or an object (annotated) This is a local branch.
It is 3 commits ahead,

fix/a
HEAD a place where your working directory is now

working-version
$ git push -u [remote] [branch] you see it, right?
This is an upstream branch This is also
a local branch
Push local branch to remote repository. Set its copy as an upstream. This is a tag. It looks like a developer's

master
note so it's probably a reference,
not an object.

And this is the past. Here was chaos, This is an initial commit, This is a merge commit,
This is a normal commit,

v1.0.1

HEAD
where no version control was used. it has no parents it has one parent it has two parents!
D o n' t l i v e i n c h a o s ! This is a tag. It looks like a
version so it's probably an object Your working directory
Use Git! is here
(annotated tag)

GitLab - Everyone can contribute https://about.gitlab.com/


https://version.aalto.fi/
v1.0.1

You might also like