Git Basic Commands
Now that we have a project and a git
repository let's start using it. Here are the basic commands you'll be using this semester.
Git Commands Summary
$ git status
$ git add ...
$ git commit ...
$ git push
$ git status
Use this command to see what git sees as the changes to your project. You should use this more than any other command.
- Examples:
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
$ git add <filepath>
We have to add files to git
after we add them to our project. First use git status
to see all new files. Then use the add command.
- Examples:
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
src/java112/labs1/JustADemo.java
no changes added to commit (use "git add" and/or "git commit -a")
git add src/java112/labs1/JustADemo.java
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: src/java112/labs1/JustADemo.java
$ git commit ...
Notice that is says Changes to be committed:
? Any time we change something in our project we have to commit. The changes can be new files or changes to existing files. This is the habit we have to into. Here's how.
All the pieces of this command are required for it to work correctly. Here's the breakdown.
git commit
: This is the command and it requires more information.-m
: This stands for "message". With them
you have to have set of double quotes with a commit message. The message is for your teammates and needs to say what was done in this commit."Initial commit of my semester project."
: This will be your message. It does not need to be long but it must state generally what was done in your project since the last commit. The first time we commit a project we usually mention something like "Initial commit".- Example:
$ git commit -m "Adding a file to demonstrate an add. Modifying a file to demonstrate a change."
[master a28a889] Adding a file to demonstrate an add. Modifying a file to demonstrate a change.
2 files changed, 30 insertions(+), 1 deletion(-)
create mode 100644 src/java112/labs1/JustADemo.java
After a commit you should do a git status
to verify everything was committed.
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
Notice that the status message says Your branch is ahead of 'origin/master' by 1 commit.
This means it's time for a push.
$ git push
A commit is local to your machine, it does not save your repository to GitHub. You need to do that step with a push
. The way we have set things up you only need to enter $ git push
.
$ git push
Counting objects: 7, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (7/7), 835 bytes | 0 bytes/s, done.
Total 7 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To git@github.com:madcol-advjava-s18/projects-<account>.git
20eeafd..4076ab2 master -> master
This is good!
Tips
- Use git status often!
- Add, commit, push each time you have a bit of functionality working. This will often be just a few lines of code.
- You can combine the add and commit into one step:
$ git commit -am "Adding Lexical Density Analyzer"
CAUTION $ git commit -am
only adds files that git is already tracking to the staging area. If you created a brand new file, you must use git add .
When in doubt, use git status
to understand the state of your files.