What is Git? Git is a source code control repository. It is useful if the need comes to revert to a previous version of the code. One can use it to find differences between versions. It supports branches, so one can try new features or changes and be able to get back to the working version at any point in time.
Git is distributed. Every developer has their own copy of the source code. This allows for fast access when needed allowing developers to work offline. That said; Git allows developers to merge their versions and share the updates with other members in the group.
The following figure (from Wikipedia) provides an overview of Git and some important commands:
The Remote repository is used by all developers to synchronize the versions of the source code. Each developer has a Local repository allowing them to work offline. The Working Directory is used to keep the current version being worked on. When the developer is ready with changes, the add command is used to generate an Index cache. The Index holds all changes that were made by a developer since the code was last fetched from a repository. To move / merge the changes in the Index, the developer uses the commit command. At that point the Local repository holds code ready to be pushed to the Remote repository. The push command may then be used to send the code to the Remote repository in the cloud and merge it to become the latest version (more on this later).
To install Git on Windows you may to use the following steps:
Step | Description |
1 | Open a web browser. |
2 | Point it to: http://git-scm.com/ |
3 | Click on the “Download for Windows” icon to download the latest release of Git Bash. |
4 | Specify a location for the download (e.g., c:\temp\Git-2.10.2-64-bit.exe). |
5 | Double click on the Git installer. |
6
|
Select the following checkboxes:
– Additional icons – Git Bash Here – Git GUI Here – Associate .git* configuration files with the default test editor – Associate .sh files to be run with Bash |
7 | Click on the <Next> button. |
8 | Select the Use Git from the Windows Command Prompt radio button. |
9 | Click on the <Next> button. |
10 | Select the Checkout Windows-style, commit Unix-style line endings radio button. |
11 | Click on the <Next> button. |
12 | Select the Use MinTTY (the default terminal of MSY2) radio button. |
13 | Click on the <Next> button. |
14 | Click on the <Install> button. The installer will take a minute or so to install Git on your computer. |
15 | Click on the <Finish> button. |
From the git-scm-com web site:
“Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows”.
Let’s set some configurations for Git:
Step | Description |
1 | Double click on the Git Bash icon. |
2 | Issue the pwd command. The default folder is not where you wish to keep your local repository. |
3 | Close the Git Bash window. |
4 | Right click on the Git Bash icon and select Properties. |
5
|
Create a convenient folder to host your Git projects (e.g., c:\Git\Projects). Do not to use spaces in the names of the selected folders. |
6 | Set the Start in: to a more convenient path (e.g., c:\Git\Projects). |
7 | Remove modifiers from Target: (e.g., “C:\Program Files\Git\git-bash.exe“). |
8 | Click on the <Apply> button. |
9 | Click on the <OK> button. |
10 | On the Git Bash console type: $ git config –global user.name “john” where “john” is your user name. |
11
|
On the Git Bash console type: $ git config –global user.email “john.canessa@gmail.com” where “john.canessa@gmail.com” is your email address. |
To test the operations of Git perform the following steps:
Step | Description |
1
|
Create a folder to hold the new project (e.g., C:\GitProjects\gitproject). |
2
|
Create a couple of empty text files to experiment with:
C:\Git\Projects\gitproject> dir Volume in drive C is OS Volume Serial Number is 26E8-87B0 Directory of C:\Git\Projects\gitproject 11/13/2016 09:11 AM <DIR> . 11/13/2016 09:11 AM <DIR> .. 11/13/2016 09:10 AM 0 readme.txt 11/13/2016 09:11 AM 0 text.txt 2 File(s) 0 bytes 2 Dir(s) 812,652,146,688 bytes free |
3
|
On the Git Bash console enter:
$ git init gitproject Initialized empty Git repository in C:/Git/Projects/gitproject/gitproject/.git/ |
4
|
A new directory (gitproject) has been created as illustrated by the following console capture:
C:\Git\Projects\gitproject> dir Volume in drive C is OS Volume Serial Number is 26E8-87B0 Directory of C:\Git\Projects\gitproject 11/13/2016 09:14 AM <DIR> . 11/13/2016 09:14 AM <DIR> .. 11/13/2016 09:14 AM <DIR> gitproject 11/13/2016 09:10 AM 0 readme.txt 11/13/2016 09:11 AM 0 text.txt 2 File(s) 0 bytes 3 Dir(s) 812,652,359,680 bytes free |
5
|
From the Git Bash console enter the following command:
$ cd gitproject John@Condor MINGW64 /c/Git/Projects/gitproject/gitproject (master) The software recognizes the folder as the master for the gitproject. |
5
|
From the Git Bash console enter the following command:
$ git status On branch master Initial commit nothing to commit (create/copy files and use “git add” to track) |
6
|
From the Git Bash issue the following set of commands to get Git to start tracking the two text files we created in a previous step:
John@Condor MINGW64 /c/Git/Projects/gitproject $ ls gitproject/ readme.txt text.txt John@Condor MINGW64 /c/Git/Projects/gitproject $ cp *.txt gitproject John@Condor MINGW64 /c/Git/Projects/gitproject $ cd gitproject John@Condor MINGW64 /c/Git/Projects/gitproject/gitproject (master) $ ls readme.txt text.txt John@Condor MINGW64 /c/Git/Projects/gitproject/gitproject (master) $ git status On branch master Initial commit Untracked files: (use “git add <file>…” to include in what will be committed) readme.txt text.txt nothing added to commit but untracked files present (use “git add” to track) John@Condor MINGW64 /c/Git/Projects/gitproject/gitproject (master) $ git add . John@Condor MINGW64 /c/Git/Projects/gitproject/gitproject (master) $ git status On branch master Initial commit Changes to be committed: (use “git rm –cached <file>…” to unstage) new file: readme.txt new file: text.txt John@Condor MINGW64 /c/Git/Projects/gitproject/gitproject (master) |
7
|
To perform the initial commit with a message enter the following on the Git Bash console:
$ git commit -m “initial commit” [master (root-commit) 3178a7f] initial commit 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 readme.txt create mode 100644 text.txt John@Condor MINGW64 /c/Git/Projects/gitproject/gitproject (master) |
8
|
To check out activity on the project from the Git Bash console enter the following command:
$ git log commit 3178a7f5b05209f17c21a54adf0174ae6ff8f702 Author: john <john.canessa@gmail.com> Date: Sun Nov 13 09:45:07 2016 -0600 initial commit John@Condor MINGW64 /c/Git/Projects/gitproject/gitproject (master) |
9
|
The following command in the Git Bash console indicates current status of the repository:
$ git status On branch master nothing to commit, working tree clean John@Condor MINGW64 /c/Git/Projects/gitproject/gitproject (master) |
10
|
Modify the readme.txt file. The file now contains two lines with text and a third blank line:
C:\Git\Projects\gitproject> dir Volume in drive C is OS Volume Serial Number is 26E8-87B0 Directory of C:\Git\Projects\gitproject 11/13/2016 09:14 AM <DIR> . 11/13/2016 09:14 AM <DIR> .. 11/13/2016 09:35 AM <DIR> gitproject 11/13/2016 09:10 AM 0 readme.txt 11/13/2016 09:11 AM 0 text.txt 2 File(s) 0 bytes 3 Dir(s) 812,651,294,720 bytes free C:\Git\Projects\gitproject> notepad.exe readme.txt C:\Git\Projects\gitproject> type readme.txt This is the readme.txt file. It was modified to include some text. C:\Git\Projects\gitproject> |
11 | To update the gitproject repository enter the following commands in the Git Bash console:
$ ls -l total 1 drwxr-xr-x 1 John 197121 0 Nov 13 09:35 gitproject/ -rw-r–r– 1 John 197121 69 Nov 13 09:56 readme.txt -rw-r–r– 1 John 197121 0 Nov 13 09:11 text.txt John@Condor MINGW64 /c/Git/Projects/gitproject $ cp readme.txt gitproject John@Condor MINGW64 /c/Git/Projects/gitproject $ cd gitproject John@Condor MINGW64 /c/Git/Projects/gitproject/gitproject (master) $ git add . John@Condor MINGW64 /c/Git/Projects/gitproject/gitproject (master) $ git commit -m “modified readme.txt” [master 4c4cbea] modified readme.txt 1 file changed, 2 insertions(+) John@Condor MINGW64 /c/Git/Projects/gitproject/gitproject (master) |
12
|
Enter the following to get a history of activity in the repository:
$ git log commit 4c4cbea42cb025f19c1fee4beb21ac3b70211847 Author: john <john.canessa@gmail.com> Date: Sun Nov 13 10:04:01 2016 -0600 modified readme.txt commit 3178a7f5b05209f17c21a54adf0174ae6ff8f702 Author: john <john.canessa@gmail.com> Date: Sun Nov 13 09:45:07 2016 -0600 initial commit We now have the initial commit followed by the modified readme.txt. Both where done by me. |
13
|
Modify the text.txt file and then check for differences:
$ notepad.exe text.txt John@Condor MINGW64 /c/Git/Projects/gitproject $ cat text.txt This line was just added. That is all folks. The file now has three lines. John@Condor MINGW64 /c/Git/Projects/gitproject $ git diff text.txt gitproject/text.txt diff –git a/text.txt b/gitproject/text.txt index 481d27d..e69de29 100644 — a/text.txt +++ b/gitproject/text.txt @@ -1,3 +0,0 @@ -This line was just added. -That is all folks. -The file now has three lines. \ No newline at end of file |
14
|
To get a one liner per commit one may use the following:
$ git log –oneline caad122 text.txt was changed 4c4cbea modified readme.txt 3178a7f initial commit |
15
|
To get help on a command (e.g., status) on a web browser enter the following into a Git Bash console:
$ git help status |
It is a very good idea to be able to store copies of the code on a cloud / remote repository. This is in particular useful when working with multiple developers.
On a future post I will cover steps on setting up a remote / cloud repository.
If you have comments / questions regarding this or any other post in this blog, please do not hesitate and send me a message.
John
john.canessa@gmail.com