-
Visual Studio Code
Download -
Extensions (optional)
GitHub Pull Requests and Issues
Git Blame
Git Graph
Git History
.gitignore
Go to File -> Preferences
Go to Settings
Type Git: Enabled in the search bar
Make sure that the box is ticked
Provide your email:
git config --global user.email "[email protected]"
Provide your git user name:
git config --global user.name "your-name"
Create the directory using mkdir and go into that directory using cd.
mkdir testjava1
cd testjava1
Similar to the step above, create the subfolders called src and bin.
Afterwards, change directory into the src subfolder.
mkdir src
mkdir bin
cd src
Similar to the steps above, create the subfolders called ie and
within ie create another subfolder called tudublin.
mkdir ie
cd ie
mkdir tudublin
Using file manager, go to the subfolder tudublin, right click
and choose New => Text Document and name it Main.Java. Your
file path should look like this:
Alternatively, you can use the command prompt to create a file within that folder.
cd tudublin
nul > Main.java
Also note that under the View tab in the menu bar, ensure that
File name extensions and Hidden Items are ticked.
package ie.tudublin;
public class Main
{
public static void main(String[] args)
{
System.out.println("Every little cell in my body is happy");
}
}Locate your root folder (in this case the testjava1 folder) and right
click and then choose Git Bash Here, a window similar below
should pop up:
First, initalize empty Git repository locally.
git init
You will now notice under your root folder through file manager
that there is a new hidden folder called .git. This is where git will store all the previous snapshots of the previous versions your project.
In your profile page, go to the Repositories tab and create a new repository by choosing New.
When creating the repository, the name will be the name of the folder that will get cloned, so it would make sense that it would match the name of your project folder in your local system. (However, you can call it whatever you like).
Then under the Description, it is highly recommended to type something in as this will be your README.md (markdown file).
You can choose whether to set this repository Public or Private.
Then, select all options in the next section.
The purpose of the .gitignore is to hide selected files (untracked) when uploading to the github server. We would only want source codes to be uploaded. If we choose to not implement a .gitignore file, our repository size would be large and merge conflicts. In this case, choose the Java template in the dropdown menu.
For Java projects, we would choose to hide .class files as they are the output of the build.
For C projects, we would choose to hide .exe files and .obj files.
Choose the MIT licence (permissive licence).
Your page should now look like this:
Choose Create repository:
Copy the link of your Github repository, it should look like this:
https://github.com/YOURGITUSERNAME/testjava1
Return to your Git bash and make sure the path you're currently in is the root folder of your project. Create link between your local repository and Github repository by inputting the line below (to paste in Windows, use right click):
git remote add origin https://github.com/YOURGITUSERNAME/testjava1
To check your remote repository in Git (repository that's hosted on the Internet or another network), type the following:
git remote -v
Then something like this should pop up:
Before heading on to the next step, it is important to check the default branch of your repository. In order to do so, go to your repository's settings then branches.
It should display the name of your default branch of your repository (either master or main).
Before doing a commit on your local system, first retrieve the files created on your Github repository, in this case we expect to retrieve .gitignore, LICENSE, and README.md.
When running a git pull for the first time on your repositories, ensure to type in the following to associate the branch from your Github to your local system. Origin is the Github url alias.
If your default branch is called master:
git pull origin master
If your default branch is called main:
git pull origin main
This essentially means: Pull from the origin upstream, master/main branch, into my master/main branch.
git remote set-url origin https://github.com/YOURGITUSERNAME/testjava1
Open your project folder in Git Bash
First add everything under the current directory to the staging area that is tracked (i.e., not in the .gitignore file).
git add .
The period . means everything under the current directory.
Then, commit everything that is under the staging area and save into the logs of commit.
git commit
Executing this will open up your editor and prompt for a comment. Once done, save the file and close it.
Alternatively, you can add your message in the command itself:
git commit -m "commit message"
To commit all changes:
git commit -a -m "commit message"
For first time commits (and also on new branches), associate your local branch to the branch from your server repository.
git push --set-upstream origin master
Thereafter, everytime a push is made, you can simply type the command like so:
git push
This essentially shows the changes of your repository.
git status
Merge a branch to the current branch (i.e., merge someBranch to master):
git checkout master
git merge someBranch
A merge conflict should look like this:
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
Open the conflicted file in your chosen editor (in this case Visual Studio Code). You should see something similar to this:
Choose which change to apply, save the file, then add the changes to the staging area, and commit changes.
If you wish to save the changes from a different branch:
git checkout --theirs test.bmp
If you wish to save the changes from your current branch:
git checkout --ours test.bmp
Then commit changes.
Show the log of all commits on your current branch:
git log
To rollback to a previous commit, select the 40 digit hexadecimal number by double clicking and pressing enter. Then, press q to exit the log and input the following (without the brackets):
git checkout <your40DigitHexadecimal>
This would rollback the system to that particular ccommit.
By doing this, you would now be in a detached-head state. Meaning no changes/edits will be saved. If you wish to keep the changes within this state, you would have to commit them into a new branch.
If you wish to go back to a branch, simply input:
git checkout <branchName>
Temporarily saves changes (like commits) into a list (aka stash) local to your system:
git stash
Give the stash a name:
git stash save <message>
View the list of changes:
git stash list
Take most recent commit and add new staged changes:
git commit --ammend
Change most recent Git commit message:
git commit --amend -m "message here"
To create a branch:
git checkout -b <someBranch>
Every changes from there on will be saved on that branch. Remember to associate the local branch to the server branch on initial commits.
To return or change to a different branch:
git checkout <branchName>
To delete a branch:
git branch -d <branchName>
To force delete a branch:
git branch -D <branchName>
To check list of branches (local):
git branch
To check list of branches (remote):
git branch -r
To clone a repository to your local system:
git clone https://github.com/YOURGITUSERNAME/REPONAME
To set the upstream remote:
git remote add upstream https://github.com/ORIGINALGITUSER/REPONAME
Note that origin is the alias of your local repo, while upstream is the original source of the repo.
To update your local master branch to the upstream master branch:
git fetch
git checkout master
git pull upstream master
git push
git rm --cached <folderName>
If your GitHub repository shows this icon:
Try
git add <folderName>
If results in an error such as:
xxx submodule xxx
Clear Git cache and try adding again













