Git Basics
Git is a version control system for software development.
There are two ways to manage versions using Git. Either through a series of commits or through the use of branches.
In this tutorial you will learn how to use the version control of Git using commit and go back to a previous commit.
Prerequisite:
Having a programming background is required to do this tutorial.
Install a Git Client
Go to Git.
Download the Git client installer and install.
Test if the Git client is installed successfully. Open a terminal window.
> gitOutput:
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>] The most commonly used git commands are: add Add file contents to the index bisect Find by binary search the change that introduced a bug : :You should see the help screen of the Git client.
Create several text files and use Git to manage the versions of the files
In this tutorial, you will be creating several .txt files that will contain a list of words representing each letter of the alphabet. The use of this approach will help you understand how Git works. Once you are familiar with Git, you may use Git with your source code (e.g., .java file).
Create the directory
gittempin the root directory. Go to the created directory.> mkdir gittemp > cd gittempCreate the subdirectory
alphabetin thegittempdirectory. Go to the created directory.> mkdir alphabet > cd alphabetThe
alphabetdirectory represents a working directory containing your source code. As mentioned earlier, instead of source code, text files will be used in this tutorial.Make the
alphabetdirectory a local Git repository.> git initOutput:
Initialized empty Git repository in /gittemp/alphabet/.git/A hidden directory named
.gitis created after you issued thegit initcommand. You should not modify the contents of the.githidden directory. This contains information that will track the changes you made inside thealphabetdirectory.Check the status of your local Git repository.
> git statusOutput:
On branch master Initial commit nothing to commit (create/copy files and use "git add" to track)As stated in the status, there is
nothing to commit. This will change once you start creating files.Create a file
animal.txtin thealphabetdirectory with the following contents:ant bat cat dog eagle fox goat horseCheck the status of your local Git repository.
> git statusOutput:
On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) animal.txt nothing added to commit but untracked files present (use "git add" to track)Git detected the
animal.txtfile that you created. However, it is currently untracked by Git. Untracked files are those in the working directory but are NOT in index.Git does not track all the files you created since it is possible that some of the files are non-essential. You need to explicitly tell Git to track a file.
Let Git track
animal.txt.> git add animal.txtIf there are multiple files that you want Git to track, you may use the same
git addcommand and just separate the files with aspace.The
git add animal.txtcommand places theanimal.txtin the index. Those files in the index are staged. Staging means that a file is being prepared to be committed.Check the status of your local Git repository.
> git statusOutput:
On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: animal.txtAt this point,
animal.txtis already in index and being tracked by Git. However, changes made inanimal.txtis not yet committed.Commit the changes made in
animal.txt.> git commit -m "added words from ant to horse"Output:
[master (root-commit) 828389d] added words from ant to horse 1 file changed, 8 insertions(+) create mode 100644 animal.txtNote that if there are other files that are staged aside from
animal.txt, the changes made in those files will also be committed.Check the status of your local Git repository.
> git statusOutput:
On branch master nothing to commit, working directory cleanAt this point, changes made in
animal.txtare committed. In thegit commitcommand, you included a messageadded words from ant to horseto give a short description on what changes were committed. This is very useful when you perform several commits later and you want to undo previous commits.Visually look at the the details regarding the commit you performed.
> gitkOutput:
*master added words from ant to horsegitkis a tool which allows you to visualize the commits you performed. You may usegitkas a guide when you need to undo previous commits.Update
animal.txtto include wordsiguanatopig:ant bat cat dog eagle fox goat horse iguana jaguar kangaroo lion monkey newt octopus pigTrack and commit the changes made in
animal.txt.> git add animal.txt > git commit -m "added words from iguana to pig"Output:
[master 5394740] added words from iguana to pig 1 file changed, 9 insertions(+), 1 deletion(-)Update
animal.txtto include wordsquailtozebra. In addition, deletedogand changemonkeytomouse:ant bat cat eagle fox goat horse iguana jaguar kangaroo lion mouse newt octopus pig quail rabbit snail tiger uakari vulture wolf x-ray tetra yak zebraTrack and commit the changes made in
animal.txt.> git add animal.txt > git commit -m "added words from quail to zebra, deleted dog, and changed monkey to mouse"Output:
[master f1fbe3c] added words from quail to zebra, deleted dog, and changed monkey to mouse 1 file changed, 12 insertions(+), 3 deletions(-)
Checkout Previous Commits
Checking out previous commits allows you to inspect the state of the files after a particular commit. This is done by changing back the contents of the working directory. However, the latest committed changes remain intact.
Get a summary of the commits performed in the local Git repository.
> git logOutput:
commit f1fbe3cfccfa0343f0b4ffe0d9c35967174b5f77 Author: Alexis V. Pantola <pantolav@gmail.com> Date: Sun Jan 10 23:32:48 2016 +0800 added words from quail to zebra, deleted dog, and changed monkey to commit 539474028f1a73dcb01111ce79eefa4a50a31baf Author: Alexis V. Pantola <pantolav@gmail.com> Date: Sun Jan 10 23:31:45 2016 +0800 added words from iguana to pig commit 828389d84eedf01b1ca4fa842c879139bc8e62b5 Author: Alexis V. Pantola <pantolav@gmail.com> Date: Sun Jan 10 23:29:40 2016 +0800 added words from ant to horseNOTE: You may press
Qto exit the log console.Notice that the three commits performed earlier are listed in the log. In addition, there are unique hash values identifying each commit. As an example, in the log above, the commit
added words from ant to horsehas a hash value of828389d84eedf01b1ca4fa842c879139bc8e62b5. Note that the hash value in your repository is not necessarily the same as the one shown in this tutorial.The hash value is important to undo previous commits and go to a particular state of a file.
Inspect the state of the file when the words ant to horse has just been added.
> git checkout <hash of commit for "added words from ant to horse">Example:
> git checkout 828389d84eedf01b1ca4fa842c879139bc8e62b5Output:
Note: checking out '828389d84eedf01b1ca4fa842c879139bc8e62b5'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b new_branch_name HEAD is now at 828389d... added words from ant to horseOpen (or reload)
animal.txtand verify that its content is now the following:ant bat cat dog eagle fox goat horseAs expected, you went back to the state when the words ant to horse has just been added to
animal.txt.Check the contents of the log.
> git logOutput:
commit 828389d84eedf01b1ca4fa842c879139bc8e62b5 Author: Alexis V. Pantola <pantolav@gmail.com> Date: Sun Jan 10 23:29:40 2016 +0800 added words from ant to horseNotice that the log has only one commit.
Go back to the last commit you made (i.e.,
added words from quail to zebra, deleted dog, and changed monkey to mouse).> git checkout masterOutput:
Previous HEAD position was 828389d... added words from ant to horse Switched to branch 'master'Open (or reload)
animal.txtand verify that its content is now the following:ant bat cat eagle fox goat horse iguana jaguar kangaroo lion mouse newt octopus pig quail rabbit snail tiger uakari vulture wolf x-ray tetra yak zebraAs expected, you are able to return to the latest state of the file.
Go back to a particular Commit
The git checkout command that was demonstrated above allows you to inspect the state of a file of a particular commit. However, the latest committed changes remain intact. In the event that you really want to go back to a particular commit (e.g., you realized that you erroneously changed the contents of a file and want to go back to its correct state), you may use the git reset command.
Get a summary of the commits performed in the local Git repository.
> git logOutput:
commit f1fbe3cfccfa0343f0b4ffe0d9c35967174b5f77 Author: Alexis V. Pantola <pantolav@gmail.com> Date: Sun Jan 10 23:32:48 2016 +0800 added words from quail to zebra, deleted dog, and changed monkey to commit 539474028f1a73dcb01111ce79eefa4a50a31baf Author: Alexis V. Pantola <pantolav@gmail.com> Date: Sun Jan 10 23:31:45 2016 +0800 added words from iguana to pig commit 828389d84eedf01b1ca4fa842c879139bc8e62b5 Author: Alexis V. Pantola <pantolav@gmail.com> Date: Sun Jan 10 23:29:40 2016 +0800 added words from ant to horseThis is the same log that was shown in the previous steps.
Go back to the state of the file when the words ant to horse has just been added.
> git reset --hard <hash of commit for "added words from ant to horse">Example:
> git reset --hard 828389d84eedf01b1ca4fa842c879139bc8e62b5Output:
HEAD is now at 828389d added words from ant to horseOpen (or reload)
animal.txtand verify that its content is now the following:ant bat cat dog eagle fox goat horseSimilar to
git checkout, you are able to go back to the state when the words ant to horse has just been added toanimal.txtusinggit reset. You will see later the difference of these two commands.Check the contents of the log.
> git logOutput:
commit 828389d84eedf01b1ca4fa842c879139bc8e62b5 Author: Alexis V. Pantola <pantolav@gmail.com> Date: Sun Jan 10 23:29:40 2016 +0800 added words from ant to horseTRY to go back to the last commit you made (i.e.,
added words from quail to zebra, deleted dog, and changed monkey to mouse).> git checkout masterOutput:
Already on 'master'Notice that the output is
Already on 'master'. This means that you have reset the last commit fromadded words from quail to zebra, deleted dog, and changed monkey to mousetoadded words from ant to horse. Theadded words from quail to zebra, deleted dog, and changed monkey to mousecommit as well as theadded words from iguana to pigcommit are already removed.Open (or reload)
animal.txtand verify that its content is STILL ant to horse:ant bat cat dog eagle fox goat horse
End of Tutorial
Go back to the List of Tutorials.
