DEV Community

Van Hoang Kha for AWS Community Builders

Posted on

Git basic

Git and Git Flow

Image description

Git Cheat Sheet

Index


Setup

Show current configuration:
$ git config --list
Enter fullscreen mode Exit fullscreen mode
Show repository configuration:
$ git config --local --list
Enter fullscreen mode Exit fullscreen mode
Show global configuration:
$ git config --global --list
Enter fullscreen mode Exit fullscreen mode
Show system configuration:
$ git config --system --list
Enter fullscreen mode Exit fullscreen mode
Set a name that is identifiable for credit when review version history:
$ git config --global user.name “[firstname lastname]”
Enter fullscreen mode Exit fullscreen mode
Set an email address that will be associated with each history marker:
$ git config --global user.email “[valid-email]”
Enter fullscreen mode Exit fullscreen mode
Set automatic command line coloring for Git for easy reviewing:
$ git config --global color.ui auto
Enter fullscreen mode Exit fullscreen mode
Set global editor for commit
$ git config --global core.editor vi
Enter fullscreen mode Exit fullscreen mode

Configuration Files

Repository specific configuration file [--local]:
<repo>/.git/config
Enter fullscreen mode Exit fullscreen mode
User-specific configuration file [--global]:
~/.gitconfig
Enter fullscreen mode Exit fullscreen mode
System-wide configuration file [--system]:
/etc/gitconfig
Enter fullscreen mode Exit fullscreen mode

Create

Clone an existing repository:

There are two ways:

Via SSH

$ git clone ssh://user@domain.com/repo.git
Enter fullscreen mode Exit fullscreen mode

Via HTTP

$ git clone http://domain.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode
Create a new local repository in the current directory:
$ git init
Enter fullscreen mode Exit fullscreen mode
Create a new local repository in a specific directory:
$ git init <directory>
Enter fullscreen mode Exit fullscreen mode

Local Changes

Changes in working directory:
$ git status
Enter fullscreen mode Exit fullscreen mode
Changes to tracked files:
$ git diff
Enter fullscreen mode Exit fullscreen mode
See changes/difference of a specific file:
$ git diff <file>
Enter fullscreen mode Exit fullscreen mode
Add all current changes to the next commit:
$ git add .
Enter fullscreen mode Exit fullscreen mode
Add some changes in <file> to the next commit:
$ git add -p <file>
Enter fullscreen mode Exit fullscreen mode
Add only the mentioned files to the next commit:
$ git add <filename1> <filename2>
Enter fullscreen mode Exit fullscreen mode
Commit all local changes in tracked files:
$ git commit -a
Enter fullscreen mode Exit fullscreen mode
Commit previously staged changes:
$ git commit
Enter fullscreen mode Exit fullscreen mode
Commit with message:
$ git commit -m 'message here'
Enter fullscreen mode Exit fullscreen mode
Commit skipping the staging area and adding message:
$ git commit -am 'message here'
Enter fullscreen mode Exit fullscreen mode
Commit to some previous date:
$ git commit --date="`date --date='n day ago'`" -am "<Commit Message Here>"
Enter fullscreen mode Exit fullscreen mode
Change last commit:

Don't amend published commits!

$ git commit -a --amend
Enter fullscreen mode Exit fullscreen mode
Amend with last commit but use the previous commit log message

Don't amend published commits!

$ git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode
Change committer date of last commit:
GIT_COMMITTER_DATE="date" git commit --amend
Enter fullscreen mode Exit fullscreen mode
Change Author date of last commit:
$ git commit --amend --date="date"
Enter fullscreen mode Exit fullscreen mode
Move uncommitted changes from current branch to some other branch:
$ git stash
$ git checkout branch2
$ git stash pop
Enter fullscreen mode Exit fullscreen mode
Restore stashed changes back to current branch:
$ git stash apply
Enter fullscreen mode Exit fullscreen mode

Restore particular stash back to current branch:

  • {stash_number} can be obtained from git stash list
$ git stash apply stash@{stash_number}
Enter fullscreen mode Exit fullscreen mode
Remove the last set of stashed changes:
$ git stash drop
Enter fullscreen mode Exit fullscreen mode

