0% found this document useful (0 votes)
11 views14 pages

Exercise Day 01

This document provides instructions for a series of Git exercises to practice revision skills. It covers setting up Git, creating and committing changes to a repository, analyzing differences, updating and amending commits, discarding changes, and removing unwanted files from the index.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views14 pages

Exercise Day 01

This document provides instructions for a series of Git exercises to practice revision skills. It covers setting up Git, creating and committing changes to a repository, analyzing differences, updating and amending commits, discarding changes, and removing unwanted files from the index.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Git/Gerrit Course

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

1. Install git version control system


sudo apt-get install git

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

4. You can check global git configuration with --list option


git config --global --list

5. Take a screenshot of your terminal window showing your global configuration.


Save screenshot as GitRevision_1.1.png

-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

1. Initialize new empty git repository for git-exercise working tree


git init git-exercise
NOTICE: If git-exercise directory do not exist it will be created

2. Take a screenshot of your terminal window showing content of git-exercise


directory. Save screenshot as GitRevision_2.1.png
ls –a git-exercise
NOTICE that git repository for git-exercise working tree is in git-
exercise/.git directory

3. Set vim as editor for git-exercise repository


git config core.editor vim
NOTICE: You need to be positioned in git-exercise working tree for repository
configuration

4. You can check local repository git configuration with --local option
git config --local --list

5. Take a screenshot of your terminal window showing git-exercise repository


configuration. Save screenshot as GitRevision_2.2.png

-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

1. Create git-exercise/hello_world.c file with following content :


#include <stdio.h>

int main()
{
printf("Hello world\n");
return 0;
}

2. Check status of current changes in working directory


git status
NOTICE that git status command gives you information about
changes on file level

3. Take a screenshot of your terminal window showing status of current changes.


Save screenshot as GitRevision_3.1.png
NOTICE that hello_world.c is grouped as “Untracked file” because it is a
new file

4. Stage changes from hello_world.c file


git add hello_world.c

5. Take a screenshot of your terminal window showing status of current changes.


Save screenshot as GitRevision_3.2.png
NOTICE that now hello_world.c is in staging area

6. Create new commit, name it “Initial commit”


git commit –m "Initial commit"

-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

8. Take a screenshot of your terminal window showing repository history. Save


screenshot as GitRevision_3.3.png
NOTICE that your new commit is shown in history

9. Take a look at content of your commit


git show <commit>
NOTICE that if you omit <commit> parameter HEAD will be used implicitly

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

1. In hello_world.c add another printf call


printf("Hello again\n");

2. Analyze current changes with git diff command


git diff
NOTICE that git diff command gives you brief status of current
changes

3. Take a screenshot of your terminal window showing brief status of current


changes. Save screenshot as GitRevision_4.1.png

4. Stage changes from hello_world.c file


-4-
Git/Gerrit Course
Day 1
5. Take a screenshot of your terminal window showing status of current changes.
Save screenshot as GitRevision_4.2.png

6. You can analyze staged changes with --cached option


git diff --cached

7. Take a screenshot of your terminal window showing brief status of staged


changes. Save screenshot as GitRevision_4.3.png

8. Create “Hello again” commit with staged changes

9. Take a screenshot of your terminal window showing repository history. Save


screenshot as GitRevision_4.4.png

Exercise 5
If you notice that there is something wrong with staged changes don’t worry, it is always
possible to update staging area

1. Add another printf call in hello_world.c


printf("Hello once again");

2. Stage changes from hello_world.c file

3. Take a screenshot of your terminal window showing brief status of staged


changes. Save screenshot as GitRevision_5.1.png

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”

7. Take a screenshot of your terminal window showing repository history. Save


screenshot as GitRevision_5.3.png

Exercise 6
If you by accident add some changes to staging area you can easily unstage it

1. Add another printf call in hello_world.c


printf("Guess who is back\n");

2. Stage changes from hello_world.c file

3. Take a screenshot of your terminal window showing status of current changes.


