A tiny exercise in git
- Install git
 - Create a github.com account
 - Create a new hello world project in IDEA (main class sout("hello world"))
 - See if 
git --helpworks. See ifgit commit --helpworks. Should work for all commands. - Move the command line to your project root 
cd "/path/to/repo" - Start a git repo in project root with 
git init - We don’t want to version control IDE files and compiled code. Add a .gitignore file to your repo root with the following lines: 
.idea
*.iml
out - Commit only .gitignore
- Add .gitignore to staging 
git add .gitignore - Use 
git statusto verify no other files are staged - Commit 
git commit -m "added gitignore" 
 - Add .gitignore to staging 
 - Commit all other files
- Add files 
git add . - Make sure ignored files are not staged 
git status - Commit 
git commit -m "added code" 
 - Add files 
 - Use git log to see what’s up 
git log - Change "hello world" to "goodbye world", commit 
git add .+git commit -m "goodbye" - Use git log to see what’s up 
git log - Create a repo in github
 - Push your project to github repo
- Add the github remote to your project 
git remote add origin <repo url>
A remote is basically an alias for some repo url - Push to your github repo 
git push origin master
Here origin is the repo alias, master is the branch name - Visit github to verify the files are there
 
 - Add the github remote to your project 
 - Add your friend as a contributor to your project ((s)he’ll need edit permissions)
 - Have your friend change your code
- Let him 
git clone <github repo url>
This will create a local copy of the repo from github. NB! don't clone a repo into another repo - runcd ..before or move whereever you want the repo cloned to. - Let him change "goodbye world" to "friend was here"
 - Let him commit and push (remote origin is already added by clone command)
 - Ensure his change is visible in github
 
 - Let him 
 - Pull the changes to your local repo 
git pull origin master
If you get tired of writing out origin master, usegit branch --set-upstream-to=origin/master master - Change "friend was here" to "hello conflict", commit, don’t push
 - Have your friend change "friend was here" to "friend still here", commit, push
 - Pull changes from github. Different people have changed the same lines, so you now have a merge conflict: 
CONFLICT (content): Merge conflict in src/Hello.java. Yay! - Let's get some help from IDEA
- Open your main class. You'll see some conflict markers >>>>>>>, ======= etc.
 - From VCS menu "Enable version control integration" -> git
 - Use VCS -> Git -> Resolve conflicts
 - Select your main class and click the MERGE button
 - The three-way-merge editor will show you your old code, your friend's code and the result in the middle. Tweak the middle code so you're happy with the result, then click Apply.
 - Merge all the files if you have more. Trivial changes are merged automatically.
 - Make sure there were no mistakes. Use Build -> Rebuild project and see if it passes.
 
 - Now the code is merged and good to go. See 
git statusand make sure git says "All conflicts fixed". Usegit addto add more files if needed. Commit the merged code. - Check 
git log --oneline --graphto see what's up - Delete your main class
 - Whoops, why did you do that?! We still need that. In case of major screw ups, run 
git reset --hard HEAD. THIS WILL DELETE ALL UNCOMMITED CHANGES and revert back to the last commited state. Try it now. Main should be back. - Let's go back in time
- If you have any uncommited changes, use 
git stashto stash them away - Use 
git logto find the first commit's id (the big long hash thingy) - Use 
git checkout <commit id>
Yay, our old code! Take a moment to admire it in IDEA - To go back to the latest commit 
git checkout master - Restore your stashed changes 
git stash pop 
 - If you have any uncommited changes, use 
 - If you mess up a merge, delete all your code and delete your branches, then 
git reflogmight save your ass. Hope you won't need it any time soon. - Remember all this stuff for your group project
 - Read about git interactive rebase at home