How To Use Git Effectively
How To Use Git Effectively
We hope you find this tutorial helpful. In addition to guides like this one, we provide simple cloud infrastructure for developers. Learn more →

How To Use Git Effectively

PostedMarch 6, 2013 342.1k views Git

This article assumes that you have git installed and that your global configuration settings (namely username and email) are properly set. If this is not the case, please refer to the git introduction tutorial.

Git is a very useful piece of software to help streamline development for programming projects. It comes with no language requirements nor file structure requirements, leaving it open for the developers to decide how they want to structure their workflow.

Before using git for your development, it's a good idea to plan out your workflow. The workflow decision is typically based on the size and scale of your project. To gain a basic understanding of git for now, a simple, single-branch workflow will suffice. By default, the first branch on any git project is called "master". In a following tutorial in this series, you will learn how to create other branches.

Let's create our first project and call it "testing". (If you already have a project that you want to import to git you can skip down to that section.)

Creating your workspace

Just like you want to have a good, clean work environment, the same idea applies to where you do your coding, especially if you're going to contribute to a number of projects at the same time. A good suggestion might be to have a folder called git in your home directory which has subfolders for each of your individual projects.

The first thing we need to do is create our workspace environment:

user@host ~ $ mkdir -p ~/git/testing ; cd ~/git/testing

The above commands will accomplish two things: 1) It creates a directory called "git" in our home directory and then creates a subdirectory inside of that called "testing" (this is where our project will actually be stored). 2) It brings us to our project's base directory.

Once inside that directory, we need to create a few files that will be in our project. In this step, you can either follow along and create a few dummy files for testing purposes or you can create files/directories you wish that are going to be part of your project.

We are going to create a test file to use in our repository:

user@host ~/git/testing $ touch file

Once all your project files are in your workspace, you need to start tracking your files with git. The next step explains that process.

Converting an existing project into a workspace environment

Once all the files are in your git workspace, you need to tell git that you want to use your current directory as a git environment.

user@host ~/git/testing $ git init
Initialized empty Git repository in /home/user/git/testing/.git/

Once your have initialized your new empty repository, you can add your files.

The following will add all files and directories to your newly created repository:

user@host ~/git/testing $ git add .

In this case, no output is good output. Unfortunately, git does not always inform you if something worked.

Every time you add or make changes to files, you need to write a commit message. The next section describes what a commit message is and how to write one.

Creating a commit message

A commit message is a short message explaining the changes that you've made. It is required before sending your coding changes off (which is called a push) and it is a good way to communicate to your co-developers what to expect from your changes. This section will explain how to create them.

Commit messages are generally rather short, between one and two sentences explaining what your change did. It is good practice to commit each individual change before you do a push. You can push as many commits as you like. The only requirement for any commit is that it involves at least one file and it has a message. A push must have at least one commit.

Continuing with our example, we are going to create the message for our initial commit:

user@host ~/git/testing $ git commit -m "Initial Commit" -a
[master (root-commit) 1b830f8] initial commit
 0 files changed
 create mode 100644 file

There are two important parameters of the above command. The first is -m, which signifies that our commit message (in this case "Initial Commit") is going to follow. Secondly, the -a signifies that we want our commit message to be applied to all added or modified files. This is okay for the first commit, but generally you should specify the individual files or directories that we want to commit.

We could have also done:

user@host ~/git/testing $ git commit -m "Initial Commit" file

To specify a particular file to commit. To add additional files or directories, you just add a space separated list to the end of that command.

Pushing changes to a remote server

Up until this point, we have done everything on our local server. That's certainly an option to use git locally, if you want to have any easy way to have version control of your files. If you want to work with a team of developers, however, you're going to need to push changes to a remote server. This section will explain how to do that.

The first step to being able to push code to a remote server is providing the URL where the repository lives and giving it a name. To configure a remote repository to use and to see a list of all remotes (you can have more than one), type the following:

user@host ~/git/testing $ git remote add origin ssh://git@git.domain.tld/repository.git 
user@host ~/git/testing $ git remote -v
origin	ssh://git@git.domain.tld/repository.git (fetch)
origin	ssh://git@git.domain.tld/repository.git (push)

The first command adds a remote, called "origin", and sets the URL to ssh://git@git.domain.tld/repository.git.

You can name your remote whatever you'd like, but the URL needs to point to an actual remote repository. For example, if you wanted to push code to GitHub, you would need to use the repository URL that they provide.

Once you have a remote configured, you are now able to push your code.

You can push code to a remote server by typing the following:

user@host ~/git/testing $ git push origin master
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 266 bytes, done.
Total 3 (delta 1), reused 1 (delta 0)
To ssh://git@git.domain.tld/repository.git
   0e78fdf..e6a8ddc  master -> master

"git push" tells git that we want to push our changes, "origin" is the name of our newly-configured remote server and "master" is the name of the first branch.

In the future, when you have commits that you want to push to the server, you can simply type "git push".

I hope this article provided you with a basic understanding of how git can be used effectively for a team of developers. The next article in this series will provide a more in-depth analysis of git branches and why they are so effective.

By Jason Kurtz

29 Comments

  • Preview
Markdown Preview
  • B
  • I
  • H1
  • H2
  • H3
  • UL
  • OL
  • Code
  • Highlight
  • Table
