Getting Started – git

This tutorial is a quick run down of how to get started with ‘git’. For the sake of this short and informative tutorial we are using Bitbucket.org & CentOS 7. We choose Bitbucket.org as it allows private repositories and upto 5 users to share and collaborate – all for free !!!
Git is a popular version control system for maintaining files within a project as a repository. Sites like Github and Bitbucket use git and this blog outlines the basics of it.

PRE-REQUISITES

  • Have a CentOS 7 VM ready on which you would want to install git.
  • Create a non-root user (gituser) as root
    • adduser gituser
    • passwd gituser
    • gpasswd -a gituser wheel [Add the user to wheel group to be able to execute tasks that require root priviledges]

INSTALLING git

  • login / ssh to the CentOS VM as non-root user (in this case ‘gituser’)
  • sudo yum install git
  • git –version
    • $ git --version
      git version 1.8.3.1

NOTE: git version via yum may be a older version. If you need the latest then you may want to manully install git [later blogs]

GETTING STARTED

  • Go over to bitbucket.org and create an account.
  • Get back to your CentOS VM. Create a directory for your Project. In this case its /home/gituser/Myfirstproject. cd to this directory
  • The repository is empty. Create your first file. (Later in the blog – how another git user can access the same project files created by user ‘gituser’)
  • Identify yourself – this helps track who is making the changes.
    • git config –global user.name “Gituser”
    • git config –global user.email “gituser@email.com”
    • Check if the configurations were set – git config –list
$ git config --global user.name "Gituser"
$ git config --global user.email "gituser@email.com"
$ git config --list
user.name=Gituser
user.email=gituser@email.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://rishiihere@bitbucket.org/rishiihere/myfirstrepo.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

WORK FLOW

The workflow can be logically broken down into 3 phases:

Working File –> Staging Area –> Repository

  • When you create your first file – its just another work file
    • $echo >> “My first line” >> file-1.txt
  • Using the ‘git add’ command you push the file to Staging Area
    • git add file-1.txt
    • git status (shows where you are currently in the workflow)
    • $ git status
      # On branch master
      #
      # Initial commit
      #
      # Changes to be committed:
      # (use "git rm --cached <file>..." to unstage)
      #
      # new file: file-1.txt
  • To make the changes permanent and push them to the repository use the below commands
    • git commit -m ‘<message>’
    • git push -u origin <branch-name>
    • $ git commit -m 'My first Commit'
      [master (root-commit) 393d65c] My first Commit
       1 file changed, 1 insertion(+)
       create mode 100644 file-1.txt
      $ git push -u origin master
      Password for 'https://rishiihere@bitbucket.org':
      Counting objects: 3, done.
      Writing objects: 100% (3/3), 227 bytes | 0 bytes/s, done.
      Total 3 (delta 0), reused 0 (delta 0)
      To https://rishiihere@bitbucket.org/rishiihere/myfirstrepo.git
       * [new branch] master -> master
      Branch master set up to track remote branch master from origin.
    •  In bitbucket.org the file now gets reflected under the ‘master’ branch.

REVERTING CHANGES

  • Discard changes in working directory – Undo changes in the file which is not yet staged using ‘git add’
    • git checkout — <filename>
    • $ echo "Line that will be reverted" >> file-1.txt
      $ cat file-1.txt
      My first line
      Line that will be reverted
      $ git status
      # On branch master
      # Changes not staged for commit:
      # (use "git add <file>..." to update what will be committed)
      # (use "git checkout -- <file>..." to discard changes in working directory)
      #
      # modified: file-1.txt
      #
      no changes added to commit (use "git add" and/or "git commit -a")
      $ git checkout -- file-1.txt
      $ cat file-1.txt
      My first line
      $

       

  • To unstage the file after the ‘git add’ command is issued
    • git reset HEAD <filename>
    • $ echo "Remove text after git add" >> file-1.txt
      $ cat file-1.txt
      My first line
      Remove text after git add
      $ git add file-1.txt
      $ git status
      # On branch master
      # Changes to be committed:
      # (use "git reset HEAD <file>..." to unstage)
      #
      # modified: file-1.txt
      #
      $ git reset HEAD file-1.txt
      Unstaged changes after reset:
      M file-1.txt
      $ cat file-1.txt
      My first line
      Remove text after git add
      $ git checkout -- file-1.txt
      $ cat file-1.txt
      My first line
      $
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s