Search

A text search on all files in the directory:
$ git grep "Hello"
Enter fullscreen mode Exit fullscreen mode
In any version of a text search:
$ git grep "Hello" v2.5
Enter fullscreen mode Exit fullscreen mode
Show commits that introduced a specific keyword
$ git log -S 'keyword'
Enter fullscreen mode Exit fullscreen mode
Show commits that introduced a specific keyword (using a regular expression)
$ git log -S 'keyword' --pickaxe-regex
Enter fullscreen mode Exit fullscreen mode

Commit History

Show all commits, starting with newest (it'll show the hash, author information, date of commit and title of the commit):
$ git log
Enter fullscreen mode Exit fullscreen mode
Show all the commits(it'll show just the commit hash and the commit message):
$ git log --oneline
Enter fullscreen mode Exit fullscreen mode
Show all commits of a specific user:
$ git log --author="username"
Enter fullscreen mode Exit fullscreen mode
Show changes over time for a specific file:
$ git log -p <file>
Enter fullscreen mode Exit fullscreen mode
Display commits that are present only in remote/branch in right side
$ git log --oneline <origin/master>..<remote/master> --left-right
Enter fullscreen mode Exit fullscreen mode
Who changed, what and when in <file>:
$ git blame <file>
Enter fullscreen mode Exit fullscreen mode
Show Reference log:
$ git reflog show
Enter fullscreen mode Exit fullscreen mode
Delete Reference log:
$ git reflog delete
Enter fullscreen mode Exit fullscreen mode

Move / Rename

Rename a file:

Rename Index.txt to Index.html

$ git mv Index.txt Index.html
Enter fullscreen mode Exit fullscreen mode

Branches & Tags

List all local branches:
$ git branch
Enter fullscreen mode Exit fullscreen mode

List local/remote branches

$ git branch -a
Enter fullscreen mode Exit fullscreen mode
List all remote branches:
$ git branch -r
Enter fullscreen mode Exit fullscreen mode
Switch HEAD branch:
$ git checkout <branch>
Enter fullscreen mode Exit fullscreen mode
Checkout single file from different branch
$ git checkout <branch> -- <filename>
Enter fullscreen mode Exit fullscreen mode
Create and switch new branch:
$ git checkout -b <branch>
Enter fullscreen mode Exit fullscreen mode
Switch to the previous branch, without saying the name explicitly:
$ git checkout -
Enter fullscreen mode Exit fullscreen mode
Create a new branch from an exiting branch and switch to new branch:
$ git checkout -b <new_branch> <existing_branch>
Enter fullscreen mode Exit fullscreen mode

Checkout and create a new branch from existing commit

$ git checkout <commit-hash> -b <new_branch_name>
Enter fullscreen mode Exit fullscreen mode
Create a new branch based on your current HEAD:
$ git branch <new-branch>
Enter fullscreen mode Exit fullscreen mode
Create a new tracking branch based on a remote branch:
$ git branch --track <new-branch> <remote-branch>
Enter fullscreen mode Exit fullscreen mode
Delete a local branch:
$ git branch -d <branch>
Enter fullscreen mode Exit fullscreen mode
Rename current branch to new branch name
$ git branch -m <new_branch_name>
Enter fullscreen mode Exit fullscreen mode
Force delete a local branch:

You will lose unmerged changes!

$ git branch -D <branch>
Enter fullscreen mode Exit fullscreen mode
Mark HEAD with a tag:
$ git tag <tag-name>
Enter fullscreen mode Exit fullscreen mode
Mark HEAD with a tag and open the editor to include a message:
$ git tag -a <tag-name>
Enter fullscreen mode Exit fullscreen mode
Mark HEAD with a tag that includes a message:
$ git tag <tag-name> -am 'message here'
Enter fullscreen mode Exit fullscreen mode
List all tags:
$ git tag
Enter fullscreen mode Exit fullscreen mode
List all tags with their messages (tag message or commit message if tag has no message):
$ git tag -n
Enter fullscreen mode Exit fullscreen mode

Update & Publish

List all current configured remotes:
$ git remote -v
Enter fullscreen mode Exit fullscreen mode
Show information about a remote:
$ git remote show <remote>
Enter fullscreen mode Exit fullscreen mode
Add new remote repository, named <remote>:
$ git remote add <remote> <url>
Enter fullscreen mode Exit fullscreen mode
Rename a remote repository, from <remote> to <new_remote>:
$ git remote rename <remote> <new_remote>
Enter fullscreen mode Exit fullscreen mode
Remove a remote:
$ git remote rm <remote>
Enter fullscreen mode Exit fullscreen mode

Note: git remote rm does not delete the remote repository from the server. It simply removes the remote and its references from your local repository.

Download all changes from <remote>, but don't integrate into HEAD:
$ git fetch <remote>
Enter fullscreen mode Exit fullscreen mode
Download changes and directly merge/integrate into HEAD:
$ git remote pull <remote> <url>
Enter fullscreen mode Exit fullscreen mode
Get all changes from HEAD to local repository:
$ git pull origin master
Enter fullscreen mode Exit fullscreen mode
Get all changes from HEAD to local repository without a merge:
$ git pull --rebase <remote> <branch>
Enter fullscreen mode Exit fullscreen mode
Publish local changes on a remote:
$ git push remote <remote> <branch>
Enter fullscreen mode Exit fullscreen mode
Delete a branch on the remote:
$ git push <remote> :<branch> (since Git v1.5.0)
Enter fullscreen mode Exit fullscreen mode

OR

$ git push <remote> --delete <branch> (since Git v1.7.0)
Enter fullscreen mode Exit fullscreen mode
Publish your tags:
$ git push --tags
Enter fullscreen mode Exit fullscreen mode

Configure the merge tool globally to meld (editor)

$ git config --global merge.tool meld
Enter fullscreen mode Exit fullscreen mode
Use your configured merge tool to solve conflicts:
$ git mergetool
Enter fullscreen mode Exit fullscreen mode

Merge & Rebase

Merge branch into your current HEAD:
$ git merge <branch>
Enter fullscreen mode Exit fullscreen mode

List merged branches

$ git branch --merged
Enter fullscreen mode Exit fullscreen mode
Rebase your current HEAD onto <branch>:

Don't rebase published commit!

$ git rebase <branch>
Enter fullscreen mode Exit fullscreen mode
Abort a rebase:
$ git rebase --abort
Enter fullscreen mode Exit fullscreen mode
Continue a rebase after resolving conflicts:
$ git rebase --continue
Enter fullscreen mode Exit fullscreen mode
Use your editor to manually solve conflicts and (after resolving) mark file as resolved:
$ git add <resolved-file>
Enter fullscreen mode Exit fullscreen mode
$ git rm <resolved-file>
Enter fullscreen mode Exit fullscreen mode
Squashing commits:
$ git rebase -i <commit-just-before-first>
Enter fullscreen mode Exit fullscreen mode

Now replace this,

pick <commit_id>
pick <commit_id2>
pick <commit_id3>
Enter fullscreen mode Exit fullscreen mode

to this,

pick <commit_id>
squash <commit_id2>
squash <commit_id3>
Enter fullscreen mode Exit fullscreen mode

Undo

Discard all local changes in your working directory:
$ git reset --hard HEAD
Enter fullscreen mode Exit fullscreen mode
Get all the files out of the staging area(i.e. undo the last git add):
$ git reset HEAD
Enter fullscreen mode Exit fullscreen mode
Discard local changes in a specific file:
$ git checkout HEAD <file>
Enter fullscreen mode Exit fullscreen mode
Revert a commit (by producing a new commit with contrary changes):
$ git revert <commit>
Enter fullscreen mode Exit fullscreen mode
Reset your HEAD pointer to a previous commit and discard all changes since then:
$ git reset --hard <commit>
Enter fullscreen mode Exit fullscreen mode
Reset your HEAD pointer to a remote branch current state.
$ git reset --hard <remote/branch> e.g., upstream/master, origin/my-feature
Enter fullscreen mode Exit fullscreen mode
Reset your HEAD pointer to a previous commit and preserve all changes as unstaged changes:
$ git reset <commit>
Enter fullscreen mode Exit fullscreen mode
Reset your HEAD pointer to a previous commit and preserve uncommitted local changes:
$ git reset --keep <commit>
Enter fullscreen mode Exit fullscreen mode
Remove files that were accidentally committed before they were added to .gitignore
$ git rm -r --cached .
$ git add .
$ git commit -m "remove xyz file"
Enter fullscreen mode Exit fullscreen mode

Git-Flow

Index


Setup

You need a working git installation as prerequisite. Git flow works on OSX, Linux and Windows.
OSX Homebrew:
$ brew install git-flow-avh
Enter fullscreen mode Exit fullscreen mode
OSX Macports:
$ port install git-flow
Enter fullscreen mode Exit fullscreen mode
Linux (Debian-based):
$ sudo apt-get install git-flow
Enter fullscreen mode Exit fullscreen mode
Windows (Cygwin):
You need wget and util-linux to install git-flow.
$ wget -q -O - --no-check-certificate https://raw.githubusercontent.com/petervanderdoes/gitflow/develop/contrib/gitflow-installer.sh install <state> | bash
Enter fullscreen mode Exit fullscreen mode

Getting Started

Git flow needs to be initialized in order to customize your project setup. Start using git-flow by initializing it inside an existing git repository:
Initialize:
You'll have to answer a few questions regarding the naming conventions for your branches. It's recommended to use the default values.
git flow init
Enter fullscreen mode Exit fullscreen mode

OR

To use default
git flow init -d
Enter fullscreen mode Exit fullscreen mode

Features

Develop new features for upcoming releases. Typically exist in developers repos only.
Start a new feature:
This action creates a new feature branch based on 'develop' and switches to it.
git flow feature start MYFEATURE
Enter fullscreen mode Exit fullscreen mode
Finish up a feature:
Finish the development of a feature. This action performs the following:
1) Merged MYFEATURE into 'develop'.
2) Removes the feature branch.
3) Switches back to 'develop' branch
git flow feature finish MYFEATURE
Enter fullscreen mode Exit fullscreen mode
Publish a feature:
Are you developing a feature in collaboration? Publish a feature to the remote server so it can be used by other users.
git flow feature publish MYFEATURE
Enter fullscreen mode Exit fullscreen mode
Getting a published feature:
Get a feature published by another user.
git flow feature pull origin MYFEATURE
Enter fullscreen mode Exit fullscreen mode
Tracking a origin feature:
You can track a feature on origin by using
git flow feature track MYFEATURE
Enter fullscreen mode Exit fullscreen mode

