These are my personal notes on using Git. When working in Java, I develop with Intellij Idea. I have a revision control server that I use to track revisions and backup code. It’s a running document. I’ve published it in case it might answer someone else’s questions from time to time.
Starting a new project
On server housing the repository:
- create new project directory
- enter that directory and run “git init –bare”
- cd my_project
- git init
- git add .
- git commit -m “My initial commit message”
- git remote add origin email@example.com:my_project
- git push -u origin master
This is really a two step process. Git uses a “staging area” to hold what you want to commit. It does this so that you can commit only a subset of the files (or even a subset of the data) that you’ve changed to a single commit. That way you organize your changes into discrete commits.
So basically you do a “git add” on the files you want to commit, then you do a “git commit” to commit them.
Intellij Idea does this in one sweep when you choose “commit” in the IDE. You can do it all at once from the command line with “git commit -a”.
General working model:
Start a new branch when you start working on something new. Once complete, switch to the master branch and “merge” it in. If you want your branch to be saved on the remote (I typically do), then be sure you push that branch to a remote branch of the same name, otherwise changes to that branch won’t go to the remote when you “push”. This can be done right in Intellij with an option in the commit window.
The ‘git clone’ command automatically sets up a ‘master’ branch that is a tracking branch for ‘origin/master’ – the master branch on the cloned repository.
Q. How can I see the detail of a tag (a full tag, not a “short” tag?
A. git tag -v [TAGNAME] such as “git tag -v v1.0”
Q. How can I send my tags to the remote?
A. git push –tags
Q. What can’t I see my tags in the log/graph in Intellij?
A1. Tags don’t show up properly until you reload your project (at least in 11.1.1 Build #IC-117.117)
A2. If the entry already has several “boxes” on it (such as [master][origin/master], …etc) the tab might not have room to show up. You’ll see it if you click on the entry.
Q. How do I undo the uncommitted changes I’ve made in my current branch?
A. git reset –hard HEAD
Q. How can I recommit/modify what I just committed? I.E. – I meant to do one more thing before I committed but forgot?
A. Use the –amend flag on the commit, it will replace the head commit with a new one.
Q. How can I delete a tag?
A. “git tag -d [TAGNAME]” will delete it locally. Then “git push origin :refs/tags/[TAGNAME]” will delete if off the remote.
Q. How can I get rid of my last commit?
A. “git reset –hard HEAD~1”, then if you want to push that to the server “git push origin HEAD –force”
Q. How can I rename a branch?
A. git branch -m old_branch new_branch
Q. How do I push a new branch to the remote?
A. git push origin branchname
Q. How can I delete a branch?
A. To delete it on the remote – ‘git push origin :branchname’ and then locally – ‘git branch -d branchname’
Q. How do I “check out” a project to a new directory?
A. You clone the repository with ‘git clone firstname.lastname@example.org:my_project’