Exercise Day 01
Exercise Day 01
Day 1
This set of exercises is prepared for you, to practice your git revision skills.
Outputs from all exercises should be placed in ftp://ftp.rt-rk.com/git-
gerrit/attenders-ns1/”your_name”/Day01/ directory
Exercise1
Purpose of this exercise is to setup git version control system on your working station.
Setup includes installation of git version control system and your personal configuration
2. Setup your user name and email. These values will be used as your signature in
all future commits
git config --global user.name "Your name"
git config --global user.email "[email protected]"
3. Setup colored terminal output. Colored terminal aids in visually parsing command
output
git config --global color.ui true
git config --global color.branch true
git config --global color.diff true
git config --global color.interactive true
git config --global color.status true
-1-
Git/Gerrit Course
Day 1
Exercise 2
To practice your revision skills you will need a repository and working tree. Create your
first git repository, for now on we will call it git-exercise repository
4. You can check local repository git configuration with --local option
git config --local --list
-2-
Git/Gerrit Course
Day 1
Exercise 3
In order to make revisions you will need some content. Let’s write hello world program
and commit it
int main()
{
printf("Hello world\n");
return 0;
}
-3-
Git/Gerrit Course
Day 1
7. Take a look at git-exercise repository history
git log
NOTICE that git log command gives you list of commits
10. Take a screenshot of your terminal window showing last commit content. Save
screenshot as GitRevision_3.4.png
Exercise 4
Git allows you to analyze current changes, before you make a commit. Let's we make
some changes on hello_world.c file and analyze it
Exercise 5
If you notice that there is something wrong with staged changes don’t worry, it is always
possible to update staging area
4. Ooops, we are missed to break a line in previous change. Never mind, just fix
mistake
printf("Hello once again\n");
-5-
Git/Gerrit Course
Day 1
5. Take a screenshot of your terminal window showing brief status of current and
staged changes. Save screenshot as GitRevision_5.2.png
NOTICE that last change is not added to staging area automatically
6. You must manually add new changes. Stage all changes and create new
commit, name it “Hello once again”
Exercise 6
If you by accident add some changes to staging area you can easily unstage it
10. Take a screenshot of your terminal window showing status of current changes.
Save screenshot as GitRevision_6.2.png
NOTICE that changes form hello_world.c file are unstaged
-6-
Git/Gerrit Course
Day 1
Exercise 7
In the life of every young programmer came a moment when he realize that some of his
work must be discarded. Reasons are numerous alcohol, drugs or just good old loud
music. With git you can easily discard current changes. Please be careful because
those changes will be lost forever
Exercise 8
If you notice that there is something wrong with your last commit don’t worry, you can
update it. Our little project do not have make file, let's create it
OBJECTS := $(SOURCES:.c=.o)
$(OUTPUT) : $(OBJECTS)
cc –o $(OUTPUT) $(OBJECTS)
.PHONY : clean
clean :
rm –f $(OUTPUT) $(OBJECTS)
-7-
Git/Gerrit Course
Day 1
5. Try out your makefile
make
make clean
NOTICE that you should get hello_world and hello_world.o files as output
from make
6. Stage Makefile
9. Take a screenshot of your terminal window showing content of last commit. Save
screenshot as GitRevision_8.2.png
NOTICE that last commit it updated with Makefile
Exercise 9
If happens that you accidentally add some unwanted files to repository index, you can
remove it from index with git rm command. Unwanted files are usually generated
files, for example build output
1. Run make in git-exercise directory. As output you should get hello_world.o and
hello_world files
2. Create commit that contains those generated files, name it “My first mistake”
-8-
Git/Gerrit Course
Day 1
3. Take a screenshot of your terminal window showing last commit content. Save
screenshot as GitRevision_9.1.png
6. To resolve earlier mistake you need to remove those files from index
git rm hello_world hello_world.o
NOTICE If those files exist they will be deleted
Exercise 10
To avoid further mistakes you can create ignore list. Files from ignore list will be
excluded from git status command output
1. Run make in git-exercise directory. As output you should get hello_world.o and
hello_world files
-9-
Git/Gerrit Course
Day 1
2. Take a screenshot of your terminal window showing status of current changes.
Save screenshot as GitRevision_10.1.png
NOTICE that unwanted files are listed as “Untracked”
5. Create new commit that contains .gitignore file, name it “Ignore list”
Exercise 11
Very often you have a need to rename or replace some files and keep file history. You
can do it manually within your file system and git will try to preserve history of files. Also
you can explicitly move or rename files with git mv command, which is
recommended approach
-10-
Git/Gerrit Course
Day 1
3. Take a screenshot of your terminal window showing status of current changes.
Save screenshot as GitRevision_11.1.png
NOTICE how git detects renaming (renamed: hello_world.c -> hello_git.c)
5. Take a screenshot of your terminal window showing history for hello_git.c file.
Save screenshot as GitRevisoin_11.2.png
git log hello_git.c
NOTICE that history shows only last commit
6. Take a screenshot of your terminal window showing history for hello_git.c file
with --follow flag. Save screenshot as GitRevision_11.3.png
git log --follow hello_git.c
NOTICE that history also shows commits made for hello_world.c changes
Exercise 12
Let’s now browse some commits from git-exercise repository. You can take a look at
every commit with git show command or you can put working tree to state of
particular commit with git checkout command
-11-
Git/Gerrit Course
Day 1
3. Take a screenshot of your terminal window showing repository history. Save
screenshot as GitRevision_12.2.png
Exercise 13
In the life of every young programmer come a moment when he realize that some of his
work must be discarded. The reasons are numerous alcohol, drugs or just old good
lemonade. Git have very effective tool for reverting changes, git revert command
will revert all changes that you want
-12-
Git/Gerrit Course
Day 1
Exercise 14
Beside reverting changes git allows you to discard repository history. History is
discarded with git reset command, which can fully delete changes or can delete
commits but preserve changes. Please be careful with git reset, with only one
wrong move you will lose all your work
-13-
Git/Gerrit Course
Day 1
9. Take a screenshot of your terminal window showing status of current changes.
Save screenshot as GitRevision_14.5.png
NOTICE that --soft option leaves changes staged after reset
command
-14-