Showing posts with label GIT. Show all posts
Showing posts with label GIT. Show all posts

Thursday, 8 August 2019

GIT TOOL MIND MAP FOR EASY LEARNING

We tried to include couple of things
  • Git commands
  • Git concepts
  • Git stages
  • Git terminology
  • Git installation and setup
  • Git public services

GIT MIND MAP


5 STEPS TO MERGE A GIT BRANCH

If you are working in development for a long time, you should know what version control system and a GIT is. In this post, we will see what a GIT branch is and how to merge a branch to other branch or a master in git repository.

WHAT IS A GIT BRANCH?

GIT store all the files and data in a repository. This repository is a centralized location where we store all data by versioning controlling them. A branch is nothing but a complete replica of a repository which effectively forks within your repository. At the time of new branch creation, the branch is similar to the repository main branch(many times it will be a master branch) or from which branch it is forked.
Once the branch is created we can start working on a new branch with out modifying master branch content this way any bugs we introduce is with in your working branch. In this post, we will see how to merge a branch to a master branch after we are confident about the changes.
In this post, we will use two branches
first_branch: This is actual changes which we do and need to merge these changes to another branch.
second_branch: This is the branch where we merge our first_branch changes.

STEPS TO MERGE A GIT BRANCH TO MASTER

STEP1: CHECK OUT TO AN EXISTING BRANCH

First, check what branches are available to you.
git branch
Example:
surendra@linuxnix:~/code/sh/gittraining$ git branch
first_branch
second_branch
* master
From the above output, the * with a name indicates your present branch. So the current branch is a master.
Checkout into first_branch branch.
git checkout branchname
Example:
surendra@linuxnix:~/code/sh/gittraining$ git checkout first_branch
Switched to branch 'first_branch'
surendra@linuxnix:~/code/sh/gittraining$ git branch
* first_branch
second_branch
master

STEP2: COMMIT AND PUSH CHANGES TO A BRANCH

Update your files and commit those changes locally and then push those changes to sync remote repository.
git commit -am “First commit.”
git push
Example:
Check if any changes already exists or not
surendra@linuxnix:~/code/sh/gittraining$ git status
On branch first_branch
Your branch is up-to-date with 'origin/first_branch'.
nothing to commit, working directory clean
Edit your files and check status.
surendra@linuxnix:~/code/sh/gittraining$ vi abc.sh
surendra@linuxnix:~/code/sh/gittraining$ git status
On branch first_branch
Your branch is up-to-date with ‘origin/first_branch’.
Changes are 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: abc.sh
no changes added to commit (use “git add” and/or “git commit -a”)
Commit changes
surendra@linuxnix:~/code/sh/gittraining$ git commit -am “Updated abc.sh script”
[first_branch f564909] Updated abc.sh script
1 file changed, 1 insertion(+)
surendra@linuxnix:~/code/sh/gittraining$ git push
Username for ‘https://github.com: linuxnix
Password for ‘https://linuxnix@github.com
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 342 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/linuxnix/gittraining.git
e4a48b6..f564909 first_branch -> first_branch

STEP3: CHECKOUT TO THE DESIRED BRANCH

Example:
surendra@linuxnix:~/code/sh/gittraining$ git checkout second_branch
Switched to branch 'second_branch'

STEP4: PULL CHANGES TO LOCAL DIRECTORY

surendra@linuxnix:~/code/sh/gittraining$ git pull
Already up-to-date.

STEP5: MERGE CHANGES TO PRESENT BRANCH.

It is a syncing your first branch data with your second branch.
git merge desired_branch_to_merge
Example:
surendra@linuxnix:~/code/sh/gittraining$ git merge first_branch
Updating 075ca41..f564909
Fast-forward
abc.sh | 2 ++
1 file changed, 2 insertions(+)
Check status if the local changes are synced to remote branch or not.
surendra@linuxnix:~/code/sh/gittraining$ git status
On branch second_branch
Your branch is ahead of 'origin/second_branch' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
If remote second_branch and local second_branch are not synced, sync them by using git push command.
surendra@linuxnix:~/code/sh/gittraining$ git push
Username for 'https://github.com': linuxnix
Password for 'https://linuxnix@github.com': 
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/linuxnix/gittraining.git
 075ca41..f564909 second_branch -> second_branch
