01 Git configuration

Set the name that will be attached to your commits and tags.

1
git config --global user.name "Your Name"

Set the e-mail address that will be attached to your commits and tags.

1
git config --global user.email "you@example.com"

Enable some colorization of Git output.

1
git config --global color.ui auto

02 Starting a project

Create a new local repository in the current directory. If [project name] is provided, Git will create a new directory named [project name] and will initialize a repository inside it.

1
git init [project name]

Downloads a project with the entire history from the remote repository.

1
git clone <project url>

03 Day-to-day work

Displays the status of your working directory. Options include new, staged, and modified files. It will retrieve branch name, current commit identifier, and changes pending commit.

1
git status

Add a file to the staging area. Use. in place of the full file path to add all changed files from the current directory down into the directory tree.

1
git add [file]

Show changes between working directory and staging area.

1
git diff [file]

Shows any changes between the staging area and the repository.

1
git diff --staged [file]

Discard changes in working directory. This operation is unrecoverable.

1
git checkout -- [file]

Revert some paths in the index (or the whole index) to their state in HEAD.

1
git reset [path...]

Create a new commit from changes added to the staging area. The commit must have a message!

1
git commit

Remove file from working directory and staging area.

1
git rm [file]

04 Storing your work

Put current changes in your working directory into stash for later use.

1
git stash

Apply stored stash content into working directory, and clear stash.

1
git stash pop

Delete a specific stash from all your previous stashes.

1
git stash drop

05 Git branching model

  • Commit a state of the code base
  • Branch a reference to a commit; can have a tracked upstream
  • Tag a reference (standard) or an object (annotated)
    • HEAD a place where your working directory is now

List all local branches in repository. With -a: show all branches (with remote).

1
git branch [-a]

Create new branch, referencing the current HEAD.

1
git branch [branch_name]

Apply commits of the current working branch and apply them to the HEAD of [branch] to make the history of your branch more linear.

1
git rebase [branch_name]

Switch working directory to the specified branch. With -b: Git will create the specified branch if it does not exist.

1
git checkout [-b] [branch_name]

Join specified [branch_name] branch into your current branch (the one you are on currently)

1
git merge [branch_name]

Remove selected branch, if it is already merged into any other. -D instead of -d forces deletion.

1
git branch -d [branch_name]

06 Inspect history

List commit history of current branch. -n count limits list to last n commits.

1
git log [-n count]

An overview with reference labels and history graph. One commit per line.

1
git log --oneline --graph --decora

List commits that are present on the current branch and not merged into ref. A ref can be a branch name or a tag name.

1
git log ref..

List commit that are present on ref and not merged into current branch.

1
git log ..ref

List operations (e.g. checkouts or commits) made on local repository

1
git reflog

07 Tagging commits

List all tags.

1
git tag

Create a tag reference named [name] for current commit. Add commit sha to tag a specific commit instead of current one

1
git tag [name] [commit sha]

Create a tag object named [name] for current commit.

1
git tag -a [name] [commit sha]

Remove a tag from local repository.

1
git tag -d [name]

08 Reverting changes

Switches the current branch to the target reference, leaving a difference as an uncommitted change. When –hard is used, all changes are discarded. It’s easy to lose uncommitted changes with –hard.

1
git reset [--hard] [target reference]

Create a new commit, reverting changes from the specified commit. It generates an inversion of changes.

1
git revert [commit sha]

09 Synchronizing repositories

Fetch changes from the remote, but not update tracking branches.

1
git fetch [remote]

Delete remote Refs that were removed from the remote repository

1
git fetch --prune [remote]

Fetch changes from the remote and merge current branch with its upstream.

1
git pull [remote]

Push local changes to the remote. Use –tags to push tags.

1
git push [--tags] [remote]

Push local branch to remote repository. Set its copy as an upstream.

1
git push -u [remote] [branch]

10 Ignoring files

To ignore files, create a .gitignore file in your repository with a line for each pattern. File ignoring will
work for the current and sub directories where .gitignore file is placed. In this example, all files are
ignored in the logs directory (excluding the .gitkeep file), whole tmp directory and all files *.swp.

1
2
3
4
5
6
cat <<EOF >  .gitignore
/logs-*
!logs/.gitkeep
/tmp
*.swp
EOF