Total Pageviews

Tuesday, 31 January 2017

MAVEN Installation and Configuration !!!

MAVEN is the most popular Build automation tool. In JAVA environment, packaging is a common thing. Whenever we develop some code, we will create packages. In real time, the source code may contain different packages, to build many packages we require some scripts to automate that build process. 

Here comes the part of Build automation tools. There are many such tools, in this post we are going to discuss MAVEN. It can be used to automate the entire build process, pom.xml (POM -Project Object Model) is the main configuration file. Unlike ANT, Maven uses its own directory structure.

Maven follows different phases in its Default Life Cycle.

Validate      :        In this phase, Maven downloads all required packages and jars.
Compile      :        It compiles the source code.
Test cases    :        Runs test cases.
Run Tests    :        Runs tests (can be QA, UAT).
Package      :        Creates/generates package files.
Install         :        Responsible to perform installations.
Deploy        :        Actual deployment of jar files to Web Server.

Note: “Whenever a phase is called, maven automatically calls all its prior phases….”

Installation is so simple, just to unzip the file and set the environment variable for Maven_Home.



Set the Environment Variable:


To check the version,

C:\Users\lokes>mvn --version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T11:41:47-05:00)
Maven home: F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\bin\..
Java version: 1.8.0_121, vendor: Oracle Corporation               JAVA is a must.....
Java home: C:\Program Files (x86)\Java\jdk1.8.0_121\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "x86", family: "dos"
C:\Users\lokes>

As we discussed, MAVEN follows own directory structure. To get this, we need to generate the archetype (Template kind of) for our Maven.