Hope this helps in understanding How to merge branches. In our next post, we will see how to delete a git branch locally and remotely.

GIT: HOW TO LIST BRANCHES(LOCAL/REMOTE)

Git is the best known distributed version control system at the moment. Sometimes you may require to list available branches within your git repository. In this post, we will see different ways to list available branches.
As we are aware GIT is distributed version control system, that said it has replicas of data locally and remotely repositories. This concept of remote and local applicable to branches as well. We can create local branches and do our work locally and once we are confident we can push those changes by creating a remote branch with the same name. This post is bit details on listing branches with examples for people who are new to git and experienced alike.
The listing of git branches has total three answers with the above explanation. Below are those three type of git branches.
Git list local branches
Git list only remote branches
Git list all branches

LIST ALL LOCAL GIT BRANCHES

git branch
Example:
surendra@linuxnix:~/code/sh/gittraining$ git branch
first_branch
* master
In the above output if you observe we have two branches locally available and master is my present branch(* indicates present branch) where I am located.
Note: We can execute git branch from any directory with in a repository.

LIST ALL REMOTE GIT BRANCHES

git branch -r
-r is for listing any remote branches our git knows.
Example:
surendra@linuxnix:~/code/sh/gittraining$ git branch -r
origin/HEAD -> origin/master
origin/master

LIST ALL REMOTE AND LOCAL GIT BRANCHES

git branch -a
Example:
surendra@linuxnix:~/code/sh/gittraining$ git branch -a
first_branch
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
Note: If you observe above output, the coloring indicates remote(orange) and local branches(green)

GIT: HOW TO SET OR CHANGE DEFAULT EDITOR?

Every user have their favorate editor. Some use VI other uses emacs. This post will help you on how to see what is the text editor in GIT and how change it to your favorate editor. Changing an editor is done in many ways for GIT, either we can edit main git configuration file ~/.gitconfig or we can use "git config" command to set it.

SO WHAT IS MY DEFAULT EDITOR FOR MY GIT?

To know it we have to use git command or open ~/.gitconfig file.
 git config core.editor
Output:
 ○ → git config core.editor

 vim
or
 grep editor ~/.gitconfig

  
 Output:
        editor = vim

Change default GIT editor to your favorate one with a command
 git config --global core.editor editor_name
Example: I want to set vim as my default editor to do that follow below command
 git config --global core.editor vim
change default GIT editor by editing ~/.gitconfig
Enter below entry in to the file under [core] section
 editor = vim
Check if your editor is updated or not with following commands
 git config core.editor

 or
grep editor ~/.gitconfig

GIT: HOW TO SET OR CHANGE USERNAME AND E-MAIL?

When we are using GIT, it’s advaisable to set username and e-mail so that we no need to set them everytime we use GIT to do changes. This post will show you on how to set username and e-mail for editing data with in git.
Check if there is any username and email set for your login to use GIT?
To know it we have to use git command or open ~/.gitconfig file.
Checking user name and password is set or not using git config command
 git config user.name

 git config user.email
Output:
 ○ → git config user.name

 Surendra Anne
And for email
 ○ → git config user.email

 surendra.anne@xyz.com
or check in ~/.gitconfig file for information
 grep -E 'name|email' ~/.gitconfig
How to set user name and e-mail for your login.
 git config --global user.name "your user name"

 git config --global user.email "Your e-mail"
Examples:
 git config --global user.name "John who"

 git config --global user.email "john.who@linuxnix.com"
Again to check these settings are saved or not open ~/.gitconfig file and see the changes.

HOW TO INSTALL GIT IN LINUX WITHOUT ROOT ACCESS ?

INTRODUCTION

