0% found this document useful (0 votes)
32 views9 pages

Sepm Exp3

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

EXPERIMENT NO.

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):

$ git <command> <arguments>

The commonly-used commands are:


1. init, clone, config: for starting a Git-managed project.
2. add, mv, rm: for staging file changes.
3. commit, rebase, reset, tag:
4. status, log, diff,, show: show status
5. checkout, branch, merge, push, fetch, pull

Help and Manual


The best way to get help these days is certainly googling.
To get help on Git commands:

$ git help <command>


// or
$ git <command> --help
The GIT manual is bundled with the software (under the "doc" directory), and also available
online @ http://git-scm.com/docs.

3.1 Getting Started with Local Repo


There are 2 ways to start a Git-managed project:
1. Starting your own project;
2. Cloning an existing project from a GIT host.

We shall begin with "Starting your own project" and cover "Cloning" later @ "Clone a Project
from a Remote Repo".

Setup the Working Directory for a New Project

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".

Initialize a new Git Repo (git init)


To manage a project under Git, run "git init" at the project root directory (i.e., "hello-git") (via
"Git Bash" for Windows, or "Terminal" for Ubuntu/Mac):
// Change directory to the project directory
$ cd /path-to/hello-git

// Initialize Git repo for this project


$ git init grep
Initialized empty Git repository in /path-to/hello-git/.git/

$ 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 Storage Model


The local repo after "git init" is empty. You need to explicitly deposit files into the repo.
Before we proceed, it is important to stress that Git manages changes to files between so-called
commits. In other words, it is a version control system that allows you to keep track of the file
changes at the commits.

By default, we start on a branch called "master". We will discuss "branch" later.


In Git, the files in the working tree are either untracked or tracked. Currently, all 3 files
are untracked. To stage a new file for tracking, use "git add <file>..." command.
// Add README.md file
$ git add README.md

$ 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

// You can use wildcard * in the filename


// Add all Java source files into Git repo
$ git add *.java

// You can also include multiple files in the "git add"


// E.g.,
// git add Hello.java README.md

$ 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.

3.2 Setting up Remote Repo


1. Sign up for a GIT host, such as Github https://github.com/signup/free (Unlimited for
public projects; fee for private projects); or BitBucket @ https://bitbucket.org/ (Unlimited
users for public projects; 5 free users for private projects; Unlimited for Academic Plan);
among others.
2. Login to the GIT host. Create a new remote repo called "test".
3. On your local repo (let's continue to work on our "hello-git" project), set up the remote
repo's name and URL via "git remote add <remote-name> <remote-url>" command.
By convention, we shall name our remote repo as "origin". You can find the URL of a
remote repo from the Git host. The URL may take the form of HTTPS or SSH. Use
HTTPS for simplicity.
4. // Change directory to your local repo's working directory
5. $ cd /path-to/hello-git
6.
7. // Add a remote repo called "origin" via "git remote add <remote-name> <remote-url>"
8. // For examples,
9. $ git remote add origin https://github.com/your-username/test.git // for GitHub

$ git remote add origin https://username@bitbucket.org/your-username/test.git // for


Bitbucket
You can list all the remote names and their corresponding URLs via "git remote -v", for
example,
// List all remote names and their corresonding URLs
$ git remote -v
origin https://github.com/your-username/test.git (fetch)
origin https://github.com/your-username/test.git (push)
Now, you can manage the remote connection, using a simple name instead of the complex
URL.
10. Push the commits from the local repo to the remote repo via "git push -u <remote-name>
<local-branch-name>".
By convention, the main branch of our local repo is called "master" (as seen from the
earlier "git status" output). We shall discuss "branch" later.
11. // Push all commits of the branch "master" to remote repo "origin"
12. $ git push origin master
13. Username for 'https://github.com': ******
14. Password for 'https://your-username@github.com': *******
15. Counting objects: 10, done.
16. Delta compression using up to 8 threads.
17. Compressing objects: 100% (10/10), done.
18. Writing objects: 100% (10/10), 1.13 KiB | 0 bytes/s, done.
19. Total 10 (delta 1), reused 0 (delta 0)
20. To https://github.com/your-username/test.git
21. * [new branch] master -> master

Branch master set up to track remote branch master from origin.

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'.

Changes not staged for commit:


(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working dire
modified: Hello.java
no changes added to commit (use "git add" and/or "git commit -a")

// Stage file changes


$ git add *.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)
modified: Hello.java

// Commit all staged file changes


$ git commit -m "Third commit"
[master 744307e] Third commit
1 file changed, 1 insertion(+)

// Push the commits on local master branch to remote


$ git push origin master
Username for 'https://github.com': ******
Password for 'https://username@github.com': ******
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 377 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://github.com/your-username/test.git
711ef4f..744307e master -> master
Again, login to the remote to check the committed files.
3.3 Cloning a Project from a Remote Repo (git clone <remote-url>)
As mentioned earlier, you can start a local GIT repo either running "git init" on your own project,
or "git clone <remote-url>" to copy from an existing project.
Anyone having read access to your remote repo can clone your project. You can also clone any
project in any public remote repo.
The "git clone <remote-url>" initializes a local repo and copies all files into the working tree.
You can find the URL of a remote repo from the Git host.
// SYNTAX
// ======
$ git clone <remote-url>
// <url>: can be https (recommended), ssh or file.
// Clone the project UNDER the current directory
// The name of the "working directory" is the same as the remote project name
$ git clone <remote-url> <working-directory-name>
// Clone UNDER current directory, use the given "working directory" name

// 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.

// Stage file changes, which produces "Staged" file changes


$ git add <file> // for new and modified files
$ git rm <file> // for deleted files
$ git mv <old-file-name> <new-file-name> // for renamed file

// Commit (ALL staged file changes)


$ git commit -m "message"

// Push
$ git push <remote-name> <local-branch-name>

OR,

// Stage ALL files with changes


$ git add -A // OR, 'git add --all'

$ git commit -m "message"


$ git push

OR,

// Add All and Commit in one command


$ git commit -a -m "message"

$ 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

Changes not staged for commit:


modified: README.md
// Now, "README.md" has both unstaged and staged changes.

$ 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.

// Stage the changes


$ git add README.md

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
modified: README.md

You might also like