Saturday, October 25, 2014

GIT Repository Tool - the stupid content tracker

This is a quick guide on commands to get started with using git in Linux.

What is git?
Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals. 

It is a repository program similar to Perforce for example that allows you to make changes to your work and 'check' them in and continue to make changes and at any time you could revert back to any version you previously had. It also can be integrated with github to share your work with others and have multiple people make changes at the same time. It is a powerful revision management system and collaboration tool.

Installing git

git is available on Linux, Mac and Windows, but in this guide I will only highlight using it with Linux. 

To install on a Debian-based distribution like Ubuntu, from terminal use:
$ apt-get install git

For Fedora:
$ yum install git

For installing from Source or on other OS's, please visit:
http://git-scm.com/book/en/v2/Getting-Started-Installing-Git

Initializing a Directory to use git (git init)

Navigate to the directory of your project or work that you want to backup using git.

$ cd myproject
~/myproject $ git init
Initialized empty Git repository in /home/user/myproject/.git/

Now there will be a hidden folder named ".git" in that directory that git will use for your repository. Do not modify or delete this folder unless you want to stop using git for this project.

Removing a Directory from git

Just delete the .git directory from your project folder and all your git repository will be gone.

Adding a File To Track in Your Repository (git add )

Please note, that you can only add files to track, not directories. It doesn't matter what your directory structure is. But if you move files or rename directories, then you will have to do a move command to rename your file (since the path changed). Using git status (below) will notify you if you need to do this and how to do it.

Create your new file then to add them use:

git add

Example:
~/myproject $ printf "hello world" > test.txt
~/myproject $ git add test.txt

Checking Status of your changes (git status)
Use the following command to show you a list of all the changes you made before committing.

git status

It will tell you how to add a file.

-----------------------------------------------------------------

How To Use GIT

A. Create a GIT Repository
    1. Navigate to the directory you want to be the repository
        ex. ~/git/priyaandarun
    2. Use command:
        git init

B. Select files that you want to modify/add
    1. git add FILENAME

C. Committing changes to repository
    1. git commit -m "Description of changes"

D. Update Git Hub with your changes
    1. git push -u [REPOSITORY] [BRANCH ON GITHUB]
        ex. git push -u priyaandarun master

E. Create New Branches
    1. git checkout -b [NEW BRANCH NAME]
        ex. git checkout -b Working
    NOTE: The default branch is called 'master'

F. Switch Branches
    1. git checkout [BRANCH]
        ex. git checkout Working
        ex. git checkout master

G. Save Changes without Committing
    1. git stash        // saves changes and reverts
    2. git stash pop    // brings back last stashed changes
    3. git stash drop   // discards stashed changes
    4. git stash list   // lists all stashes

7. See changes
    1. git log          // Shows who made changes
    2. git reflog       // Shorter change list

8. Change to a specific changelist #
    1. git reset [CHANGELIST#]  // revert all files back to previous state
    2. git checkout [FILENAME]  // revert specific file to latest

9. Git color
    1. git config color.ui true

10. Merging
    1. git mergetool