Most of the time, when we work with Git, we will use the command line:
$ git --help
usage: git [--version] [--help] [-C <path>] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
We will create a repository and create a file inside the repository:
$ mkdir TestRepo
$ cd TestRepo/
$ git init
Initialized empty Git repository in /home/echou/Master_Python_Networking_second_edition/Chapter10/TestRepo/.git/
$ echo "this is my test file" > myFile.txt
When the repository was initialized with Git, a new hidden folder of .git was added to the directory. It contains all the Git-related files:
$ ls -a
. .. .git myFile.txt
$ ls .git/
branches config description HEAD hooks info objects refs
There are several locations Git receives its configurations in a hierarchy format. You can use the git config -l command to see the aggregated configuration:
$ ls .git/config
.git/config
$ ls ~/.gitconfig
/home/echou/.gitconfig
$ git config -l
user.name=Eric Chou
user.email=<email>
core.editor=vim
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
When we create a file in the repository, it is not tracked. For git to be aware of the file, we need to add the file:
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
myFile.txt
nothing added to commit but untracked files present (use "git add" to track)
$ git add myFile.txt
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: myFile.txt
When you add the file, it is in a staged status. To make the changes official, we will need to commit the change:
$ git commit -m "adding myFile.txt"
[master (root-commit) 5f579ab] adding myFile.txt
1 file changed, 1 insertion(+)
create mode 100644 myFile.txt
$ git status
On branch master
nothing to commit, working directory clean
Let's make some changes to the file and commit it:
$ vim myFile.txt
$ cat myFile.txt
this is the second iteration of my test file
$ git status
On branch 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: myFile.txt
$ git add myFile.txt
$ git commit -m "made modificaitons to myFile.txt"
[master a3dd3ea] made modificaitons to myFile.txt
1 file changed, 1 insertion(+), 1 deletion(-)
The git commit number is a SHA1 hash, which an important feature. If we had followed the same step on another computer, our SHA1 hash value would be the same. This is how Git knows the two repositories are identical even when they are worked on in parallel.
We can show the history of the commits with git log. The entries are shown in reverse chronological order; each commit shows the author's name and email address, the date, the log message, as well as the internal identification number of the commit:
$ git log
commit a3dd3ea8e6eb15b57d1f390ce0d2c3a03f07a038
Author: Eric Chou <[email protected]>
Date: Fri Jul 20 09:58:24 2018 -0700
made modificaitons to myFile.txt
commit 5f579ab1e9a3fae13aa7f1b8092055213157524d
Author: Eric Chou <[email protected]>
Date: Fri Jul 20 08:05:09 2018 -0700
adding myFile.txt
We can also show more details about the change using the commit ID:
$ git show a3dd3ea8e6eb15b57d1f390ce0d2c3a03f07a038
commit a3dd3ea8e6eb15b57d1f390ce0d2c3a03f07a038
Author: Eric Chou <[email protected]>
Date: Fri Jul 20 09:58:24 2018 -0700
made modificaitons to myFile.txt
diff --git a/myFile.txt b/myFile.txt
index 6ccb42e..69e7d47 100644
--- a/myFile.txt
+++ b/myFile.txt
@@ -1 +1 @@
-this is my test file
+this is the second iteration of my test file
If you need to revert the changes you have made, you can choose between revert and reset. Revert changes all the file for a specific commit back to their state before the commit:
$ git revert a3dd3ea8e6eb15b57d1f390ce0d2c3a03f07a038
[master 9818f29] Revert "made modificaitons to myFile.txt"
1 file changed, 1 insertion(+), 1 deletion(-)
# Check to verified the file content was before the second change.
$ cat myFile.txt
this is my test file
The revert command will keep the commit you reverted and make a new commit. You will be able to see all the changes up to that point, including the revert:
$ git log
commit 9818f298f477fd880db6cb87112b50edc392f7fa
Author: Eric Chou <[email protected]>
Date: Fri Jul 20 13:11:30 2018 -0700
Revert "made modificaitons to myFile.txt"
This reverts commit a3dd3ea8e6eb15b57d1f390ce0d2c3a03f07a038.
modified: reverted the change to myFile.txt
commit a3dd3ea8e6eb15b57d1f390ce0d2c3a03f07a038
Author: Eric Chou <[email protected]>
Date: Fri Jul 20 09:58:24 2018 -0700
made modificaitons to myFile.txt
commit 5f579ab1e9a3fae13aa7f1b8092055213157524d
Author: Eric Chou <[email protected]>
Date: Fri Jul 20 08:05:09 2018 -0700
adding myFile.txt
The reset option will reset the status of your repository to an older version and discard all the changes in between:
$ git reset --hard a3dd3ea8e6eb15b57d1f390ce0d2c3a03f07a038
HEAD is now at a3dd3ea made modificaitons to myFile.txt
$ git log
commit a3dd3ea8e6eb15b57d1f390ce0d2c3a03f07a038
Author: Eric Chou <[email protected]>
Date: Fri Jul 20 09:58:24 2018 -0700
made modificaitons to myFile.txt
commit 5f579ab1e9a3fae13aa7f1b8092055213157524d
Author: Eric Chou <[email protected]>
Date: Fri Jul 20 08:05:09 2018 -0700
adding myFile.txt
Personally, I like to keep all the history, including any rollbacks that I have done. Therefore, when I need to rollback a change, I usually pick revert instead of reset.
A branch in git is a line of development within a repository. Git allows many branches and thus different lines of development within a repository. By default, we have the master branch. There are many reasons for branching, but most of them represent an individual customer release or a development phase, that is, the dev branch.
Let's create a dev branch within our repository:
$ git branch dev
$ git branch
dev
* master
To start working on the branch, we will need to checkout the branch:
$ git checkout dev
Switched to branch 'dev'
$ git branch
* dev
master
Let's add a second file to the dev branch:
$ echo "my second file" > mySecondFile.txt
$ git add mySecondFile.txt
$ git commit -m "added mySecondFile.txt to dev branch"
[dev c983730] added mySecondFile.txt to dev branch
1 file changed, 1 insertion(+)
create mode 100644 mySecondFile.txt
We can go back to the master branch and verify that the two lines of development are separate:
$ git branch
* dev
master
$ git checkout master
Switched to branch 'master'
$ ls
myFile.txt
$ git checkout dev
Switched to branch 'dev'
$ ls
myFile.txt mySecondFile.txt
To have the contents in the dev branch be written into the master branch, we will need to merge them:
$ git branch
* dev
master
$ git checkout master
$ git merge dev master
Updating a3dd3ea..c983730
Fast-forward
mySecondFile.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 mySecondFile.txt
$ git branch
dev
* master
$ ls
myFile.txt mySecondFile.txt
We can use git rm to remove a file. Let's create a third file and remove it:
$ touch myThirdFile.txt
$ git add myThirdFile.txt
$ git commit -m "adding myThirdFile.txt"
[master 2ec5f7d] adding myThirdFile.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 myThirdFile.txt
$ ls
myFile.txt mySecondFile.txt myThirdFile.txt
$ git rm myThirdFile.txt
rm 'myThirdFile.txt'
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: myThirdFile.txt
$ git commit -m "deleted myThirdFile.txt"
[master bc078a9] deleted myThirdFile.txt
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 myThirdFile.txt
We will be able to see the last two changes in the log:
$ git log
commit bc078a97e41d1614c1ba1f81f72acbcd95c0728c
Author: Eric Chou <[email protected]>
Date: Fri Jul 20 14:02:02 2018 -0700
deleted myThirdFile.txt
commit 2ec5f7d1a734b2cc74343ce45075917b79cc7293
Author: Eric Chou <[email protected]>
Date: Fri Jul 20 14:01:18 2018 -0700
adding myThirdFile.txt
We have gone through most of the basic operations we would use for Git. Let's take a look at how to use GitHub to share our repository.