Wednesday, November 8, 2017

Installing GIT in GCP instance (centos)

A GCP instance of centos is created and this post tells how to install git

Login to centos SSH and issue the following commands

[pkanchanaece@devops ~]$ sudo yum -y install epel-release
 $sudo yum -y install epel-release  --> This installs extra packages for enterprise linux
 $sudo yum -y install git  --> This installs git
 $rpm -qa | grep git --> To check the package details
 $git --version --> To check the version of the git

 Execute below steps to set up the preferences, which is must to use git.
Before setting these preferences there will not be any file called ".gitconfig" in ~ directory
When the prefernces are set, a hidden file called "gitconfig" is created

 $git config --global user.name "kanchana"
 $git config --global user.email "pkanchana@gmail.com"
 $git config --global core.editor /usr/bin/vim
 $git config --global color.ui true
 $git config --list  or cat ~/.gitconfig  --> To check the preferences.


 How to configure git ?

HOW TO CREATE A NEW PROJECT UNDER GIT

1. Go to cd /root directory
2. Create a new project in the above path
$mkdir devops_training
$cd devops_training
$git init  ---> It will create a folder called .git, a directory with structure under it and it will create a empty project structure
$ls -a --> .git will be available
$sudo yum -y install tree
$tree .git  --> it shows the structure and this folder should not be changed (It is like engine of the vehicle)
$vi file1.txt --> Add few lines
$git status -->
$git add . -->
$git status -->
$git commit -m "any message" -->
$git status --->
$git log -->
$git log --oneline  -->
$git show <commitid> --->

git checkout
git reset HEAD
git commit --amend
git init --bare -shared


How to create branch in git

A copy of the code in master branch is moved to a branch created and  it will also be committed.
All the files and commit ids are copied to newly created branch.

$git branch --> to check branches in system, * indicates which is the current branch.
$git branch <new_branch> --> It will create a branch with the name given

$git checkout <new_branch>   --> It will jump to the branch provided

Create a file in new branch and commit it. Now new branch will have an additional file from master branch, check the diff using

$git diff master <branch _name>

To merge the files in branch1 to master , be in branch1 and issue the command

$git checkout <new_branch> --> be in the branch from where the updates has to be done
$git merge master --> Now files and commits from new branch are merged to master

If new file is created in master and if new branch needs that updation

$git checkout master --> be in master as it has files to be merged to new branch
$git merge branch_name  --> All files will be merged to new branch

Checkout (Branch switch, File undo and Commit)

$git checkout <branch name> ---> To switch to branches
$git checkout <commit id> --> This will go to that particular commit id and ignores commits above that commit id. From here we can create a new branch and add files or add new files and then create a new branch.

$git log --oneline
$git checkout <commit id> --> This command is used when we want to go the particular commit and change something here. Suppose you created 10 modules/files and after commiting 10 files, you remembered that you need to check how 4th module works if you add few lines in file or if any other module is added after 4th module. Then you will checkout from 4th commit id, make necessary changes and then create a new branch. If everything works well you can merge.
$ls  ---> here you can see changes that only files till the checkout commit id are displayed.
$touch newfile.txt  --> create a new file
$git add .
$git commit -m ""
$git checkout -b <new branch name> --> files till the first checkout commit and the new files are copied to the new branch. New branch will not have all the files and commits of master branch.


TO undo the contents in a file use checkout
[root@devops devops_training]# vi f3.txt --> write some lines
[root@devops devops_training]# git checkout -- f3.txt
[root@devops devops_training]# vi f3.txt --> Lines recently written will be removed, lines till commit will be present





RESET

If a file is added to staging and to unstage it and get it to untrack stage use
$vi file1.txt
$git add . --> Added to staging area to unstage issue below command

$git reset HEAD file1.txt

If there are 4 commits and if you want to go to a particular commit and then remove commits above them use
[root@devops devops_training]# git log --oneline
d1f9349 added 4 files
1af7b86 added history  ---> I need till here and remove above commit and files
f070c81 new file
3048bd5 added file1.txt
[root@devops devops_training]# git reset --hard 1af7b86
HEAD is now at 1af7b86 added history
[root@devops devops_training]# git log --oneline
1af7b86 added history
f070c81 new file
3048bd5 added file1.txt
[root@devops devops_training]# ls
file1.txt  history.txt  test.txt
[root@devops devops_training]#

REVERT (This is dangerous action)


$git revert <commit id>



Ammend

This is used to changed the commit message of the last commit id, but make sure to add sufficent details when changing the commit status

[root@devops devops_training]# git log --oneline
645204e new commit
d2dfebc added f4
4499d1d added f3
282811d added 2 files
1af7b86 added history
f070c81 new file
3048bd5 added file1.txt
[root@devops devops_training]# git commit --amend  ----> This will open the file change the commit and save
[master 3b8c8e4] changed commit
 1 file changed, 1 insertion(+)
[root@devops devops_training]# git log --oneline
3b8c8e4 changed commit
d2dfebc added f4
4499d1d added f3
282811d added 2 files
1af7b86 added history
f070c81 new file
3048bd5 added file1.txt


Stashing





[pkanchanaece@devops ~]$ sudo yum -y install epel-release
[pkanchanaece@devops ~]$ sudo yum -y install epel-release
[pkanchanaece@devops ~]$ sudo yum -y install epel-release[pkanchanaece@devops ~]$ sudo yum -y install epel-release
[pkanchanaece@devops ~]$ sudo yum -y install epel-release
[pkanchanaece@devops ~]$ sudo yum -y install epel-release

No comments:

Post a Comment