F:\Workspace_local\Maven_Practice\apache-maven-3.3.9>mvn archetype:generate
[INFO] Scanning for projects...
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom (4 KB at 2.5 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/22/maven-plugins-22.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/22/maven-plugins-22.pom (13 KB at 82.7 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-

************     OUTPUT TRUNCATED    ************

1760: remote -> tr.com.obss.sdlc.archetype:obss-archetype-java (This archetype provides a common skelton for the Java packages.)
1761: remote -> tr.com.obss.sdlc.archetype:obss-archetype-webapp (This archetype provides a skelton for the Java Web Application packages.)
1769: remote -> us.fatehi:schemacrawler-archetype-plugin-lint (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 923:
Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6: 5
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar (5 KB at 28.2 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom (703 B at 10.2 KB/sec)
Define value for property 'groupId': : com.myjava
Define value for property 'artifactId': : java_app
Define value for property 'version':  1.0-SNAPSHOT: : 1.0
Define value for property 'package':  com.myjava: : jar
Confirm properties configuration:
groupId: com.myjava
artifactId: java_app
version: 1.0
package: jar
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: package, Value: jar
[INFO] Parameter: groupId, Value: com.myjava
[INFO] Parameter: artifactId, Value: java_app
[INFO] Parameter: packageName, Value: jar
[INFO] Parameter: version, Value: 1.0
[INFO] project created from Old (1.x) Archetype in dir: F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:24 min
[INFO] Finished at: 2017-02-02T20:25:08-05:00
[INFO] Final Memory: 11M/51M
[INFO] ------------------------------------------------------------------------
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9>

Lets see the directory structure,


F:\Workspace_local\Maven_Practice\apache-maven-3.3.9>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA
 Directory of F:\Workspace_local\Maven_Practice\apache-maven-3.3.9
02/02/2017  08:25 PM    <DIR>          .
02/02/2017  08:25 PM    <DIR>          ..
11/10/2015  11:44 AM    <DIR>          bin
11/10/2015  11:44 AM    <DIR>          boot
11/10/2015  11:38 AM    <DIR>          conf
02/02/2017  08:25 PM    <DIR>          java_app
11/10/2015  11:44 AM    <DIR>          lib
11/10/2015  11:44 AM            19,335 LICENSE
11/10/2015  11:44 AM               182 NOTICE
11/10/2015  11:38 AM             2,541 README.txt
               3 File(s)         22,058 bytes
               7 Dir(s)  226,675,310,592 bytes free
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9>

F:\Workspace_local\Maven_Practice\apache-maven-3.3.9>cd java_app
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>


F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA
 Directory of F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app
02/02/2017  08:25 PM    <DIR>          .
02/02/2017  08:25 PM    <DIR>          ..
02/02/2017  08:25 PM               648 pom.xml
02/02/2017  08:25 PM    <DIR>          src
               1 File(s)            648 bytes
              3 Dir(s)  226,675,310,592 bytes free
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>cd src




F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>cd src\main\java
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\src\main\java>
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\src\main\java>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA
 Directory of F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\src\main\java
02/02/2017  08:25 PM    <DIR>          .
02/02/2017  08:25 PM    <DIR>          ..
02/02/2017  08:25 PM    <DIR>          jar
               0 File(s)              0 bytes
               3 Dir(s)  226,675,310,592 bytes free
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\src\main\java>cd jar
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\src\main\java\jar>

F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\src\main\java\jar>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA
 Directory of F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\src\main\java\jar
02/02/2017  08:25 PM    <DIR>          .
02/02/2017  08:25 PM    <DIR>          ..
02/02/2017  08:25 PM               179 App.java
               1 File(s)            179 bytes
               2 Dir(s)  226,675,310,592 bytes free
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\src\main\java\jar>

Default pom.xml generated by archetype....

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.myjava</groupId>
  <artifactId>java_app</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>java_app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Let's try to run and observe different phases of Maven Life Cycle....


F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building java_app 1.0
[INFO] ------------------------------------------------------------------------
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.pom (8 KB at 4.3 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0.6/maven-2.0.6.pom (9 KB at 90.2 KB/sec)

************** OUTPUT TRUNCATED **************

Downloaded: https://repo.maven.apache.org/maven2/junit/junit/3.8.2/junit-3.8.2.jar (118 KB at 62.6 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/com/google/collections/google-collections/1.0/google-collections-1.0.jar (625 KB at 327.7 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.574 s
[INFO] Finished at: 2017-02-02T20:57:20-05:00
[INFO] Final Memory: 12M/29M
[INFO] ------------------------------------------------------------------------
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>

Let’s Check whether “target” directory created or not….

F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA
 Directory of F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app
02/02/2017  08:57 PM    <DIR>          .
02/02/2017  08:57 PM    <DIR>          ..
02/02/2017  08:25 PM               648 pom.xml
02/02/2017  08:25 PM    <DIR>          src
02/02/2017  08:57 PM    <DIR>          target
               1 File(s)            648 bytes
               4 Dir(s)  226,675,310,592 bytes free
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>

F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>cd target
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\target>

F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\target>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA
 Directory of F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\target
02/02/2017  08:57 PM    <DIR>          .
02/02/2017  08:57 PM    <DIR>          ..
02/02/2017  08:57 PM    <DIR>          classes
02/02/2017  08:57 PM    <DIR>          maven-status
               0 File(s)              0 bytes
               4 Dir(s)  226,675,310,592 bytes free
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\target>

If you try compiling one more time, it won’t download anything. Since the download of required packages and jars is one time.

F:\Workspace_local\Maven_Practice\apache-maven-3.3.9>cd ../
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>

F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building java_app 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java_app ---
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ java_app ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.492 s
[INFO] Finished at: 2017-02-02T21:01:27-05:00
[INFO] Final Memory: 6M/16M
[INFO] ------------------------------------------------------------------------
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>

Now let’s try to run the test phase (So, it will call Validate, Compile and Test)

F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building java_app 1.0
[INFO] ------------------------------------------------------------------------
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-surefire-plugin/2.12.4/maven-surefire-plugin-2.12.4.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-surefire-plugin/2.12.4/maven-surefire-plugin-2.12.4.pom (11 KB at 15.1 KB/sec)
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ java_app ---

************     OUTPUT TRUNCATED    ************

Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.12.4/surefire-booter-2.12.4.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.12.4/surefire-booter-2.12.4.pom (3 KB at 39.0 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.12.4/surefire-api-2.12.4.pom
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running jar.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.584 s
[INFO] Finished at: 2017-02-02T21:02:42-05:00
[INFO] Final Memory: 14M/35M
[INFO] ------------------------------------------------------------------------
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>

Let’s check the “package” phase…

F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building java_app 1.0
[INFO] ------------------------------------------------------------------------
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/2.4/maven-jar-plugin-2.4.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/2.4/maven-jar-plugin-2.4.pom (6 KB at 6.3 KB/sec)
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java_app ---
[INFO] skip non existing resourceDirectory F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\src\main\resources


************     OUTPUT TRUNCATED    ************

[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ java_app ---
[INFO] Surefire report directory: F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\target\surefire-reports
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running jar.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ java_app ---
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.5/maven-archiver-2.5.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.5/maven-archiver-2.5.pom (5 KB at 59.1 KB/sec)


************     OUTPUT TRUNCATED    ************

Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0/plexus-utils-3.0.jar (221 KB at 550.6 KB/sec)
[INFO] Building jar: F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\target\java_app-1.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.880 s
[INFO] Finished at: 2017-02-02T21:04:33-05:00
[INFO] Final Memory: 9M/23M
[INFO] ------------------------------------------------------------------------
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>

We can see clearly, "package" phase called all its prior phases (Validate, Compile, Test cases and then Package) …

F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA
 Directory of F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app
02/02/2017  08:57 PM    <DIR>          .
02/02/2017  08:57 PM    <DIR>          ..
02/02/2017  08:25 PM               648 pom.xml
02/02/2017  08:25 PM    <DIR>          src
02/02/2017  09:04 PM    <DIR>          target
               1 File(s)            648 bytes
               4 Dir(s)  226,675,298,304 bytes free
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>

Final output of package phase should be, generating a jar file... 

F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app>cd target
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\target>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA
 Directory of F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\target
02/02/2017  09:04 PM    <DIR>          .
02/02/2017  09:04 PM    <DIR>          ..
02/02/2017  08:57 PM    <DIR>          classes
02/02/2017  09:04 PM             1,969 java_app-1.0.jar
02/02/2017  09:04 PM    <DIR>          maven-archiver
02/02/2017  08:57 PM    <DIR>          maven-status
02/02/2017  09:02 PM    <DIR>          surefire-reports
02/02/2017  09:02 PM    <DIR>          test-classes
               1 File(s)          1,969 bytes
               7 Dir(s)  226,675,298,304 bytes free
F:\Workspace_local\Maven_Practice\apache-maven-3.3.9\java_app\target>

Similar to all these phases, install and deploy phases will be. Deploy will be the final phase where our source code will be deployed to our Web Server.


In real time, this Build automation will be performed for many builds at a time and so all of them should be integrated continuously and will deployed continuously. To achieve automated Build process along with Continuous Integration and followed by Continuous Delivery we use tools like Jenkins, which will be covered in our next post.

################################################################################

.

.

Monday, 30 January 2017

Git Commands and Administration !!!

In my previous post, I discussed about the installation of Git SCM. In this post, let's see some working with Git. How a developer works, commands they use and the concepts they follow. Git is a distributed VCS, it has many advantages compared to SVN. I am going to explain why we are maintaining a local and remote repo.

Git uses its own directory structure,

    1. master
             main workspace... contains stable code.

    2. branches
             development branches, usually for every Release.

    3. tags
              Usually these are read only, contains files related to every build.

We have already seen basic commands like git add, status, --version, commit in the previous post. Lets go to some other regular commands.

Git log command: full log output

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git log
commit e4c2887da9aad9dedbd325b0926f53773076b057
Author: userh d <userh d>
Date:   Sun Jan 29 22:32:57 2017 -0500
1st file
commit c5ef99904b07aabc9d31324c96cd4d62649370b1
Author: admin <admin@gitblit>
Date:   Sun Jan 29 22:30:30 2017 -0500
Initial commit
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $


Git Short log command: one liner output 



user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git shortlog
userh d (1):
      1st file
admin (1):
      Initial commit
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

Creating users, simple GUI task...





Clone the repo to aj workspace,



user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ cd ../aj_git/
user@userH-PC MINGW64 /f/Workspace_local/aj_git $
user@userH-PC MINGW64 /f/Workspace_local/aj_git $ git clone ssh://aj@10.0.0.229:29418/git_onp_repo.git
Cloning into 'git_onp_repo'...
Password authentication
Password:
remote: Counting objects: 6, done
remote: Finding sources: 100% (6/6)
remote: Getting sizes: 100% (4/4)
remote: Compressing objects: 100% (72/72)
remote: Total 6 (delta 0), reused 3 (delta 0)
Receiving objects: 100% (6/6), 435 bytes | 0 bytes/s, done.
Resolving deltas: 100% (1/1), done.
user@userH-PC MINGW64 /f/Workspace_local/aj_git $

user@userH-PC MINGW64 /f/Workspace_local/aj_git $ ls
git_onp_repo/
user@userH-PC MINGW64 /f/Workspace_local/aj_git $ cd git_onp_repo/
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ ls
first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ touch                  123
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ ls
123  first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        123
nothing added to commit but untracked files present (use "git add" to track)
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

Lets Commit,

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git add 123
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        new file:   123
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $


user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git commit -m "add 123 file"
[master 9799557] add 123 file
 Committer: userh d <userh d>
1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 123
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

 Let's push these changes to remote repo,

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git push
Password authentication
Password:
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 267 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To ssh://10.0.0.229:29418/git_onp_repo.git
 ! [remote rejected] master -> master (User "aj" does not have push permissions for "git_onp_repo.git"!)
error: failed to push some refs to 'ssh://aj@10.0.0.229:29418/git_onp_repo.git'
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

 My aj user does not have permissions for this, so lets assign them....


Let's try now....

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git push
Password authentication
Password:
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 267 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
remote: Updating references: 100% (1/1)
To ssh://10.0.0.229:29418/git_onp_repo.git
   e4c2887..9799557  master -> master
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

These changes should not be there on any other user's repo, lets move onto our earlier repo....

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ cd ../../git_onp_repo/
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

We can pull/fetch the diff changes or the changes pushed by other developers onto remote repo to our local repos...

Difference between pull and fetch…

We can pull the remote repo changes to local using both pull and fetch, but when we used fetch they won’t be applied to “local workspace”. Whereas when we use “pull” they will get applied to local.

No Untracked files, so lets try pulling these diff changes between local and remote repo.

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git pull ssh://admin@10.0.0.229:29418/git_onp_repo.git
Password authentication
Password:
remote: Counting objects: 3, done
remote: Finding sources: 100% (2/2)
remote: Getting sizes: 100% (2/2)
remote: Total 2 (delta 0), reused 2 (delta 0)
Unpacking objects: 100% (2/2), done.
From ssh://10.0.0.229:29418/git_onp_repo
 * branch            HEAD       -> FETCH_HEAD
Updating e4c2887..9799557
Fast-forward
 123 | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 123
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
123  first_file  README.md           We got them, 123 file is pulled….
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $



Command to see available remote repos...

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git remote -v
origin  ssh://admin@10.0.0.229:29418/git_onp_repo.git (fetch)
origin  ssh://admin@10.0.0.229:29418/git_onp_repo.git (push)
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

Command to clean untracked files,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ touch 7 8 9
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
123  7  8  9  abc  first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        7
        8
        9
nothing added to commit but untracked files present (use "git add" to track)
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git clean -f
Removing 7
Removing 8
Removing 9
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
123  abc  first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

Commands to deal with staged files,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ touch 7 8 9

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git add .
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        new file:   7
        new file:   8
        new file:   9
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

There may be situations where we are not supposed to commit these files, but in future we may need them. So, to store them without committing we do stashing.

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git stash
Saved working directory and index state WIP on master: ca8adff add abc
HEAD is now at ca8adff add abc
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git stash list
stash@{0}: WIP on master: ca8adff add abc
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ 


user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
123  abc  first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

Now after few days, if we want those changes to be applied, (always be careful with the naming)

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git stash apply stash@{0}
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        new file:   7
        new file:   8
        new file:   9
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
123  7  8  9  abc  first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

We can commit these changes, if we are sure with the files....


user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:                     
  (use "git reset HEAD <file>..." to unstage)
        new file:   7
        new file:   8
        new file:   9
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

To view the stash list,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git stash list
stash@{0}: WIP on master: ca8adff add abc

To drop/delete a stash,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git stash drop stash@{0}
Dropped stash@{0} (7c48e681cf4a8bd25bfe3ce133f42d08aff01d50)
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git stash list
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git stash pop
           No stash found.                          ---- If found, it will applied and deleted from the list
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

To create branches,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git branch b1
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git checkout b1
A       7
A       8
A       9
Switched to branch 'b1'
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git status
On branch b1
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        new file:   7
        new file:   8
        new file:   9
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ ls
123  7  8  9  abc  first_file  README.md
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $

To push this change to git server,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git push --set-upstream origin b1
Password authentication
Password:
Total 0 (delta 0), reused 0 (delta 0)
remote: Updating references: 100% (1/1)
Branch b1 set up to track remote branch b1 from origin.
To ssh://10.0.0.229:29418/git_onp_repo.git
 * [new branch]      b1 -> b1
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $



To view the Git directory structure,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git ls-remote
Password authentication
Password:
ca8adff0f969129fef85dc20fc6d185b67c7d5fb        HEAD
ca8adff0f969129fef85dc20fc6d185b67c7d5fb        refs/heads/b1
ca8adff0f969129fef85dc20fc6d185b67c7d5fb        refs/heads/master
24c6ca01c3306070b5ace459827ca5e37353e2aa        refs/meta/gitblit/reflog
From ssh://admin@10.0.0.229:29418/git_onp_repo.git
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $

Lets create/delete another branch. 


user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git branch b2
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git branch
* b1
  b2
  master
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git push --set-upstream origin b2
Password authentication
Password:
Total 0 (delta 0), reused 0 (delta 0)
remote: Updating references: 100% (1/1)
Branch b2 set up to track remote branch b2 from origin.
To ssh://10.0.0.229:29418/git_onp_repo.git
 * [new branch]      b2 -> b2
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $



user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git branch -d b2
Deleted branch b2 (was ca8adff).
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git branch
* b1
  master
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $



To push this change to remote repo,

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git push origin :b2
Password authentication
Password:
remote: Updating references: 100% (1/1)
To ssh://10.0.0.229:29418/git_onp_repo.git
 - [deleted]         b2
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git branch
* b1
  master
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $




Commands to work with patchsets. Patchsets are helpful to share the data/files which are not pushed to main remote repo. Dev A can share his files to Dev B, without pushing them to actual git server using patchsets.

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (b1) $ git checkout master
Your branch is up-to-date with 'origin/master'.
Switched to branch 'master'
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ touch aa ab ac ad
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git add .
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        new file:   aa
        new file:   ab
        new file:   ac
        new file:   ad
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $


Let’s create a patch to share these changes to other developer “aj”…


user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git format-patch origin/master --stdout >patch1.patch
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
123  aa  ab  abc  ac  ad  first_file  patch1.patch  README.md
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

I am simply copying, since we are the user aj. In general we do sharing through “scp”, “mails”, “shared dirs.” etc…

user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ cp patch1.patch ../aj_git/git_onp_repo/
user@userH-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

“aj” user in his workspace….

user@userH-PC MINGW64 ~$ cd F:
user@userH-PC MINGW64 /f $ cd Workspace_local/aj_git/git_onp_repo/
            user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ ls
123  abc  first_file  patch1.patch  README.md
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        patch1.patch
nothing added to commit but untracked files present (use "git add" to track)
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

As of now, he does not have the changes done by admin…. Only untracked file is the patch file, which we copied just now…


user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git apply patch1.patch

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        aa
        ab
        ac
        ad
        patch1.patch
nothing added to commit but untracked files present (use "git add" to track)
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $ ls
123  aa  ab  abc  ac  ad  first_file  patch1.patch  patchset_1.patch  README.md
user@userH-PC MINGW64 /f/Workspace_local/aj_git/git_onp_repo (master) $

If we are OK with the files, we can commit them to our local repo…

These are the few commands which are helpful while working with Git SCM. Still I didn’t discuss few concepts related to branch merging, rebase and cherry pick. They are simple and can be implemented easily according to the situation..
 
######################################################################################