Getting your hands dirty with Githug

Getting your hands dirty with Githug

Learn Githug commands practically

Githug is an awesome program to help you improve your git skills. It contains several levels, that start off with some easy tasks and will go deeper with every level. At first, you start with initializing a bare git repo, adding and committing files, and configuring the .gitignore file. But then you get to remote repositories, stashing, rebasing, merging, and reflogs. I found it very useful, to improve my knowledge about git and found myself reading the man files more often, getting to know some awesome details about git.

Githug is written in Ruby and maintained on Github. There are more levels to come, so this blog will be updated continuously. If you find any errors or missing levels, feel free to comment below.

Installation

Githug requires Ruby 1.8.7 or higher. You can check which version of Ruby is installed with the following command:

ruby --version

If ruby is not installed, follow the installation instructions on ruby-lang.org.

Once you have installed Ruby, you're good to install Githug. To install it, run the command:

gem install githug

If you get a complaint about permissions, you can rerun the command with sudo:

sudo gem install githug

Game Instructions

After Githug is installed, change the directory to the location where you want the game-related assets to be stored. Then run githug:

githug

You will be prompted to create a directory.

No githug directory found, do you wish to create one? [yn]

Type y (yes) to continue, n (no) to cancel and quit Githug.

Commands

Githug has 4 game commands:

  • githug play - The default command, checks your solution for the current level
  • githug hint - Gives you a hint (if available) for the current level

  • githug reset - Reset the current level or reset the level to a given name or path

  • githug levels - List all the levels

Solutions

As I had some trouble myself with some of those levels, I thought, that it might be helpful for other people to find a solution to the levels. I decided to make my solutions available, hoping they might help you. My approach to solving the levels might differ from your approach, but that’s the beauty of such problems. There often isn’t this one way to get things done. But without any further ado, here’s my solution.

Note: Please do not go to the solution directly. First try yourself!

Make sure you move into the git_hug directory created by Githug to continue with the game.

cd git_hug

Level 1: init

A new directory, git_hug, has been created; initialize an empty repository in it.

git init

Level 2: config

Set up your git name and email, this is important so that your commits can be identified.

git config --global user.name "Ashutosh Krishna"
git config --global user.email "ashutoshbritish@gmail.com"

Level 3: add

There is a file in your folder called README, you should add it to your staging area
Note: You start each level with a new repo. Don't look for files from the previous one.

git add README

Level 4: commit

The README file has been added to your staging area, now commit it.

Make sure, your branch at this stage is master. If it's not, change your branch to master using the command:

git checkout -b master

After the branch has been changed to master, here's the solution:

git commit -m "Add README"

Level 5: clone

Clone the repository at https://github.com/Gazler/cloneme.

git clone https://github.com/Gazler/cloneme

Level 6: clone_to_folder

Clone the repository at https://github.com/Gazler/cloneme to my_cloned_repo.

git clone https://github.com/Gazler/cloneme my_cloned_repo

Level 7: ignore

The text editor 'vim' creates files ending in .swp (swap files) for all files that are currently open. We don't want them creeping into the repository. Make this repository ignore those swap files which are ending in .swp.

vim .gitignore

This opens the 'vim' editor. Press I to Insert Text and add the following in the .gitignore file:

*.swp

To save and exit, press the Esc key and type :wq, and press Enter.

Level 8: include

Notice a few files with the '.a' extension. We want git to ignore all but the 'lib.a' file.

vim .gitignore

Add the following in the .gitignore file:

*.a
!lib.a

Level 9: status

There are some files in this repository, and one of the files is untracked, which file is it?

git status

Level 10: number_of_files_committed

There are some files in this repository, how many of the files will be committed?

git status

Level 11: rm

A file has been removed from the working tree, however, the file was not removed from the repository. Find out what this file was and remove it.

git status
git rm deleteme.rb

Level 12: rm_cached

A file has accidentally been added to your staging area, find out which file and remove it from the staging area.

NOTE: Do not remove the file from the file system, only from git.

git status
git rm --cached deleteme.rb

Level 13: stash

You've made some changes and want to work on them later. You should save them, but don't commit them.

git stash

Level 14: rename

We have a file called oldfile.txt. We want to rename it to newfile.txt and stage this change.

git mv oldfile.txt newfile.txt
git add newfile.txt

Level 15: restructure

You added some files to your repository, but now realize that your project needs to be restructured. Make a new folder named src and using Git move all of the .html files into this folder.

mkdir src
git mv *.html src

Level 16: log

You will be asked for the hash of the most recent commit. You will need to investigate the logs of the repository for this.

git log

Conclusion

I hope you had a fun time learning Git with Githug.

Share your score in the comments section, we'd love to see it!