In one of our earlier articles on using the git distributed version control system, we showed you how to install git in Linux. As you may observe from that article, installing git is a fairly straightforward process. But what if you were not a system administrator and did not have sahil privileges on the system you were working on. One alternative could be to build the git package from the source code but that would still require access to a compiler like gcc which you may not have.
In this article, we will share a very simple method to install the git version control system on your machine requiring only access to the rpm package for git.

Step 1: Create a folder named git 
The first step is to create a folder named git where you would like to store git related binaries.
[sahil@linuxnix:~] $ mkdir git

Step 2: Copy the git rpm to the git folder
The next step is to copy the git rpm to the folder named git that we created in the previous step.
[sahil@linuxnix:~] $ mv git-1.8.3.1-12.el7_4.x86_64.rpm git
[sahil@linuxnix:~] $ cd git
[sahil@linuxnix:~/git] $ ls -ltr
total 4516
-rw-r--r-- 1 sahil sahil 4600452 Aug 16 08:59 git-1.8.3.1-12.el7_4.x86_64.rpm

Step 3: Extract the git rpm contents
Now we will extract the contents of the git rpm into the current working directory i.e git.
For extracting the rpm contents we will use a combination of the rpm2cpio and cpio commands.
[sahil@linuxnix:~/git] $ rpm2cpio git-1.8.3.1-12.el7_4.x86_64.rpm | cpio -idmv
./etc/bash_completion.d
./etc/bash_completion.d/git
./usr/libexec/git-core
./usr/libexec/git-core/git-add--interactive
---------------------------------------------------output truncated for brevity
For the time being, don’t worry if you are unable to understand how the rpm2cpio and cpio command combination worked. We will emphasize on that aspect of the redhat package manager in a separate article dedicated to it. For the purpose of this demonstration, just understand that the rpm2cpio and cpio command combination extracts all the files contained in the rpm into the current working directory. Note that the file and directory hierarchy structures are maintained during this rpm extraction process.

Step 4: Verify that the git binary is now available
Now that the rpm package contents have been extracted, let’s take a look at the files and directories available in our git folder now.
[sahil@linuxnix:~/git] $ ls
etc git-1.8.3.1-12.el7_4.x86_64.rpm usr
[sahil@linuxnix:~/git] $
Notice that there is a usr directory here. Under the usr directory, we will have a bin directory under which the git related binaries will reside.
[sahil@linuxnix:~/git/usr/bin] $ pwd
/export/home/sahil/git/usr/bin
[sahil@linuxnix:~/git/usr/bin] $ ls
git git-receive-pack git-shell git-upload-archive git-upload-pack
[sahil@linuxnix:~/git/usr/bin] $
The file git is the git binary that we will use.
[sahil@linuxnix:~/git/usr/bin] $ ls -lht git
-rwxr-xr-x 112 sahil unix 1.5M Aug 11 2017 git
[sahil@linuxnix:~/git/usr/bin] $

Step 5: Verify that git works
To verify that the binary actually works, let’s check the version of the git package using the below command
[sahil@linuxnix:~/git/usr/bin] $ ./git --version
git version 1.8.3.1
[sahil@linuxnix:~/git/usr/bin] $

Step 6: Add the location of the git binary to the PATH variable
Under the current setup, we would have to be within our ~/git/usr/bin directory to be able to use the git binary.
To be able to use it from within anywhere in the file system hierarchy, we need to add this location to the PATH environment variable inside the .bash_profile file in our home directory.
grep PATH .bash_profile
export PATH=$PATH:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/local/sbin:$HOME/git/usr/bin

Step 7: Verify that git is available outside ~/git/usr/bin directory
With the PATH variable now set we should be able to use git commands from anywhere within the file system hierarchy. Let’s give it a try.
[sahil@linuxnix:~] $ cd /tmp
[sahil@linuxnix:/tmp] $ pwd
/tmp
[sahil@linuxnix:/tmp] $ git --version
git version 1.8.3.1
[sahil@linuxnix:/tmp] $
The above output confirms that access to the git binary is now available outside the ~/git/usr/bin directory.

CONCLUSION

In this article, we demonstrated how we install the git version control system on our machine despite not having superuser privileges on it.