Git Intro

by Horatiu Dan

Abstract

The purpose of this post is to provide a brief and straightforward hands-on introduction to Git, first basic and mostly used commands.

It will the following:

  • How to create a new Git repository
  • The basic operations when working with the local repository – check status, add, commint, change, revert
  • How to make the local repository visible by synchronizing to a remote one (GitHub)

Git Installation

Git can be downloaded from here.

Sample Project

In this post, a simple Java project called ‘gitintro’ is created and published to GitHub. Its few files can be seen in Appendix A.

Command line will be used.

Create – git init

A local folder may be transformed into a project repository by the git init command from inside it and thus, a new Git Repository is created.

c:\_work\git\gitintro>git init Initialized empty Git repository in c:/_work/git/gitintro/.git/ 

Check Status – git status

Anytime during development, the state of the repository may be checked using the git status command.

c:\_work\git\gitintro>git status On branch master No commits yet nothing to commit (create/copy files and use "git add" to track) 

This the main development branch, called master. For now, there are no modifications.

Add, Commit, Change, Revert

Git tracks all changes that happen inside the local repository. It is assumed the sample project is already created inside the ‘gitintro’ folder (see Appendix 1).

The current status is the following:

c:\_work\git\gitintro>git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) .classpath .project .settings/ pom.xml src/ target/ nothing added to commit but untracked files present (use "git add" to track) 

Apart from the pom.xml and /src folder, the others should not be added to the GitHub repository. If written to a .gitignore file that resides in the root folder of the project, they are ignored.

 .gitignore .classpath .project .settings/ target/ 

The new status is:

c:\_work\git\gitintro>git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) pom.xml src/ nothing added to commit but untracked files present (use "git add" to track) 

Currently, all files of interest are untracked.

To add them, git add command is issued.

c:\_work\git\gitintro>git add --all c:\_work\git\gitintro>git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: pom.xml new file: src/main/java/com/hcd/gitintro/Names.java new file: src/test/java/com/hcd/gitintro/NamesTest.java 

To add them as a permanent change to repository, git commit command is issued.

c:\_work\git\gitintro>git commit -m"First version." [master (root-commit) b63316b] First version. 3 files changed, 69 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/hcd/gitintro/Names.java create mode 100644 src/test/java/com/hcd/gitintro/NamesTest.java c:\_work\git\gitintro>git status On branch master nothing to commit, working tree clean 

Let’s modify Names.java. To see the state, git status is used.

c:\_work\git\gitintro>git status On branch master Changes 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: src/main/java/com/hcd/gitintro/Names.java no changes added to commit (use "git add" and/or "git commit -a") 

To see the actual modifications, git diff is used.

c:\_work\git\gitintro>git diff diff --git a/src/main/java/com/hcd/gitintro/Names.java b/src/main/java/com/hcd/gitintro/Names.java index a287550..eef7b92 100644 --- a/src/main/java/com/hcd/gitintro/Names.java +++ b/src/main/java/com/hcd/gitintro/Names.java @@ -5,6 +5,6 @@ import java.util.List; public class Names { public List<String> get() { - return List.of("Andrei", "Ann", "Roger"); + return List.of("Andrei", "Ana", "Roger"); } } 

To revert the changes, git checkout command is used.

c:\_work\git\gitintro>git checkout -- src/main/java/com/hcd/gitintro/Names.java c:\_work\git\gitintro>git status On branch master nothing to commit, working tree clean 

Publish to Remote Repository

The remote repository used is GitHub. The steps are:

  • Create an empty repository in GitHub – go to https://github.com/new, login, fill in the repository name.
  • Link local to remote repository, using git remote command.
    c:\_work\git\gitintro>git remote add origin https://github.com/horatiucd/gitintro.git 
  • Synchronize the local and remote repositories, using git push command.
    c:\_work\git\gitintro>git push -u origin master Enumerating objects: 19, done. Counting objects: 100% (19/19), done. Delta compression using up to 4 threads Compressing objects: 100% (8/8), done. Writing objects: 100% (19/19), 1.79 KiB | 229.00 KiB/s, done. Total 19 (delta 0), reused 0 (delta 0) To https://github.com/horatiucd/gitintro.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'. 

The sample project is available now in GitHub – gitintro

Appendix A

Sample project uses Java 11, Apache Maven 3.6.3 and JUnit 4.13.

It consists of 3 files:

  • Names.java
    package com.hcd.gitintro; import java.util.List; public class Names { public List<String> get() { return List.of("Andrei", "Ann", "Roger"); } } 
  • NamesTest.java
    package com.hcd.gitintro; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.Test; public class NamesTest { @Test public void get() { final Names names = new Names(); List<String> result = names.get(); assertEquals(3, result.size()); } } 
  • pom.xml
    <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hcd</groupId> <artifactId>gitintro</artifactId> <version>0.0.1-SNAPSHOT</version> <description>Sample Maven Project used as Git intro.</description> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <release>11</release> <includeEmptyDirs>true</includeEmptyDirs> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> </dependencies> </project> 

Despre ZTB.ro

ZTB.ro este un agregator românesc de bloguri care colectează și afișează articole din diverse domenii, oferind vizibilitate bloggerilor și o platformă centralizată pentru cititori. Articolele sunt preluate prin feed-uri RSS/Atom și direcționează traficul către blogurile originale.

Articole recente