Git is one of the most widely used distributed/decentralized version control systems. To use Git, you first need to download and install it on your computer. For Windows, you can download the Git installer (at the time of writing, a file named Git-2.20.1-64-bit.exe
) from the following web site:
For other operating systems, check the following web site for details:
https://www.atlassian.com/git/tutorials/install-git
Double-click the installer file Git-2.20.1-64-bit.exe
to install Git, and accept all the default settings during the installation. After successful installation, run the program named Git GUI, shown in Figure C.1A. Select Create New Repository, and a new window will pop up, as shown in Figure C.1B. Select the directory where your project will be located, in this case, E:MyProject
, and then click the Create button. The Git GUI program's main interface will appear, as shown in Figure C.1C. From the Repository menu, select Git Bash, which is a terminal program, as shown in Figure C.1D. This is where you are mainly going to use Git. You can use standard commands, such as pwd
to display the current project directory and dir
or ls
to list its content, as shown in Figure C.1D.
For more details about Git, check this free online Pro Git book:
https://git-scm.com/book/en/v2
To start with version control using Git, first you will need to use the git config --global
command to configure your name and your email address in the Git Bash program, as shown in Figure C.2 (top). You can also use the git config --list
command to list all your configurations.
Then type git init
to initialize the version control. This will generate a hidden directory named .git
, which will contain all the details about your project version control. You can also type git status
to get the status of the project version control, as shown in Figure C.2 (bottom). Each Git project is called a repository.
Now you can add your Java programs to your project, an operation called staging. Let's use the HelloWorld.java
program shown in Example 2.1 in Chapter 2 as an example. Use Windows Explorer to add the HelloWorld.java
program into your project directory at E:MyProject
. It is always useful to have a README
file in your project to explain what your project is about. You can either use a text editor to create a text file named README.md
(not README.md.txt
) in your project directory or simply type the following command in the Git Bash window:
echo "# MyProject" >> README.md
To add files to Git version control, type the following command in the Git Bash program:
git add .
This will add all your files in the current directory into the Git version control; see Figure C.3 (top). The git add .
command does not add subdirectories; so, to add everything, including files and subdirectories, type
git add --all
or
git add -A
You can also use the git rm <file name>
or git reset <file name>
command to remove any files from the version control. When you are happy with your project, you can commit your project, which will save the changes in Git, as shown in Figure C.3 (bottom). You can commit as many times as you like. You can also use git log
to trace all the changes, and you can use the git reset --soft HEAD~1
command to revert the commit.
To share the project with others, you will need a remote version control server; the obvious choice is GitHub, one of the most popular web-based version control hosting servers. To use GitHub, just go to its website (https://github.com/
) and create an account on GitHub (Figure C.4A). After you sign up and log in, click the Start A Project button and create a new repository for your project (Figure C.4B). Give your project a name, and use all the default settings to create the repository (Figure C.4C). A new Quick setup page will appear, from which you can find the HTTPS link address to your new repository. In this example, it is as follows:
https://github.com/PerryXiao2015/MyProject.git
If you want to use an SSH link, it will be as follows:
[email protected] :PerryXiao2015/MyProject.git
You can set your GitHub link to your local Git origin and upload, or push, your local repository to the GitHub remote repository, using the following commands:
git remote add origin https://github.com/PerryXiao2015/MyProject.git
git push –u origin master
or
git remote add origin [email protected]:PerryXiao2015/MyProject.git
git push –u origin master
Before you can push your local repository to the GitHub remote repository, you need to make sure the contents will be encrypted, by generating a public/private key pair. Figure C.5 (top) shows how to generate an RSA key pair. (The algorithm is named after its creators Rivest Shamir Adleman.) RSA is a public-key encryption algorithm that is widely used for secure data transmission. You can also use different algorithms to generate a public/private key pair. More details about encryption, RSA, and public/private key pairs are available in Chapter 9. The key pair generated is stored in the .ssh
hidden directory. By default, the private key is stored in a file named id_rsa
, and the public key is stored in a file named id_rsa.pub
. Figure C.5 (bottom) shows how to display the .ssh
hidden directory and the public key. The content of the public key should start with ssh-rsa
.
Copy the public key, and then go to the GitHub website, select Deploy Keys from the project settings, and add a new key. Give it a name, paste the public key into the key content, and click the green Add Key button, as shown in Figure C.6.
Figure C.7A shows how to add your GitHub project site and how to push the local Git repository to the remote GitHub repository. A GitHub login window will appear, as shown in Figure C.7B. Type in your GitHub login username and password, and your GitHub project site will be updated with your files, as shown in Figure C.7C.
Now, let's modify the HelloWorld.java
program to make it like Example 2.2, with args[0]
used. If you run the git status
command, it will show that the HelloWorld.java
program has been modified by displaying it in red (Figure C.8). The red color markup here indicates that the changes have not been staged to commit. You need to use the git add .
or git add -A
command to add changed files to the local repository. If you run git status
again, it will show that the HelloWorld.java
program has been modified by displaying it in green. The green color markup here indicates the changes are ready to commit.
You can commit the program again and push it to the GitHub master repository. In Figure C.9, you can see that the files in the GitHub site have been updated accordingly. The GitHub web site also shows how many commits have been made, along with details and corresponding files of each commit. Each commit represents a different version of your project code.
You can also use the git log --online
command to show the history of commits from the Git Bash window, as shown in Figure C.10 (top). Each commit has a unique number, presented in hexadecimal format. If a disastrous event happens, you can always revert to the previous version of a program by using the git checkout <unique number>
command, as shown in Figure C.10 (bottom). Now your local repository files will revert to the previous version.
The following is a list of Git terms.
Table C.1 is a cheat sheet summarizing the Git commands.
Table C.1: Git Command Cheat Sheet
COMMAND CATEGORY | COMMAND | WHAT IT DOES |
Configuration | git config --global user.name "Your_Name" |
Sets up your name in Git |
git config --global user.email " [email protected] " |
Sets up your email address in Git | |
git config --list |
Lists your Git configuration | |
Starting a Repository | git init |
Initializes Git |
git status |
Gets the status of Git | |
Staging Files | git add <filename> <filename> … |
Adds files to the local repository |
git add . |
Adds all files in the current directory (subdirectories not included) to the local repository | |
git add –all |
Adds all files in the current directory and subdirectories to the local repository | |
git add –A |
Adds all files in the current directory and subdirectories to the local repository | |
git rm <filename> |
Removes a file from the local repository | |
git reset <filename> |
Removes a file from the local repository | |
Generating an SSH Public/Private Key Pair | ssh-keygen -t rsa -b 4096 -C " [email protected] " |
Generates an RSA key pair |
ls –la ~/.ssh |
Lists the content of the .ssh hidden directory |
|
cat ~/.ssh/id_rsa.pub |
Displays the content of the RSA public key | |
Committing to a Repository | git commit –m "Your message here" |
Saves changes to the local repository |
git commit --amend -m "Your amend message here" |
Modifies the commit | |
git reset --soft HEAD~1 |
Reverts the commit | |
git log |
Displays the history of commits | |
Pushing and Pulling from GitHub | git remote add origin <your GitHut link> |
Sets your GitHub link to the local Git origin |
git push –u origin master |
Uploads the local repository to the GitHub remote repository | |
git clone <project name> |
Gets a copy of an existing Git repository | |
git pull |
Fetches and downloads content from a remote repository and immediately updates the local repository to match that content | |
git log --oneline |
Displays the history of the remote repository commits | |
git remote rm origin |
Removes the remote origin | |
Branching | git branch |
Creates a version of the repository that diverges from the main working project |
git branch <branch name> |
Creates a version of the repository with a given name | |
git checkout <branch name> |
Sets the current branch as the main working project | |
git merge <branch name> |
Merges changes from one branch into another |