A tiny exercise in git
- Read https://guides.github.com/introduction/git-handbook/
- Install git
- Create a github.com account
- Create a new hello world project in IDEA
- Open the command line. See if
git --help
works. See ifgit commit --help
works. Help exists for all commands. - Navigate to your project root
cd "/path/to/project"
- 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 this content
- Commit only .gitignore
- Include .gitignore in the next commit
git add .gitignore
- Use
git status
to verify no other files are staged - Make a commit
git commit -m "added gitignore"
- Include .gitignore in the next commit
- Commit all other files
- Include all files in the next commit
git add .
- Make sure ignored files are not staged
git status
- Make a commit
git commit -m "added code"
- Include all files in the next commit
- Use git log to see the history
git log
- Change the code to print "goodbye world" instead of "hello world". Commit the change:
git add .
+git commit -m "goodbye"
- Use
git log
to verify the commit was created - Create a new public repository in github
- Push your project to the new github repository
- Add the github remote to your project
git remote add origin <repo url>
. A remote is basically an alias for a repository url. - Push your commits to the github repository
git push origin master
. Hereorigin
is the repo alias andmaster
is the branch name. - Visit github to verify that the files are there
- Add the github remote to your project
- Add a friend as a collaborator to your project. If you have no friends, ask the instructor to fill in 🐧
- Have the friend change your code:
- Clone the repository with
git clone <github repo url>
This will pull a copy of the repo from github and store it locally. Don't run the clone command inside another repository directory. - Change "goodbye world" to "friend was here"
- Commit and push (remote is already configured by clone command)
- Ensure the change is visible in github
- Clone the repository with
- Pull the changes to your local repo
git pull origin master
- Change "friend was here" to "hello conflict". Commit the change, but don’t push yet.
- Have the friend make another change: change "friend was here" to "friend still here", commit and push.
- Pull changes from github again. Since different people have changed the same lines, git will be unable to automatically merge the changes. This is called a merge conflict. The pull will show
CONFLICT (content): Merge conflict in src/Hello.java
. - 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 status
and make sure git says "All conflicts fixed". Usegit add
to add more files if needed. Commit the merged code and push it. Verify that it's in github. - Check
git log --oneline --graph
to 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 stash
to stash them away - Use
git log
to find the first commit's id (the big long string of random characters) - Use
git checkout <commit id>
. This will restore the old code. Take a moment to admire it in IDEA. - To go back to the latest commit
git checkout master
. - Restore your stashed changes (if any) with
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 reflog
might 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 if you're bored.