Daniel Gafitescu Freelance web developer focused on open source solutions


Git Cheat Sheet

I have been working with Git for few months and even if at the start was kind of difficult to grasp it after switching from SVN.

Here is a list of most used commands :


Cloning the repository
> git clone git://url

>  git config --global user.name “Firstname Lastname”

> git config --global user.email “user@email.com”

> git config --global color.ui true

> git config --global alias.st "status" -  define an alias to git status : > git st

List the configuration

> git config --list - list all the configuration

Setup a project in the root directory

>  git init

Look inside the git file

> tree .git

Add all files to repository

> git add .

Commit files to the repository
> git commit -m “First import”

Commit all changes that have done to the project ( ask for a message)

> git commit -a

Add a single file to the commit (is qued up for commit)
> git add {filename} - add the file
> git rm {filename} - delete the file
> git mv {first_file} {second_file} - rename

See prev change ( DIFF between changes)
git diff {filename} - see the difference between changes

get information about the tracked files , changed or new files  
> git status

See log
> git log
> git log --until=2012-06-14
> git log --since=2012-05-12
> git log --author=”James”
> git log --grep=”Init”
> git log --online

HEAD - last state of the repository , what was last checked out

git show {sha} - show the commit information
git diff {sha} - show a diff from a previous commit
git diff {sha1}..{sha2}  show a diff between 2 commits
git diff {sha1}..{sha2}  {filename} - show a diff between 2 commits on a file

git diff --stat --summary
git log --graph --oneline --decorate --all


- master is the main branch ( ~ trunk in the svn)

Branch listing

> git branch

Create a new branch

>  git branch human => (create the “human” branch )

To switch to the human branch we need to log out from the master branch

> git checkout human ( swich to the human branch )

If I want to get the master changes in the human branch

> git rebase master

Merge human to master

First you want to see the changes

1) git checkout master

2) git diff master .. new_feature - compare 2 branches

3) git merge new_feature - merge the branch with the name new_feature ( I am in the master branch)

To check if the branch is merged you can do a diff between branches git diff master..new_feature

If we want to go back before the merge

4) git reset --hard ORIG_HEAD

If we have conflicts on merge make changes and then commit

> git branch --merged - show which branches are merged

> git branch -m {old_branch_name} {new_branch_name}- rename the branch
> git branch -d {branch_name} -
delete a branch

> git push origin :{branch_name} - delete a remote branch

> git push origin --delete {branch_name} - also deletes a remote branch


When you work on the public server you need to push the commit to live server.

> git push

Donwload the last changes

> git pull

Git Architecture:

2-tree architecture - repository & working copy (svn use)
3-tree architecture - repository , stagging index & working copy (git use)

working copy -> git add file.txt ->  staging index -> git commit file.txt -> repository

Undone changes

- undo changes to the working directory

git checkout {filename}  or
git checkout -- {filename}

- unde changes to the staging directory

git reset HEAD {filename}

- change the commit message or add new change to a file that is already on staging directory
git commit --amend -m “Change the files  already staged”

- changes to the older commits
git checkout {hash} -- {filename}

- revert a commit
git revert {hash}

- rever multiple commits ( move the HEAD pointer)
git reset
-- soft (move the pointer but doesn’t change the staging index or working directory)
-- mixed (default) = move the head pointer to the specified commit and change the staging index but doesn’t change the working directory
-- hard - move the HEAD , change the staging index and working directory

git clean -f   - remove the untracked files



- store changes temporary , only visible for you
git stash save “config changes” -
save the files to stash
git stash list
- show the stash lists with the name of the changes
git stash show stash@{0}
- show the file changes in specified stash (to see more add -p option)
git stash pop {stash_item}
- get and remove changes from stash , if a stash item is not specified get the last one
git stash apply {stash_item} -
get the changes from the stash

git stash drop stash@{0} - delete a stash item
git stash clear - remove all stash items

Ignoring files

/proect_dir/.gitignore - add all the files and directories that will be ignored by git.

Sample file:
# comment
test.txt - ignore file test.txt
*.tmp   - ignore all the files that have the extension .tmp

If you want to ignore specific files you need to alter .gitignore_global file located in the user directory

If you want to track an empty directory , create a empty file .gitkeep in that specific directory


Manage a GitHub repository in Netbeans 7.1

GitHub create repository

A bit of introduction

As with latest Netbeans version 7.1 it comes with git support integrated inside the IDE, along with SVN and Mercurial.

Since GitHub has become such a popular site for keeping open source repositories as well as private projects ( many companies like Facebook , Twitter are using it to keep their private repositories ).

Since my IDE of choice is  Netbeans I couldn't clone a public repository that I have created on GitHub only using Netbeans ( hope there's a solution and maybe it will be added in a future version it will be added to the IDE but as far as I have tried I coudn't do it).

So in order to create the origin and master branch I had to download and use a separate tool which is easy to install and work with and you have the whole process explained  on GitHub's Set Up page .

If you are a Mac User I advise that you use GitHub for Mac which I have used and works great!

Setup the repository on GitHub

If you don't have an account on github go ahead and create one if you allready have click on the "New Repository" button .

GitHub create repository

GitHub create repository

You are going to be redirected to a form where you need to enter a project title and a description.

After you have created the repository you are redirect to a page where you are instructed how to create the general setting of your name and email , initialize the repository and create the origin and master branches.

Taking the next steps in command line

The steps that are listed in the repository detail page need to be done in command line. As mentioned before everything is explained in detail  on GitHub Setup page.

Since I am on windows I have downloaded the latest Git for Windows installed it , generated a ssh key and add it to my account settings. Once you read that you need to enter the commands listed on the repository detail page on the Git Bash.

 As you can see from the image I setup all in d:\www\codeigniter but you can put it anywhere you want because right after last command I am going to delete it completely so I can clone it again from Netbeans.

Now if you go to the repository detail page all you are going to see it's the readme file.


Netbeans setup

1) Team > Git > Clone

2) Clone the repository , you enter the repository url along with the user/password

3) Select master branch

4) Save to a destination directory. Please note that I have initialized the project from command line in d:\www\codeigniter . Before doing this next step I went and deleted all the files including the hidden ones.

5) Create project from cloned sources

6) Next the project will be created and I will add the new files to it , in my case I have downloaded Codeingiter 2 and add it to the project and I will commit the files using the commit command from the context menu

7) Next you will need to enter the commit message and select the files that you need to add to the commit.

8) After this commit if you go the github repository page you will see that nothing has changes this is because the git system works. Commit will be done one the local created repository

If you open the output view from the Netbeans windows you will see something like this :

9) Next step will be doing the Remote push to Github , and this is done by selecting from the context menu Git > Remote > Push

10) Next you need to select the remote repository from dropdown :

11) Select the Local Branch

12) Update local references which basically copies the master local branch to origin/master branch remote.

13) After this if you go to github repository page you will see all files listed.

And that's it. Again if someone else know how to do this without downloading the command line tool directly for Netbeans let me know.

It would be nice the guys from Netbeans would add this feature in the near future.