Make a Release

Support preparation of a new production release. Allow for minor bug fixes and preparing meta-data for a release
Start a release:
To start a release, use the git flow release command. It creates a release branch created from the 'develop' branch. You can optionally supply a [BASE] commit sha-1 hash to start the release from. The commit must be on the 'develop' branch.
git flow release start RELEASE [BASE]
Enter fullscreen mode Exit fullscreen mode
It's wise to publish the release branch after creating it to allow release commits by other developers. Do it similar to feature publishing with the command:
git flow release publish RELEASE
Enter fullscreen mode Exit fullscreen mode
(You can track a remote release with the:


git flow release track RELEASE

command)

Finish up a release:
Finishing a release is one of the big steps in git branching. It performs several actions:
1) Merges the release branch back into 'master'
2) Tags the release with its name
3) Back-merges the release into 'develop'
4) Removes the release branch
git flow release finish RELEASE
Enter fullscreen mode Exit fullscreen mode
Don't forget to push your tags with


git push --tags


Hotfixes

Hotfixes arise from the necessity to act immediately upon an undesired state of a live production version. May be branched off from the corresponding tag on the master branch that marks the production version.
Git flow hotfix start:
Like the other git flow commands, a hotfix is started with
$ git flow hotfix start VERSION [BASENAME]
Enter fullscreen mode Exit fullscreen mode
The version argument hereby marks the new hotfix release name. Optionally you can specify a basename to start from.
Finish a hotfix:
By finishing a hotfix it gets merged back into develop and master. Additionally the master merge is tagged with the hotfix version
git flow hotfix finish VERSION
Enter fullscreen mode Exit fullscreen mode

Commands

Image description

Git flow schema

Image description

Top comments (0)