Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Tuesday, April 21, 2020

How to perform git revert - revert to previous Git commit - Git Revert Examples

Git is popular distributed and open source version control systems. It is very easy to use. Let us see how can we revert to previous commit.

In order to demo this, I have created a sample repo in GitHub. You can clone it without using ssh keys. You can also try in your Sample Repo as well.

git clone https://github.com/akannan1087/myRepoForJavaApp.git

cd myRepoForJavaApp

First code commit
Let us create a text file called hello.txt
sudo vi hello.txt
Add one line like below
First commit!

git add hello.txt
git commit -m "making our first commit"
git push

you will see all commits with latest commit in the top.

Second code commit
sudo vi hello.txt
Add one line like below
First commit!
Second commit!

git add hello.txt
git commit -m "making our second commit"
git push

third code commit

sudo vi hello.txt
Add one line like below
First commit!
Second commit!
Third commit!!!

git add hello.txt
git commit -m "making our third commit"
git push

Try to view all commits in one line
git log --oneline

547d163 making our third commit
1ec0921 making our second commit
550095c making our first commit
1bad8df Update index.jsp
c206c37 Update index.jsp
161a241 Update index.jsp
5b4e649 my first project setup in GitHub
b429bb6 Initial commit

How to revert to first commit
Now let us say we want to revert to first commit # 550095c making our first commit

git checkout  550095c hello.txt

type git status

git commit -m "reverting to first commit"
git push

cat hello.txt

Please click here if you want to learn more about Git.

Friday, March 13, 2020

What is Source Code Management (SCM) | What is Version Control System

Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.

If you are a software developer or web designer and want to keep every version of a file or layout (which you would most certainly want to), a Version Control System (VCS) is a very wise thing to use. It allows you to revert selected files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more. Using a VCS also generally means that if you screw things up or lose files, you can easily recover. In addition, you get all this for very little overhead.

Version control systems are two types:
1. Centralized version control system
2. Distributed version control system

Centralized Version Control Systems
CVCS have a single server that contains all the versioned files, and a number of clients that check out files from that central place. For many years, this has been the standard for version control.

The most obvious is the single point of failure that the centralized server represents. If that server goes down for an hour, then during that hour nobody can collaborate at all or save versioned changes to anything they’re working on. If the hard disk the central database is on becomes corrupted, and proper backups haven’t been kept, you lose absolutely everything — the entire history of the project except whatever single snapshots people happen to have on their local machines. 

Distributed Version Control Systems
Git is a popular distributed version control system. In Git, developers don’t just check out the latest snapshot of the files; rather, they fully mirror the repository, including its full history. Thus, if any server dies, and these systems were collaborating via that server, any of the client repositories can be copied back up to the server to restore it. Every clone is really a full backup of all the data.

developers clone the remote repo into local machine which is called local repo. When they clone in local machine, all history is copied into local repo. After making code changes, when they commit the code, it goes into local repo with new version. Then they can push code changes into remote repo where it is available to others. Client and remote server will have full repository.

Click here to learn how Git is different that traditional version control systems?

Sunday, February 23, 2020

How to push a code change into a GitHub repository? | push code changes into GitHub using Git bash

Let us see how to push a code change using Git bash or iTerm from Jenkins EC2 instance into GitHub.

After configuring a freestyle job or pipeline using Jenkins for building and deployment, you would like to make a code change to make sure Jenkins have started automated builds/deployments.

Pre-requisites:
Implementation steps:
1. Login to Jenkins EC2 instance first. Make sure you are inside EC2 instance.
2. Type below command to see if you have already cloned your repository.
ls -al

you should see the repo name. once you see the repo name, cd into repo folder