Could not load preview. Try again
Loading preview...
  • (1) What did thd command "touch file" do? (2) It would have been helpful either to make the text representing command prompt accurately represent what I see on my screen, or make it of a different color than the text of commands that I am supposed to type, as has been done in other tuturials.
  • The command "touch file" creates a blank file, named "file". And I'm sorry, but I don't exactly understand what you mean about the text color.
  • Part 3 of this excellent git series. https://www.digitalocean.com/community/articles/how-to-use-git-branches
    by Jason Kurtz
    A branch, at its core, is a unique series of code changes with a unique name. Each repository can have one or more branches. This tutorial will teach you how to create two branches (master and develop) and how to merge code from the development stage to production.
  • Jason, I think yeosamuelh would like the prompt to be a different color than the command you typed in. This had me thrown a bit too as I was typing the directory path as if it were part of the command. So instead of typing, "touch file" I was typing, "~/git/testing $ touch file".
  • what GUI client can i used with Git on the VPS??
  • @trisha.duke http://www.sourcetreeapp.com/
  • ssh://git@git.domain.tld/repository.git. is not easy understable. What should we that our domain is "example.com"? It should be ssh://git@git.example.com/repository.git. or what should be ?
  • @egemenozkan: It should be the git server you're pushing to. It's usually git@github.com if your repository is on Github.
  • @KamalNasser I did everything in first tutorial. I also did everything in this tutorail. But I couldn't understand how can set droplet as a git server for remote access from eclipse, or etc.. I have only an IP. I think that tutorials should be prepared for dummies. I have only one user and it is root. I don't have a user which is called git. and I have only repository "testing" like on tutorial.
  • @egemenozkan: This article does not walk you through setting up a git server on your vps - it simply guides you how to use git effectively, and is not directly related to VPSes at all.
  • @egemenozkan At first the path is wrong: ssh://git@git.domain.tld/repository.git It should be something like: ssh://git@git.domain.tld:repository.git What it does mean ? - The server has a user named "git" if you are hosting in your account use "user" instead. The purpose of the git account is obviously to avoid other people to use your own private "user" one. - The server is hosted on git.domain.tld - The path from the account root (here git) to reach the directory is /repository.git, it might be something like "/home/user/git/testing/.git/" instead So according to the example above, you should use the following address (assuming "host" is the correct domain): ssh://user@host:/home/user/git/testing/.git Still there is 2 things to think about to let this work well: 1. Your client will be trying to connect via ssh to the "git" (or "user") account, normally you will be asked for it's password, to avoid being asked all the time you must add your SSH public key to the server's known host. Search google for gitosis keydir if you are using the "git" user, "adding public key to known host" otherwise. 2. The site is talking about a ".git" folder but is actually mentioning a "repository.git" folder. Assuming you had your project on your client (with it's .git folder inside) and that you never pushed on any server. You can create the repository.git using the command "git clone --bare yourClientGitFolder repository.git" Then just put this "repository.git" file in a place your git client will be able to find it.
  • A small tweak is suggested at http://toroid.org/ams/git-website-howto The post-receive file would be: #!/bin/sh GIT_WORK_TREE=/var/www/www.example.org git checkout -f make sure the post-receive file is executable by the git user chmod +x hooks/post-receive and that the web directory is writable This worked like a charm for me.
  • I'm getting this error when trying to push up to the remote repo on the server: git push deploy master Counting objects: 84, done. Delta compression using up to 2 threads. Compressing objects: 100% (57/57), done. Writing objects: 100% (59/59), 246.00 KiB | 169.00 KiB/s, done. Total 59 (delta 27), reused 0 (delta 0) fatal: Out of memory, malloc failed (tried to allocate 67108865 bytes) error: unpack failed: unpack-objects abnormal exit Any ideas?
  • @tommillard81: Your droplet is running out of memory. Try adding swap: https://www.digitalocean.com/community/articles/how-to-add-swap-on-ubuntu-12-04.
    by Etel Sverdlov
    Linux swaps allow a system to harness more memory than was originally physically available. Here's how to set up a linux swap file on Ubuntu 12.04
  • I'm permission error when trying to pull down repo I created on the server using:
    $ git clone ssh://git@git.my_website.io/myproject/myproject.git

    I do I need to create the git user for this to work?

  • Here are the steps you need for pushing local machine code changes to your VPS:

    Follow the steps mentioned in the article above before the "Pushing changes to a remote server" section.

    On your local machine command line:

    git remote add origin root@<YOUR_VPS_IP_ADDRESS>:/home/<FOLDER_ON_VPS_WHERE_YOU_GIT_INIT>/.git
    

    If you want to check which Git remotes you have on your local machine:

    git remote -v
    

    If you want to remove existing Git remotes on your local machine:

    git remove rm origin
    
    • Ok, sooo if I follow the guide and your comment I can develop in my local pc upload to github repository and publish changes in my DO server?

      Thanks in advance.

  • Actually there's a great gem called Git-deploy that actually handles all git hooks to deploy

  • Hey Jason I appreciate the article but I would also add a caveat that if you initialize a repository on github with a README.md file then the initial commit won't go through. I realize that this is a tutorial series on Git but most people will probably be using github and I couldn't figure out why my initial commit wasn't pushing. Lost a few hours on that one lol! But thanks again for the series!

  • I am getting a 'command not found' error when I enter in user@host. For example myuser@my.ip.address. Does anyone know what I am missing?

Previous 1 2 Next
Creative Commons License