05 February 2014

Git




Git is very different compared to other SCMs. When ever you commit, git creates another image of your project and saves the difference. So when you switch between two different branches, you are switching between two images of your project folder.

If you are on Windows, use Git Bash. Tortoise Git is usually not recommended.

To start, select the folder where you want to manage your code.

Then initialise the folder like so:
$ git init

Check with help to see the available options for git-add
$ git add --help

You can do a trial run to see what will get added before you actually execute it
$ git add -na   #This shows what happens if you do - git add -a

Adding specific file/files
$ git add */foo-bar.txt

Adding all files in a specific folder
$ git add */foo/*

Adding modified or deleted tracked files. New files not included.
$ git add -u

Add everything. Add files that are modified, add new files, remove deleted files
$ git add -A

Remove any file

$ git rm file.java

 Lets say you added some code, and you want to start committing code.
$ git add *
$ git commit -m 'comments to your commit'




You want to push these to a remote server and start collaborating
$ git remote add origin https://server.com/foo-bar/project-name.git 

# Creates a remote named "origin" pointing at your remote repository
$ git push origin master 

# Pushes code to HEAD or master or trunk or main 'thing'


You want to get an existing project from the remote server
$ git clone https://server.com/foo-bar/project-name.git

To get the latest code
$ git pull
You changed something and now you want to discard your changes without committing.
$ git checkout -- finename



Instead of totally discarding your changes, you want to save/stash them, for just in case moments
$ git stash save --keep-index



Show me all the branches
$ git fetch
#this updates branch information from remote server

$ git branch --all
#this shows all the branches


You want to know what's up with your project folder
$ git status


You want to change to another branch, commit all local changes and do this:
$ git checkout --track origin/branch_name


If you want some graphical output of the git tree
$ git log --graph --oneline

Display list of files in the commit
$ git log --name-only


To create a new branch
$ git checkout -b new-branch-name

This is same as
$ git branch new-branch-name
$ git checkout new-branch-name

Create a new tag. This will create one locally
$ git tag -a v8.0 -m "commit message"

Push the new created tag to remote
$ git push --follow-tags

  This should get you started.

Happy gitting!