The entire project, minus files that Maven generated.
Install Git.
You can use the "
git init" command to set up your project folder as a local Git repository, but you'd still have to configure URLs so that you can push your repository to GitHub. Instead, I think there's a quicker and easier way:
Create a new public repository on your GitHub account. Initialize the repository with a
.gitignore file that uses the Maven template.
After you've created the repository, clone it on your local file system. In your repository on GitHub, under the green "Code" button, you can find a URL for your repository that starts with
https:// and ends with
.git. Copy this URL and then run the following command from your desktop folder:
A folder will be created on your desktop that has the same name as your repository on GitHub. Inside it should be a hidden
.git folder, and a
.gitignore file. Move them to the project folder that you were already working on. You can now delete the empty folder on your desktop. Don't worry that the name of your project folder doesn't match the name of your repository on GitHub; Git doesn't care.
The .git folder contains all the metadata that Git needs to keep track of the state of your repository. Never touch this folder. Not only does it contain your repository configuration, but also old versions of your code that you can revert back to when you've messed up.
The
.gitignore file tells Git which files it must ignore. A Git repository should only contain source/resource files that were not created by the build process. A Maven build puts all files it generates in the
target/ folder of your project folder, so the Maven template you used when you created your repository should already contain a line that tells Git to ignore the
target/ folder.
Getting your changes from your local repository to your GitHub repository is a three step process:
1) Stage changes you want to commit. Your local repository contains a staging area (better known as the "index") that contains all changes you've made to files that you are preparing to commit.
2) Commit staged changes. Git will create a commit that contains all staged changes, and add the commit to the current branch. The changes are now saved forever*. Even if you add more commits, you can always return to older commits to retrieve that version of your code.
3) Push commits to the remote server. When you've made one or more commits, they're not automatically synchronized to GitHub. You must do so explicitly.
Here's how you perform step 1). Run all commands from your project folder:
The "
git status" command will show you the current state of your working directory. Initially, it will only show you untracked folders and files, because the only file that Git tracked up to this point was the
.gitignore file.
The "
git add ." command will add all changes you've made in the current directory to the staging area recursively. If you want, instead of using "
." you can also just add individual files or folders.
After you've performed an action with Git, always run "
git status" again to see what your working directory looks like right now. You will see that all files you've staged are now in the index. Ensure that the index doesn't contain any files that are inside the
target/ folder.
Step 2):
Git creates a commit of all staged changes using the specified commit message, and adds the commit to your current branch. When you check the status of the working directory, you will see that your index is now empty once again, and that your current branch is one commit ahead of the origin.
Step 3):
This pushes all your local commits to the remote server and updates the origin branch. All your committed changes will appear on GitHub.
*: "Forever" means as long as you don't touch the
.git folder, and as long as you don't use
-f or
--force options with
git commands. Git makes it very hard for you to lose committed changes. Even if you've deleted all source files in your repository, or if you've used a bunch of
git commands wrongly and completely messed up your working directory, an experienced Git user can always revert your working directory to a previously known state. A common mantra among Git users is "Commit early, commit often".