Getting started easily with your first Git Repository

Posted on 23 June, 2017

Git Repository Configuration

Stages

Files in Git can reside in three main states: committed, modified and staged.    

Commited - Data is securely stored in a local database.

Modified - File  has been changed but not committed to the database.

Staged - To be included in the next  commit.

WorkFlow

The basic Git workflow goes something like this :

  1. You modify files in your working tree.
  2. You stage the files, adding snapshots of them to your staging area.
  3. You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.

Identity Setting

After installing Git, users should immediately set the username and email address, which will be used for every Git commit.

Type the following into Terminal or Command Line :

$ git config --global user.name "Jane Smith"

$ git config --global user.email "example@email.com"

To confirm that you have the correct settings, enter the following command:.

$ git config --global user.name (should return Jane Smith)

$ git config --global user.email (should return example@gmail.com)

Ignoring Files

Usually, untracked files consist of uncommitted files recently added to a project or compiled binaries with extensions such as .obj and .pyc. Compiled binaries can make it difficult to clearly monitor your repository. Git allows users to avoid this situation by ignoring certain files by sending them to a special file with the name .gitignore.

For creating .gitingnore file :

$ git config --global core.excludesfile /home/.gitignore

Below command starting from the directory i'm currently in, find all files whose name ends with extension .pyc, and pass file name to the command git rm -f.

$ find . -name "*.pyc" -exec git rm -f "{}" \ ;

Git repository creation

Open a terminal and change directory to your project directory. Then execute the below command.

$ cd home/projects/helloworld

$ home/projects/helloworld>git init

 Initialized empty Git repository in home/projects/helloworld/.git/

Cloning

 You can also create a copy of an existing Git repository from a particular server, using the clone command with a repository’s URL:

$ git clone https://github.com/demo

This command leads to the creation of a directory called “demo,” with an initialized .git directory inside it, which has copies of all versions of all files for the specified project. The command also automatically checks out or retrieves for editing a copy of the newest version of the project.

To clone a repository into a directory with a desired name, use the following command :

$ git clone https://github.com/demo mydirectory

The above command makes a copy of the target repository in a directory named “mydirectory”.

Branching

 We can create a branch, work on it and save commit files within it, switch between various branches, and merge changes.

A Git branch is basically a movable pointer that always points to the most recent commit or snapshot. 

Git uses the default local branch name “ master,” which can be changed.

Just because the default name is “master” does not imply that it is higher in importance or has more functionality than other branches. 

The head is a special pointer which indicates which branch you are currently working within.

To create a new branch :

$ git branch test

This command creates a new branch named “test”. However, this command does not switch you over to this new branch.

To switch to a new branch: Use the "git checkout" command.

$ git checkout test

This command moves the HEAD pointer to the test branch

Creating a Branch and Checkout :

To simultaneously create a new branch and switch to it, use the -b switch with the git checkout command:

$ git checkout -b test

This command creates a new branch “test2” and switches you to it.

Listing Of Branches: You can list available branches by using the "git branch" command.

$ git branch

Git Status

$ git status

This command is used to check status and report of your repository. 

What does it do? This command can list all the files which are modified after your last commit.

Git status = Current Working Tree - Pointer To Head.

Status Description
Deleted Removed Files
Modified Files with modified content or changed permission
Untracked New files

Diff

Git provides a command “ git diff ” to compare different versions(Changes) of your files.

The most common use of git diff is to see what changes made after your last commit.

To compare changes between the working directory and local repository,

git diff HEAD [filename]

To compare changes in the working directory and local index,

git diff [filename]

To compare changes between an index and local repository,

 git diff --cached [filename]

Most commonly used git command is :

git diff file_name: Explore all changes related to given file only.

git diff: Explore all changes.

  • git diff: Shows the changes that are not staged yet.
  • git diff HEAD: Shows the changes after your last commit. (HEAD Pointer)
  • git diff --cached: The diff between your last commit and changes to be committed next.

Git add : 

Git Version 1.x

Git Version 2.x

This command adds the current changes of a file of working tree to prepare the content staged for the next commit.

 We can execute git add for multiple times, before committing the updates. 

If you want to include changes in the next commit, then you must run git add  to add the new content to the index.

Git Reset

Reset = Undo add

You can undo git add before committing changes,

git reset <file>

Only given file will be removed from the index. File content will not be changed while using git reset.

git reset

Remove all files which are added by git add. Will not affect the content of a file.

Git Commit

When you’re done with your changes and have added the changes using git add  to the staging area, you can now commit your changes to push it into your branch.

Commonly used command of commit:

git commit -m "Your Message"

Git Push

  Usage :

git push <remote> <branch>

 Push the committed content to specified branch to <remote>.  Keep in mind, Git will not allow you to overwrite commits.

Git Pull

git pull <remote> <branch>

git pull = git fetch + git merge

It is to be noted that git fetch operation neither changes any of your own local branches, nor it changes your local working copy.
 
If you want your local branch up-to-date with its remote version then git pull  is a preferable command.


Viraj Joshi , Sr. Odoo Developer
Odoo Technical Notes

Post Your Review

X

Your Review has been posted

0 Comment(s)