Sepm Exp3
Sepm Exp3
Sepm Exp3
Git Commands
Git provides a set of simple, distinct, standalone commands developed according to the "Unix
toolkit" philosophy - build small, interoperable tools.
To issue a command, start a "Terminal" (for Ubuntu/Mac) or "Git Bash" (for Windows):
We shall begin with "Starting your own project" and cover "Cloning" later @ "Clone a Project
from a Remote Repo".
Let's start a programming project under the working directory called "hello-git", with one source
file "Hello.java" (or "Hello.cpp", or "Hello.c") as follows:
// Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world from GIT!");
}
}
Compile the "Hello.java" into "Hello.class" (or "Hello.cpp" or "Hello.c" into "Hello.exe").
It is also highly recommended to provide a "README.md" file (a text file in a so-called
"Markdown" syntax such as "GitHub Flavored Markdown") to describe your project:
// README.md
This is the README file for the Hello-world project.
Now, we have 3 files in the working tree: "Hello.java", "Hello.class" and "README.md". We
do not wish to track the ".class" as they can be reproduced from ".java".
$ ls -al
drwxr-xr-x 1 xxxxx xxxxx 4096 Sep 14 14:58 .git
-rw-r--r-- 1 xxxxx xxxxx 426 Sep 14 14:40 Hello.class
-rw-r--r-- 1 xxxxx xxxxx 142 Sep 14 14:32 Hello.java
-rw-r--r-- 1 xxxxx xxxxx 66 Sep 14 14:33 README.md
A hidden sub-directory called ".git" will be created under your project root directory (as shown
in the above "ls -a" listing), which contains ALL Git related data.
Take note that EACH Git repo is associated with a project directory (and its sub-directories). The
Git repo is completely contained within the project directory. Hence, it is safe to copy, move or
rename the project directory. If your project uses more than one directories, you may create one
Git repo for EACH directory, or use symlinks to link up the directories, or ... (?!).
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
Hello.class
Hello.java
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: Hello.java
new file: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
Hello.class
The command "git add <file>..." takes one or more filenames or pathnames with possibly
wildcards pattern. You can also use "git add ." to add all the files in the current directory (and all
sub-directories). But this will include "Hello.class", which we do not wish to be tracked.
When a new file is added, it is staged (or indexed, or cached) in the staging area (as shown in the
GIT storage model), but NOT yet committed.
22. Login to the GIT host and select the remote repo "test", you shall find all the committed
files.
23. On your local system, make some change (e.g., on "Hello.java"); stage and commit the
changes on the local repo; and push it to the remote. This is known as the
"Edit/Stage/Commit/Push" cycle.
24. // Hello.java
25. public class Hello {
26. public static void main(String[] args) {
27. System.out.println("Hello, world from GIT!");
28. System.out.println("Changes after First commit!");
29. System.out.println("Changes after Pushing to remote!");
30. }
}
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
$ 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)
modified: Hello.java
// EXAMPLES
// ========
// Change directory (cd) to the "parent" directory of the project directory
$ cd path-to-parent-of-the-working-directory
// Clone our remote repo "test" into a new working directory called "hello-git-cloned"
$ git clone https://github.com/your-username/test.git hello-git-cloned
Cloning into 'hello-git-cloned'...
remote: Counting objects: 13, done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 13 (delta 2), reused 13 (delta 2)
Unpacking objects: 100% (13/13), done.
Checking connectivity... done.
// Verify
$ cd hello-git-cloned
$ ls -a
. .. .git .gitignore Hello.java README.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
The "git clone" automatically creates a remote name called "origin" mapped to the cloned
remote-URL. You can check via "git remote -v":
// List all the remote names
$ git remote -v
origin https://github.com/your-username/test.git (fetch)
origin https://github.com/your-username/test.git (push)
3.4 Summary of Basic "Edit/Stage/Commit/Push" Cycle
// Edit (Create, Modified, Rename, Delete) files,
// which produces "unstaged" file changes.
// Push
$ git push <remote-name> <local-branch-name>
OR,
OR,
$ git push
Before the changes are committed, suppose we modify the file again:
// README.md
This is the README file for the Hello-world project.
Make some changes and staged.
Make more changes before the previous changes are committed.
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
modified: README.md
$ git diff
diff --git a/README.md b/README.md
index b2e9afb..ca6622a 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,4 @@
// README.md
This is the README file for the Hello-world project.
Make some changes and staged.
+Make more changes before the previous changes are committed.
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
modified: README.md