Skip to main content

Basics of Git

Git is one among the popular open source version control software. This version control software helps you to track the changes in the source code or any text file. The changes can be in the terms of adding, modifying or deleting. All can be tracked through Git!. As per the official documentation "
In Git, however, every developer is potentially both a node and a hub — that is, every developer can both contribute code to other repositories and maintain a public repository on which others can base their work and which they can contribute to." which defines the distributed nature of it.
Let's try to understand how it works.

1. Downloading and Installing Git:

Based on the operating system, choose the installer file. Windows user can download it from the link  https://git-scm.com/ and Ubuntu users can type this command from terminal "sudo apt-get install git"

2. Configuring Git:

git config command is used to set Git configuration values on a global or local level. The basic configuration is to configure the user name and email id of the user.  Now with the git config, you can use the --global or --local for setting the config values. If nothing is passed, then git configure it locally which can be found in the .git\config file.

If the config is done using --global then the configuration would be found in .gitconfig in the home or root directory on linux or unix system. In windows, it can be found in C:\Users\username\.gitconfig


To set those values, type git config --global user.name " abc3036" and git config --global user.email "abc86@gmailc.om" from the terminal. To check the configuration list, type git config --list. If you ever need the help, then type git help 

3. Initialising Local Repository:
Git repository can be created through two ways:
1. Either clone an existing repository. For example: git clone <url> 
2. If the local directory is not under control version then turn it into git repository using git init.  This command create a .git sub directory in the working directory.  To start tracking the files in the working directory, initialise it through git init.  Now to start version controlling, type git add <file_name> or git add .  Both the commands index the changes to the staged area. Now this is the high time to understand the states in git.
   
4. Different states in Git:
Modified: File has some changes in the working directory
Staged: File changes are saved but yet not committed.
Committed: Files changes are stored and committed.
5. Commit the work of Local Repository:
This command is used to record the changes to the local repository. git commit -m "mesage goes here" .
To see the commit history, use command git log

6. Creating Branches:
As per the git documentation "A branch in Git is simply a lightweight movable pointer to one of these commits" . The default branch name is master. Whenever we commit, the head moves towards the recent commit snapshot.
You don't want to commit erroneous things in the master branch therefore it is recommended to create a branch for testing or for development purpose.

For creating a branch, the command is   git branch <branch_name>. For example : git branch development. To list the branch list and where head is pointing to, the command is git branch --list 
7. Switching Out to Different Branch:
Switching to other branches is very easy and simple. git checkout branch_name. For example git checkout development.
Note: Alternatively, git checkout -b development can be used for creating and switching to the development branch.
8. Checking Logs of Different Branches:
git log branch_name --oneline

9. Merging two branches:

git merge <name of the branch>. For example: git merge develop 

10. Creating Alias:
It's easy to create alias for the frequent used commands while working with git. For example instead of writing git checkout branch_name you can write git co branch_name. Such alias can be created by using the command git config --global alias.co checkout 

Some more examples: git config --global alias.br branch
git config --global alias.st status
git config --global alias.ci commit

To know more regarding installing git, please go through the videos. Git Tutorial -1  and Git Tutorial -2 











Comments

Popular posts from this blog

Read different types of data from standard input, process them as shown in output format and print the answer to standard output.

/* Problem Statement: Read different types of data from standard input, process them as shown in output format and print the answer to standard output. Input format: First line contains integer N. Second line contains string S. Output format: First line should contain N x 2. Second line should contain the same string S. Constraints: 0≤N≤10 1≤|S|≤15 where |S|= length of string S  */ import java.util.Scanner; public class TestClass {      public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         int number = 0;         String string = null;         int firstInput = sc.nextInt();         String secondInput = sc.next();         // apply constraints         if (firstInput >= 0 && firstInput <= 1...

Copying files from location to another in linux using Java

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintWriter; public class TestCopyFile { public static void main(String[] args) { File file1=new File(dummy path); String path=System.getenv("HOME"); File file2=new File(path+"/Desktop/dump/CSV_branch_master.csv"); System.out.println(path); System.out.println("after line2"); BufferedReader reader; PrintWriter writer; String line; try { System.out.println("inside try"); if(file2.createNewFile() || !file2.createNewFile()) { System.out.println("inside if"); reader =new BufferedReader(new FileReader(file1)); writer=new PrintWriter(new FileWriter(file2)); while((line=reader.readLine())!=null) { writer.println(line); } reader.close(); writer.close(); } }catch(Exception e) { System.out.println(e +"couldn...