Simple usage of Git

Git - basic commands

Last time we talk about why we like Git. Today I would like to show you how we use Git. I will go thought base commands but if you like more information please use Git documentation.

Let's get started!

Git is distributed repository. This mean that every one in our team have all history of changes locally. You can commit changes quickly without Internet connection. The most important thing is when something goes wrong in one local machine rest of the team has copy of repo (repository). In case you have central repository, if something is broken in this repository you loose all your changes.

I will be talking about command line for Git. I think it is easier to understand Git and use command line then some graphic tools.

First - setup

When we use Git we want to be sure that your changes will be assigned to us. So we set up our user data.

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

We can also add colors to our command line.

git config --global color.ui true

Create repository

To start using Git in our project, we go to this project through command line and then we initialize Git inside.

git init

This command will initialize only local Git repository. To put our repo to server you can use tools like GitHub or Bitbucket. Ok now, how we can work with Git. Very helpful command will be:

git status

This command tell us what was change from last time we use Git. So we have for example ruby file:

# hello_world.rb
def hello
  "hello"
end

And it is not tracked by Git we can see something like that:

git status

# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   hello_world.rb
#
# nothing added to commit but untracked files present (use "git add" to track)

First commit

Now we must tell Git: please track this file, so we do.

git add hello_world.rb

After this when we use git status we see:

git status

# On branch master
#
# Initial commit
#
# Changes to be commited:
#   (use "git rm --cached  <file>..." to unstage)
#
#   new file: hello_world.rb
#

Now we can do commit. I can say that commit is like prepare box to send. You put into one box everything which creates one common piece. For our changes we add only one file so our commit will contain a simple file.

git commit -m "Create a hello function"

Then Git status shows:

git status

# On branch master
# nothing to commit (working directory clean)

So we change our function:

# hello_world.rb
def hello
  "hello world"
end
git status

# On branch master
# Changed but not updated:
#
#  modified: hello_world.rb
# no changes add to commit

If we want create new commit we must add new things to your box:

git add hello_world.rb

and create commit

git commit -m "Change our hello function"

Push/pull changes to/from remote repository

Ok, we know how create commits. Now I would like to show you how we can push our code to server (to remote repository) and how we can download (pull) our repo from server. To do this we will use GitHub but you can use others solutions.

When we have account on GitHub we create new repository and we need to run in command line something like this:

git remote add origin link_to_or_repo

Don't worry, after you create your new repository, GitHub show you all commands. You only need to copy and paste this commands to your command line. In short this command tells local Git: Hey this is my remote repository and its name is origin (We can say that this is my server for code). When we want to send our code to this remote repository we do:

git push -u origin master

So we tell to Git: push your code from branch master to repository with name origin.

What is branch?

It is like path. You can have many path in your code (for now we have only one and it's default name is master). And you can switch between this paths. After this command you will be asking about you GitHub user name and password. When you put this informations to console, your code will be available on GitHub.

Last think!

When you know that other people do some changes in this repository you can pull this changes locally.

git pull

This command pull code from remote repository with origin name to our current branch. Because we have only one branch - master, all changes will be download to master branch.

This is end. All you need to know to start working with Git is here. I hope you like it. If you wants know more please let me now in comments. See you next time! Bye!