Git and GitHub Exercises#
Getting Started with Git#
Start a repository in a new directory.
Create a new file in your new Git repository. Make sure Git is tracking the file and then create a new commit.
Make changes to the file, and then commit these changes.
Add two new files to your repository, but only commit one of them. What is the status of your repository after the commit?
Undo the last commit, add the untracked file, and redo the commit.
Solution to
# 1
cd $(mktemp -d)
pwd
git init
# 2
echo hello > README.md
git add README.md
git status README.md
git commit -m initial
# 3
echo sunshy! >> README.md
git commit README.md -m 'add name'
# 4
echo echo Namaste > README.in.md
echo echo Merhaba > README.tr.md
git add README.in.md
git commit -m 'add translations'
git status
# 5
git reset --soft HEAD~
git status # README.in.md should be in `staged` state
git add README.tr.md
git commit -m 'add translations'
git status
Important Git Features#
Look at the help pages for
git log
andgit diff
.Add to the
.gitignore
you already started to include a specific file name, then add that file to your repository.Create a file that contains the Git log for this repository. Use
grep
to see which day of the week most of the commits occurred on.
Solution to
for
git log
the--oneline
option looks nifty:cd $(mktemp -d) pwd git init for i in {1..5} do echo $i >> f git add f; git commit -m $i done echo ================= echo WITHOUT --oneline echo ================= git log echo ================= echo WITH --oneline echo ================= git log --oneline
To see our last log:
cd $(mktemp -d) pwd git init for i in {1..5} do echo $i >> f git add f; git commit -m $i done git log -1
git log
is also useful to see the commits between two commits. Every commit is unique and described by a long hash number (e.g.,01fda1b4d72c21760fdffcd5cc4e132570e0001f
) but the first digits of this hash number are typically enough for identifying the commit (e.g.,01fda1b
). Instead of commits we can use also use the aliasHEAD
, branch names, or tags (if tags exist in the repository). Let us show the changes between the first and second version:cd $(mktemp -d) pwd git init echo 1 >> f git add f; git commit -m 1; git tag v1 for i in {2..5} do echo $i >> f git add f; git commit -m $i done; git tag v2 echo 6 >> f git add f; git commit -m 6; git log --oneline v1..v2
git diff
is useful to see your modifications to your last commit:cd $(mktemp -d) pwd git init for i in {1..5} do echo $i >> f git add f; git commit -m $i done echo 6 >> f echo -1 >> g git diff
Note that modifications to
g
are not shown becauseg
is not part of the repo.Git will not accept this and will warn you:
cd $(mktemp -d) pwd git init echo 1 >> f.txt touch g.log echo '*.log' > .gitignore git add g.log f.txt
cd $(mktemp -d); pwd git clone https://github.com/seankross/the-unix-workbench; cd * git log > log for day in Mon Tue Wed Thu Fri Sat Sun do echo $(grep -c $day log) commits on $day done | sort --numeric | tac
Branching#
Start a new branch.
Switch to that branch and add commits to it. Switch to an older branch and then merge the new branch into your current branch.
Purposefully create and resolve a merge conflict.
Solution to
cd $(mktemp -d)
pwd
git init
for i in {1..5}
do
echo $i >> f
git add f; git commit -m $i
done
# 1
git branch test
# 2
git checkout test
echo test >> f
git commit f -m test
git checkout main
git log --oneline
git merge test
# 3
echo twenty >> f
git commit f -m twenty
git checkout test
echo 20 >> f
git commit f -m 20
git checkout main
git merge test
# fix conflict by editing f
# then commit
GitHub#
Create a new repository on GitHub. Clone your repository and add a
README.md
file. Push this file to GitHub and create a GitHub Pages website for this repository.Fork an existing repository (try one of mine: seankross) and try to identify something valuable you could contribute. Make changes or additions to that repository, then open a pull request.
Read through GitHub’s Guides.
Solution to
In Github guides browse Connecting to GitHub with SSH to use public key authentication instead of passwords. This way you do not have to provide your password every time whenever you push your repo.