Heart containing Coding Chica Java 101

Hello World! – Now Let’s Commit

TIP: References Quick List

Additional Info:

Now that we have a starting code base, let’s commit it to a git repository for safe keeping.

Confirm Git is on the Path

Issue a command like the following to ensure that git is on your path:

git version

# Actual output will vary
# Example output:
# git version 2.40.0.windows.1

Initialize the Local Git Repo

From within the java101 directory, issue the following command:

git init

# Example output:
# Initialized empty Git repository in C:/Users/codingchica/devProjects/java101/.git/

Checking Git Status

Issue the following command to see what files (git ignores empty folders) are available to commit:

git status

# Example output:
# On branch master
#
# No commits yet                                                  
# 
# Untracked files:                                                
#  (use "git add <file>..." to include in what will be committed)
#        .idea/ 
#        pom.xml
#        src/   
#        target/
#
# nothing added to commit but untracked files present (use "git add" to track)

If using IntelliJ, the files in our project should also appear in red to show that they are not in version control (now that git has been initialized).

Telling Git Which Files to Ignore

Files about your local IDE configuration should not be committed to version control. Let’s add them to a new file to tell git to ignore them.

From IntelliJ, right click on the java101 project name -> New -> File.
New File Name: .gitignore
If prompted, you can add this file to git.
Add the following to the new .gitignore file:

# IntelliJ configuration files
/.idea/

# Eclipse configuration files
/.settings/

# Maven build files
/target/

If you chose a different IDE, the folder name you provide may differ from the examples provided above. The # in the code above signals a comment line, so they are ignored.

Adding files to Git

Add the files that we want to commit to Git version control:

git add pom.xml
git add src
git add .gitignore

Alternatively, you can add an individual file to git in IntelliJ via right click on file -> Git -> add.

At this point, the git status command should return something like:

git status

# Example output:
# On branch master
#
# No commits yet
# 
# Changes to be committed:
#  (use "git rm --cached <file>..." to unstage)
#        new file:   .gitignore
#        new file:   pom.xml
#        new file:   src/main/java/codingchica/java101/App.java
#        new file:   src/test/java/codingchica/java101/AppTest.java

Commit Locally

Issue a command like the following to commit the added files to the repository:

# -m = message
# "Initial commit" is the commit message that will be used

git commit -m "Initial commit"

Change Local Branch Name

Let’s pick a branch name that doesn’t have heavy associations. A common choice is main as in the main trunk of a tree.

git branch -M main

Optional: Create a Remote GitHub Repository

If you choose to store the code base in a remote GitHub repo, let’s create it now.

Optional: Push the Local Changes to Remote GitHub Repository

If you chose to store the code base in a remote Git repo, let’s push our local changes up to the remote repo for safe keeping:

# Tell the local git where to find the remote git repository
# You could also use the ssh option, but those instructions aren't included in this post.
git remote add origin https://github.com/<yourUserIdHere>/java101.git

# push the local branch to git remote
git push -u origin main

# If you haven't already setup git username / password locally, it will prompt you for your username (AKA yourUserIdHere value from above), and password (See https://github.com/settings/tokens to generate a new personal access token)

Example Code

You can find the Hello World! example code base at GitHub: codingchica/java101: hello_world tag

Hello World! – Now Let’s Commit

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.