Git basics

Linux, as usual, is more friendly to command line tool than Windows. Git is either already available for you or you can easily install it (e.g. sudo apt-get install git). On Windows one of the options is to use Cygwin; Windows 10 has a Linux Bash available where you can have Git support; you can also install Git for Windows.

To follow this post you should have GitHub account as well (if you are a student be sure to take advantage of this pack).

The first thing you should do after installing Git is to set your information. This is important because every Git commit uses this information:

git config --global user.name "Your Name"
git config --global user.email yourname@example.com

See this note about email on GitHub host. (You may need to reset the author information on your last commit: git commit –amend –reset-author)

Let’s start by getting somebody else’s project, mine for example:

git clone https://github.com/andykirik/hello-world-c.git

after running this command you should be ‘hello-world-c’ folder with two files inside: hello.c and makefile, and you can run make command to compile it (sorry, Linux only).

Now let’s learn some git basics. To start create new folder and copy hello.c and makefile you just cloned over there.

– to create empty repository:

git init

– to check status of repository:

git status

git would tell you that there are two untracked files, let’s add them to our repository, we do it in two steps: (1) we stage them and (2) we commit staged files:

git add hello.c makefile
(or git add *)
git commit -m "Initial commit"

if you omit comment text editor would appear (our favorite vi on Linux 🙂 ) to let you add some comments.

So we successfully created a git repository and added two files in it.

Next step let’s copy our repository to GitHub. Login to github.com, and click on a plus sign in the right top corner, and select New repository; type in name (let’s call is a ‘test’); you can make it public, BUT important thing is UNcheck “Initialize this repository with README” and select “Add .gitignore” None and “Add license” None – you need create empty repository in order to be able to push your existing one in there.

GitHub would provide you with instructions for the next steps. First we need to link you existing repository with the one we just created:

git remote add origin https://github.com/<your-name>/test.git

to verify run

git remote -v

and second step is upload you files to GitHub:

git push -u origin master

git should ask you for your username/password for GitHub account and upload all your files.
Now you can back to GitHub web page, refresh it and see hello.c and makefile in there.

Now you can go home and create a copy of you project there:

git clone https://github.com/<your-name>/test.git

This would create third copy of your repository. Please note that in Git world all copies are equal. There is no such thing as main repository or server.

In part II we will talk how to work with branches.

Leave a Reply