24 Mar 2016
Cheat sheet: How to use Git

Cheat sheet: How to use Git

     Either you are a proffessional coder, or you are newbie to programming, either you use Git every day long or you have recently explored it and now you’re finding it very helpful – definitly you have faced up with need to know some basic package of most often used Git commands. 

     So here there are some kind of generalisation of the most wide used Git commands, which, we hope, you might find useful for you in your every day workflow.


TELL GIT WHO YOU ARE

Configure the author name to be used 

with your commits

git config --global user.name "Sam Smith"

CREATE A REPOSITORY

Create a new local repository from scratch

git init [project name]

 

ADD FILES

Add one or more files to staging (index)

git add <filename>
git add *

 

CHECK OUT A REPOSITORY

Create a working copy of a local repository

git clone ~/path_to_repository

For a remote server, use

git clone username@host:/path/to/repository

 

CHECK THE STATUS

List the files you've changed and those you still need to add or commit

git status

 

WORK WITH BRANCHES

Create a new branch and switch to it

git checkout -b <branchname>

Switch from one branch to another

git checkout <branchname>

List all the branches in your repo, and also tells you what 

branch you're currently in

git branch

List all local and remote branches

git branch –r
git branch –av 

Delete the feature branch

git branch -d <branchname>

Push the branch to your remote repository, so others can use it

git push origin <branchname>

Push all branches to your remote repository

git push --all origin

Delete a branch from your remote repository

git push origin :<branchname>

 

UPDATE FROM THE REMOTE REPOSITORY

Fetch and merge changes on the remote server to your working directory

git pull

Fetch and rebase changes on the remote server to your working directory

git pull --rebase

To merge a different branch into your active branch

git merge < branchname>

View all the merge conflicts

git diff

View the conflicts against the base file

git diff--base <filename>

Show changes staged for commit (i.e., difference between index and last commit)

git diff --cached

Show changes (staged and unstaged) in working directory since last commit

git diff HEAD

Preview changes between commits, before merging

git diff <sourcebranch> <targetbranch>

After you have manually resolved any conflicts, you mark the changed file

git add <filename>

 

FILE AND DIRECTORY CONTENTS

Show contents of file (specified relative to the project root) from revision rev

git show rev:file

Show all tracked files (“-t” shows file status)

git ls-files [-t]

Show all untracked files

git ls-files --others

 

CLEANING

Remove all untracked files from working copy

git clean –f

Remove all untracked files and directories from working copy

git clean -fd

Remove all untracked and ignored files from working copy

git clean –fx

Remove all untracked and ignored files and directories from working copy

git clean -fxd

COMMIT

Commit changes to head (but not yet to the remote repository)

git commit -m "Commit message"

Commit changes made to all tracked files since the last commit, 

optionally using commit message msg

git commit –a [-m msg]

Re-commit previous commit, including file1, file2, etc., using previous commit message or, 

optionally, a new one given by msg

git commit --amend file1 file2 [-m msg]

 

PUSH

Send changes to the master branch of your remote repository

git push origin master

 

CONNECT TO A REMOTE REPOSITORY

If you haven't connected your local repository to a remote server, add the server to be able

to push to it

git remote add origin <server>

Adds a remote named remote for the repository at url

git remote add remote url

Remove reference to remote repository named remote: all tracking branches and 

configuration settings for remote will be removed

git rm remote url

List all currently configured remote repositories

git remote -v

 

UNDO LOCAL CHANGES

If you mess up, you can replace the changes in your working tree with the last content in head

git checkout -- <filename>

Changes already added to the index, as well as new files, will be kept.

Instead, to drop all your local changes and commits, fetch the latest history from the server 

and point your local master branch at it, do this

git fetch
origin //no merge
git reset --hard origin/master

Either unstage file, keeping the file changes

git reset [file]

 

WORK WITH TAGS

You can use tagging to mark a significant changeset, such as a release

git tag 1.0.0 <commitID>

CommitId is the leading characters of the changeset ID, up to 10, but must be unique. 

Get the ID using

git log

Show recent commits, most recent on top, with full differences

git log –p

Push all tags to remote repository

git push --tags origin

 

SEARCHING FOR CONTENT

Search working tree for text matching regular expression regexp

git grep regex

 

SEARCHING LOGS AND COMMIT HISTORY

Search commit logs for lines of text matching regular expression regexp

git log --grep regex

 

STASHING

Save your local modifications to a new stash, and run “git reset –hard” to revert them. 

This is the default action when no subcommand is given

git stash save [msg]

List all current stashes

git stash list

Restore the changes recorded in the stash on top of the current working tree state

git stash apply [stash]

Remove a single stashed state from the stash list and apply on top of the current 

working tree state

git stash pop [stash]

Remove all the stashed states

git stash clear

 

COMMIT HISTORY

Show who authored each line in file

git blame file

Show who authored each line in file as of rev (allows blame to go back in time)

git blame file rev
 

AND OF COURSE…

You can always use Git help

git command --help




Workflow:

     Your local repository consists of three "trees" maintained by Git. The first one is your Working Directory, which holds the actual files. The second one is the Index (Stage), which acts as a staging area and finally the HEAD which points to the last commit you've made. Also there is a remote repository – is a version of your project that is hosted on the Internet or network somewhere; when you need to share work with others, you need to push and pull data to and from a remote repository.




     Actually, that's it. Hope You spent these few minutes beneficially. Because the knowledge of these commands could really simplify your daily coding routine.

Similar posts:


Favourite posts

What it Takes to Get an e-Commerce Site Online

Getting an e-Commerce website online might sound like a huge undertaking,...

WebView Interactions with JavaScript

WebView displays web pages. But we are interested not only in web-content...

Google Maps API for Android

Google Maps is a very famous and helpful service, which firmly entrenched...

Unit Testing with RSpec

RSpec is an integral part of Test Drive Development (TDD) and its main id...

Client side JavaScript: Knockout in practice

When developing a web application that extensively works with user input ...

Accessing Field Configurations in JIRA for changing field description

Field configuration defines behavior of all standart (system) fields and ...

A Guide for Upgrading to Ruby on Rails 4.2.0

As you might have already heard, the latest stuff for upgrading rails was...