Java / Git

Solutions

1. First Java Project

public class Application {
   public static void main(String args[]) {
       System.out.println("Hello World");
   }
}

2. Initialize Git Repository

cd directory/of/my/project/helloworld
git init
ls -la
git status
git status -s
echo "out" >> .gitignore     # this appends out to the .gitignore file
echo ".idea" >> .gitignore   # this appends .idea to the .gitignore file
echo "*.iml" >> .gitignore   # this appends *.iml* to the .gitignore file
git status
git status
git add .gitignore src             # add files to the staging area
git status
git commit -m "Initial commit"     # commit the new version
git status

3. Making Another Commit

git add first.txt
git commit -m "Added first.txt"
git status

4. Inspecting the log

git log
git log --oneline -1
git log -1 -p

5. Adding and Removing

git add first.txt second.txt
git status
git commit -m "Added second.txt and removed first.txt"
git status
git log

6. Partially Staged Files and Diff

git status
git add third.txt
git status
git commit -m "Added text to third.txt"
git status
git diff third.txt
git add third.txt
git commit -m "Added more text to third.txt"
git status
git log

7. Branching and Merging

git log --oneline
git branch
git branch testing
git branch
git checkout testing 
git branch
git add third.txt
git commit -m "Added even more text to third.txt"
git log --oneline
git checkout master
git log --oneline
git add LICENSE
git commit -m "Added Added LICENSE file"
git merge testing
git branch -d testing 

8. Handling Merge Conflicts

git branch testing
public class Application {
    public static void main(String[] args) {
        System.out.println("Thank you for using the Hello World App");
    }
}
git add Application.java
git commit -m "Added a welcoming message"
git checkout testing
public class Application {
    public static void main(String[] args) {
        System.out.println("Hello World");
        System.out.println("Version 1.0");
    }
}
git add Application.java
git commit -m "Added the version number"
git checkout master
git merge testing
public class Application {
    public static void main(String[] args) {
        System.out.println("Thank you for using the Hello World App");
        System.out.println("Version 1.0");
    }
}
git status
git add src/Application.java
git status
git commit -m "Merge branch 'testing'"   # commit resolved conflicts and end the merge
git status
git log --oneline
git branch -d testing

9. Using Remotes

git remote add origin git@github.com:yourusername/helloworld.git
git remote -v
git branch
git push origin master
git branch -u origin/master 
git push
git fetch
git merge origin/master

10. Git Clone and Import

git clone git@github.com:arestivo/helloworld.git
cd helloworld
ls -la