My Personal Git Cheat Sheet


What is GIT?

Git is a free and open source distributed version control system.

Why we use VCS (version control system)?

Why GIT (among other VCS)?


Setup

Now let’s install Git on our machine and start using it.

Installation

Please follow the offical doc to install git.

Configuration

Use git config command to get and set configuration variables

Config file locations :

Set Config

git config --global user.name "John Doe"
git config --global user.email "johndoe@example.com"

Get Config

git config --list
git config --global --list
git config --system --list

Initialization (create a new repository)

To initialize git on a project, simply use git init command. ( a hidden folder named .git will be created in the project’s root directory )

To get a copy of an existing Git repository, use the git clone command.


Extra

  • What’s Inside .git Directory?
  • Editor : By default, Git uses whatever you’ve set as your default text editor ($VISUAL or $EDITOR) or else falls back to the vi editor to create and edit your commit and tag messages.for example, to use Sublime as the editor, git config --global core.editor "subl -n -w"
  • aliases : we can also easily set up an alias for each command using git config.

Lifecycle

Lifecycle

source : https://git-scm.com/book/en/v2/book/02-git-basics/images/lifecycle.png

Files has mainly 4 statuses :

1. untracked :

at first any new file is untracked.

2. staged :

after we use ‘git add’ command, the file is statged.

3. unchanged :

If the file is not changed since its last commit, the file is unchanged.

4. unstaged :

If there is any modification to that staged or unchanged file, that file becomes unstaged.

note : ‘git checkout’ is a dangerous command! Use stash instead.


Basic Commands

The diagram below show how the git works locally and remotely using different commands.

Commands

source : http://blog.osteele.com/2008/05/my-git-workflow/

I found this video really useful to understand the workflow.

git add

Use git add command to track the untracked files (put it on the stage).

git commit

Commits the staged snapshot to the project history.

Different ways to commit,

git commit

This will open an editor, write your commit message and exit. To write the commit message directly in command line, use the -m flag. e.g, git commit -m "My Commit Message"

git commit -am "commit message"

when we use ‘-a’ flag, it includes all changed files. Git will automatically stage every unstaged files before doing the commit. note that, untracked files will not be commited using this command.

e.g,

git commit -m 'initial commit'
git add forgotten_file
git commit --amend

*do not use `–amend’ if you already pushed your commit.

git log

Show commit logs

use git log command to show all the commits from the very beginning (for currently active branch).

e.g,

commit 1cddaa29ad059a6083e776cc3e4b950e6487f236
Author: Talha Ibne Imam <talha@bscheme.com>
Date:   Sat Apr 22 12:26:11 2017 +0600

    Registration Complete

commit eea5997a17f02e78d78ccf69a233123192a73822
Author: Talha Ibne Imam <talha@bscheme.com>
Date:   Thu Apr 20 14:13:18 2017 +0600

    Design Complete

commit 178d3c00a5e55cd0f6ba0170a9dd58dda24561cf
Author: Talha Ibne Imam <talha@bscheme.com>
Date:   Thu Apr 20 11:35:36 2017 +0600

    First Commit

There are many different options avalaible for showing logs in different ways.

for example,

for more details, please use the command git log --help.

git show :

Shows the changes of a particular commit

git show COMMIT_HASH*

*only first 4-5 character of COMMIT_HASH is enough! (or not?)

If we do not specify COMMIT_HASH, then it will show changes from HEAD. (HEAD is a reference to the last commit in the currently checked-out branch.)

‘git show’ can be used to show other objects rather than commits. such as tags, trees etc. for more details, please visit official link

git diff:

Show changes between commits, commit and working tree, etc

we usually use the git status command to see which files were currently changed. To understand how they were changed in detail, we can use git diff.

git diff

show changes only that are unstaged.

git diff --staged

show changes only that are staged.

we can use --cached instead of --staged (--staged is a synonym of --cached).

git diff HEAD

show changes that are tracked (staged + unstaged).


Working Remotely

git remote:

Manage repositories

To collaborating with other developers, we need to manage remote repositories and pushing and pulling data to and from.

git push:

Push commits to remote repository

After we made some commits, we might want to push them to a remote repository. this can be done with a git push [remote-name] [branch-name] command. ie, git push origin master

git pull:

Fetch and merge remote changes.

perform a pull can be done simply with a git pull command.

‘git fetch’ vs ‘git pull’ :

In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD. src : https://git-scm.com/docs/git-pull

more details : http://stackoverflow.com/a/7104747/7804179

Comments

comments powered by Disqus