Save screenshot as GitRevision_6.1.png

4. Unstage changes from hello_world.c file


git reset HEAD hello_world.c

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

1. Take a screenshot of your terminal window showing status of current changes.


Save screenshot as GitRevision_7.1.png
NOTICE that you should have changes from previous exercise

2. Discard changes from previous exercise


git checkout -- hello_world.c

3. Take a screenshot of your terminal window showing status of current changes.


Save screenshot as GitRevision_7.2.png
NOTICE that changes are discarded

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

4. Crate git-exercise/Makefile file with following content:


SOURCES := hello_world.c
OUTPUT := hello_world

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

7. Take a screenshot of your terminal window showing status of current changes.


Save screenshot as GitRevision_8.1.png

8. Update your last commit with new changes


git commit --amend
NOTICE that git opens editor, in this point you can modify commit
message

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

4. Run make clean in git-exercise directory

5. Take a screenshot of your terminal window showing status of current changes.


Save screenshot as GitRevision_9.2.png
NOTICE that those generated files are detected as deleted

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

7. Take a screenshot of your terminal window showing status of current changes.


Save screenshot as GitRevision_9.3.png
NOTICE that those files are staged as deleted

8. Create new commit, name it “Fix first mistake”

9. Take a screenshot of your terminal window showing repository history. Save


screenshot as GitRevision_9.4.png

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”

3. To ignore generated files create a ignore list. Create git-exercise/.gitignore file


with following content
hello_world
hello_world.o

4. Take a screenshot of your terminal window showing status of current changes.


Save screenshot as GitRevision_10.2.png
NOTICE that ignored files are not listed any more

5. Create new commit that contains .gitignore file, name it “Ignore list”

6. Take a screenshot of your terminal window showing repository history. Save


screenshot as GitRevision_10.3.png

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

1. Rename hello_world.c to hello_git.c using git mv command


git mv hello_world.c hello_git.c

2. Stage all changes

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

4. Create new commit, name it “Rename Example”

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

1. Take a screenshot of your terminal window showing content of “Hello again”


commit. Save screenshot as GitRevision_12.1.png
git show <commit>

2. Temporary go back to “Hello again” commit


git checkout <commit>
NOTICE that repository is currently in “detached HEAD” state

-11-
Git/Gerrit Course
Day 1
3. Take a screenshot of your terminal window showing repository history. Save
screenshot as GitRevision_12.2.png

4. To return in regular state alight HEAD with master branch


git checkout master
NOTICE that every repository must have at least one branch (master
branch). Master branch is main track of your repository

5. Take a screenshot of your terminal window showing repository history. Save


screenshot as GitRevision_12.3.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

1. Revert changes from “Ignore list” commit


git revert <commit>
NOTICE that revert command create new commit which cancels earlier
changes. Revert is common way to discard changes but preserve
repository history

2. Take a screenshot of your terminal window showing content of revert


commit. Save screenshot as GitRevision_13.1.png
NOTICE that revert commit contains changes opposite to commit which is
reverted

-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

1. Take a screenshot of your terminal window showing repository history. Save


screenshot as GitRevision_14.1.png

2. Delete 1 commit from the top, use relative approach


git reset --hard HEAD~1

3. Take a screenshot of your terminal window showing repository history. Save


screenshot as GitRevision_14.2.png
NOTICE that there is one commit missing from the top

4. Delete 1 commit from the top, use absolute approach


git reset --hard <commit>
NOTICE that commit specified to git reset command is preserved

5. Take a screenshot of your terminal window showing repository history. Save


screenshot as GitRevision_14.3.png

6. Delete 1 commit from the top, but preserve changes


git reset HEAD~1
NOTICE that reset command leaves changes in working tree

7. Take a screenshot of your terminal window showing status of current changes.


Save screenshot as GitRevision_14.4.png

8. Reset repository to “Initial commit”, but leave changes staged


git reset --soft <commit>

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

You might also like