(If you don't see any GitHub Repo folder already cloned, you need to clone from GitHub by executing below command:

git clone git@github.com:user_name/your_repo.git

now go inside repo folder.

3. cd youreponame

change repo_name per your name of the repo you created in GitHub.

Go into below folder by typing the command:
4. Now try to get latest code changes from GitHub into local repo by performing below command:
git pull
5. cd MyWebApp/src/main/webapp
Now you can edit below file to reflect changes in Tomcat
6. sudo vi index.jsp

Change Hello world into below yellow highlighted:
<html>
<body>
<h2>Howdy Folks !!! Welcome to DevOps!</h2>
</body>
</html>

7. press escape, enter :wq!
8. git add index.jsp
9. git commit -m "made change to jsp"
10. git push

Now Login to GitHub and verify the code changes you made are pushed into your repo in GitHub.

Thursday, September 19, 2019

Setup SSH Keys in BitBucket & Setup Java Web App in BitBucket

We will see how to setup Java Web App using Maven in BitBucket and also how to setup SSH keys in Bitbucket.

Pre-requisites:
If you don't have an bitbucket account, You need to create an account in bitbucket.org.
Implementation steps:

1. after login into bitbucket, click on + and Click on Create Repository


2. enter the repo name as MyBitbucketRepo, make sure it is private, Include read me option - choose yes with template. Click Git as version control system.
3. Click on create repository.
4. now click on your avatar (left bottom of the screen, BitBucket settings-> security--> ssh keys


5. now go to your laptop, open git bash for windows laptop or iTerm (for iMac laptop) and connect to Jenkins EC2 instance using Git bash. How to connect, refer this url - http://www.cidevops.com/2018/02/how-to-connect-to-ec2-instance-from.html
6. Now type  ssh-keygen (and then simply enter four times, do not give any password)


7. copy the content of public key executing below command in Gitbash.

sudo cat ~/.ssh/id_rsa.pub

8. go to click on Add key from step # 3,  and paste the content of public key. give some name as mySSH key. click on Add key.
9. Now click on repo you created in step # 1. Click on Clone and copy the SSH url. Do not select HTTPS Url. The url will be from your repo you created under Clone link. 
10. go back to Gitbash  windows connected to Jenkins EC2, execute the below command. Paste the SSH url you copied above.

git clone <ssh_url_from_bitbucket_repo_you_created>

   11. Now after cloning, type ls -al command
12. It will show a folder with repo name..
13. navigate to repo folder by below command:
    cd mybitbucketrepo
14. use below command to create Java project using Maven. Maven is the build for creating and building java projects.
mvn archetype:generate -DgroupId=com.mkyong -DartifactId=MyWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
15. type ls -al to see the new folder called MyWebApp
   16. type  below command to see the newly created project
git status



17. git add  *

18. Perform below command to commit the changes to local repository first.

git commit -m "my first project check-in to bitbucket"
19. Now perform below command to push from local repository to remote repository.

git push
Please watch here for live demo:



Monday, August 19, 2019

How do you handle git merge conflicts? - Git Merge conflicts

Git Merge conflicts

Merging and conflicts are a common part of the Git experience. Conflicts in other version control tools like SVN can be costly and time-consuming. Git makes merging super easy. Most of the time, Git will figure out how to automatically integrate new changes.

Why conflicts happen?

Conflicts generally arise when two people have changed the same lines in a file, or if one developer deleted a file while another developer was modifying it. In these cases, Git cannot automatically determine what is correct. Conflicts only affect the developer conducting the merge, the rest of the team is unaware of the conflict. Git will mark the file as being conflicted and halt the merging process. It is then the developer's responsibility to resolve the conflict.


Monday, August 12, 2019

What is Git? What is difference between Git and GitHub or Bitbucket?

Git is a open source, version control system, also known as SCM(source code management system) tool. It was founded by Linux Torvalds (founder of Linux OS).

Git is different from traditional version control systems such as SVN, CVS, etc. Git is distributed in nature which allows developers to have full history of their code repositories locally.

Git also has other features - branching, merging, pull requests, re-writing commit history.

Git is the most widely used version control system in the world today and is considered the modern standard for software development.

 BitBucket and GitHub are git based repositories hosted on internet or it could be hosted on-prem as well.

You need to have git client installed on your machine in order to access GitHub or BitBucket Repo.

To learn more about the difference between Git and other source version control systems, please click here.

Wednesday, July 31, 2019

How to install Git cliet on RHEL - Install Git client on Red Hat Linux 8

Git client does not come default with RHEL. It can be installed by executing below command:


sudo yum install git

git --version



Thursday, February 21, 2019

stderr: xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools

If you get the above error, you need to go ahead and install XCode command-line tools.

The problem is that Xcode Command-line Tools needs to be updated.

xcode-select --install
 

Wednesday, November 7, 2018

How to push a code change into Bitbucket remote repository? - Code push into Bitbucket using Git bash


Let us see how to push a code change using Git bash or iTerm on your local machine into bitbucket.

After configuring a freestyle job or pipeline using Jenkins for building and deployment, you would like to make a code change to make sure Jenkins have started automated builds/deployments.

Pre-requistes:
SSH keys set up and uploaded into Bitbucket.

Implementation steps:
1. Go to bitbucket, select repository which you already have setup MyWebApp.
If you already cloned, move to step # 6.
2. Click on clone
3. copy SSH clone url
4. go to git bash window
5. paste the url and enter
git clone git@bitbucket.org:username/repo_name.git
6. cd repo_name

change repo_name per your name of the repo you created in Bitbucket
7. cd MyWebApp/src/main/webapp
8. sudo vi index.jsp

Change Hello world into below yellow highlighted:
<html>
<body>
<h2>Howdy Folks !!! Welcome to DevOps!</h2>
</body>
</html>

9. press escape, enter :wq!
10. git add index.jsp
11. git commit -m "made change to jsp"
12. git push

Now refresh the browser and click on Source to see code changes you made in your git bash window.

Friday, July 20, 2018

Git best SCM practices - Git recommended branching strategy -


Find below typical Git branching strategy followed in agile teams:

    • Feature branch – developers may create this branchy for mostly every story during sprint.
    • Develop branch – after feature is developed, code will be merged to develop branch.
    • Release branch – Create this branch for every release, fix the defects raised during QA and release all the way to PROD.
    • Master branch – current prod release code. Once after every release, code needs to merged from release branch to master branch to keep the code in sync.
    • Hot fix branch - For any urgent bug fixes, after the fix is done, code needs to be merged to develop branch and master branch.

      Thursday, July 12, 2018

      Why Git is Distributed ? Advantages of Git - How Git is different from traditional SCM tools?

      Most of version control systems such as SVN, TFVC or CVS work on client-server model. After making code changes, when developers check-in the code changes, it goes to central server. All history is being maintained in a central server, not locally.

      Whereas in Git, developers clone the remote repo into local machine which is called local repo. When they clone in local machine, all history is copied into local repo. After making code changes, when they commit the code, it goes into local repo with new version. Then they can push code changes into remote repo where it is available to others. Client and remote server will have full repository.

       

      Here below is the difference Git and other control systems:

      Area Git SVN/TFVC
      Repository type Distributed Centralized
      Full History Local machine Central machine
      Checkout They clone the whole repository and do locally changes Developer checkout working copy which is snapshot of a code
      Work Offline Yes No
      Branching and Merging Reliable, used often, supports fat forward merge and 3-way merge Reliable, use with caution
      Operations speed Fast and most are local Network-dependent
      Learning Curve Needs a bit time Relatively simple
      Staging area Supported to stash the temporary changes and retrieve it back later Not supported
      Collaboration model Repository-to-repository interaction Between working copy and central repo

      Wednesday, March 21, 2018

      How to add an existing project into bitbucket using command line ?

      Let us say you have a project created along with source code. Now you would like to check-in the project into Bitbucket. How will you do it?

      Pre-requistes:

      1. Make sure Git is installed on that machine.
      2. Make sure you add SSH keys to bitbucket.

      1. Create a new repo in Bitbucket. Do not create README file yet.
      2. Open git bash or command line terminal
      3. navigate to your directory where you have source files.
      4. run the below command to initialize the  directory as local repository.
             git init
      5. Execute git add .
      6. git commit -m "my project setup in bitbucket"
      7. git remote add origin remote repository URL
      8. git push -u origin master

      Now login to Bitbucket and make sure code is available under Source tab for the repo.

      Friday, February 2, 2018

      How to set up SSH keys in Azure Repos | Setup SSH keys and Java Web App in Repos in Azure DevOps

      How to set up SSH keys in Azure Repos and Setup WebApp in Azure Repo


      Go to your visual studio home page. You should be able to go by clicking on the below URL.
      https://dev.azure.com/

      Create a new project by clicking on New project and enter project details like below. This will create a project dashboard.


      Once project is created, Click on Repos.


       Click on initialize link to add README.



      Click on clone link on the top right hand side.
      Click on SSH link
      Click Manage SSH keys

      Click Add

      Go to any virtual machine you had setup that has both Java and Maven installed. If you would like to know how to create a Virtual Machine in Azure Cloud, click here to do so.

      Create the SSH keys by executing the below command:
      (If you already have keys generated, you can overwrite it or skip to next step to copy keys)
      ssh-keygen

      execute the below command to copy the keys:

      cat ~/.ssh/id_rsa.pub

      Add the public keys.
      Once keys added into Azure DevOps, go to Repos, copy the SSH clone url

      Go to your machine where you have installed Java, Maven, preferably your EC2. Execute this command:
      git clone <ssh_url>

      This should download the empty repo from Azure DevOps to local machine(or ec2).
      now after cloning, it will create a folder with repo name..

      type 

      ls -al to see the folder name after you cloned.

      go inside that folder by
      cd reponame

      Now create the maven project executing the below command:
      mvn archetype:generate -DgroupId=com.cf -DartifactId=MyAwesomeApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

      type git status --> to see the newly created project
      git add *
      git status

      git commit -m "my first project check-in to Azure Repos"
      git push

      Now go to Azure DevOps, Select the Repos —> Files —> you should see the project uploaded here.



      Saturday, January 27, 2018

      Git Workflow and commands | Git popular commands


      git init

      This command turns a directory into an empty Git repository. This is the first step in creating a repository. After running git init, adding and committing files/directories is possible.

      git add

      Adds files in the to the staging area for Git. Before a file is available to commit to a repository, the file needs to be added to the Git index (staging area). There are a few different ways to use git add, by adding entire directories, specific files, or all unstaged files.

      git commit

      Record the changes made to the files to a local repository. For easy reference, each commit has a unique ID.
      It’s best practice to include a message with each commit explaining the changes made in a commit. Adding a commit message helps to find a particular change or understanding the changes.

      git status

      This command returns the current state of the repository.

      git clone

      To create a local working copy of an existing remote repository, use git clone to copy and download the repository to a computer.

      git fork
      A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project.

      git pull
      To get the latest version of a repository run git pull. This pulls the changes from the remote repository to the local computer.

      git push

      Sends local commits to the remote repository. git push requires two parameters: the remote repository and the branch that the push is for.

      git checkout

      To start working in a different branch, use git checkout to switch branches.

      git merge

      Integrate branches together. git merge combines the changes from one branch to another branch. For example, merge the changes made in a staging branch into the stable branch.

      git commit --amend

      You can also re-write history of your most recent commit in your local git repository by using git commit --amend command.

      git commit --amend
      
      

      Tuesday, July 4, 2017

      How to set up Jenkins, Maven, Java, Git on Amazon EC2? Script for setting up EC2 with Jenkins, Maven, Java, Git

      Here below is the shell script for installing Git, Jenkins, Java, Maven on RedHat Enterprise Linux...
      Copy the below lines into a shell script, name as setup_ec2.sh. Give necessary permission to execute the shell script and execute it...

      sudo yum -y update
      sudo yum -y install wget
      sudo yum -y install git

      Java installation
      wget --no-cookies --header "Cookie: gpw_e24=xxx; oraclelicense=accept-securebackup-cookie;" "http://download.oracle.com/otn-pub/java/jdk/8u11-b12/jdk-8u11-linux-x64.rpm"
      sudo rpm -i jdk-8u11-linux-x64.rpm
      sudo /usr/sbin/alternatives --install /usr/bin/java java /usr/java/jdk1.8.0_11/bin/java 20000
      sudo /usr/sbin/alternatives --config java

      download jenkins 
      wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
      rpm --import http://pkg.jenkins-ci.org/redhat-stable/jenkins-ci.org.key
      sudo yum -y install jenkins
      service jenkins start

      Maven installation
      sudo wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.reposudo sed -i s/\$releasever/6/g /etc/yum.repos.d/epel-apache-maven.repo
      sudo yum install -y apache-maven

      Sunday, June 4, 2017

      How to migrate Repos from TFVC to Git - TFVC to Git Migration

      Migrate code from Team Foundation Version control (TFVC) to Git

      You can perform the code migration by using an utility called TFS-Git. This is two-way cross platform tool used for migrating TFVC repositories to git. It can update TFS with changes committed to git as well. It works similar to git-svn tool.

      Two things are important for successful code migration.
      1. actual code base - each and every files is migrated
      2. change sets history - this includes entire history.

      Let us get into the crux of our migration.

      Pre-requisites:
      1. Tfs-git. (Download from here)
      2. git client.

      Configuration steps:
      1. Extract git-tf zip into local hard drive, lets say in C:\ drive, after extracting you should see a folder called C:\git-tf
      2. Add c:\git-tf to PATH env variable so that you can execute it from anywhere
      3. Go to command prompt, execute git-tf. You should see some commands. This tells git-tf is successfully installed.

      Migration:
      Migration is actually performed in three phases.
      1. Clone - download project files from TFVC repository by cloning into local machine.
      2. Prep - Set up and qualify downloaded projects for uploading git remote repository.
      3. Push - Execute push command for uploading into git remote repo

      Steps:
      1. Create a remote Git repo(target) in VSTS or where ever you are hosting Git repos.
      2. Go to command prompt, execute the below command for cloning the TFVC repo locally .
      git-tf clone http://<TFS Server Name>:Port/tfs/<CollectionName> "$/TeamProjectName" C:\MyProject –-deep
      --deep means it will include history for the repository.
      3. Now entire project repo is downloaded in your local machine C:\MyProject.
      We can upload into Git repo created in step 1 above. we need to add remote git repo.
      git remote add origin <git_url>
      where git_url could be --> https://MyVSTS.com/DefaultCollection/MyGitreponame/_git/projectRepoName
      4. git push origin master
      or git push head:branchname ---> you can execute this command if you like to migrate to a different branch and didn't want to migrate to master branch.
      This should push code from your local machine into remote Git repo.

      How to verify?
      It is recommended to verify two things to make sure we have successfully migrated.

      1. Comparing the number of commits between TFVC and Git
      Go to command prompt, execute the below command.
      git log --oneline
      This gives list of total commits in that project in one line. Count this total commits and go to TFVC, count the total number of change sets by going into that project , Code --> explorer --> History.
      Both numbers should match.

      2. Compare the total number of files in TFVC and Git(optional step)
      Go to TFVC, select the project. Download as a zip. Once downloaded, right click on the folder to see total number of files and folder. This should match with what we pushed into Git.
      Notes:
      • Depending on size and volume of source TFVC repository, cloning may take considerable amount of time.
      • Avoid doing migration when connected on VPN. It may slow down cloning depending on internet bandwidth.
      That's it! Happy Migration :)