Getting started with Git

Getting Started

To start your first Git project, create a subdirectory for your new project somewhere and run git init. The script reports that a correctly configured Git repository resides in this directory.

The next step is to create a file. I use Vi, but you can use Emacs or any other editor and add a line to your newly created file (Figure 7). Type git add <filename> to add it to your local staging area and later commit it to the repository.

Figure 7: A first, but typical Git session that shows how helpful the prompt tool can be.

You can – in fact should – enter a comment describing your changes. A comment will help your teammates understand what you did. By the way,

git add .

adds local files to your staging area.

After a short status check with git st, the user has decided to have Git display the protocol of the recent actions. You can see in Figure 7 that a file has been created, deleted, and another one created. If you prefer a graphical tool to manage your Git activity, try Gitk [5] (Figure 8).

Figure 8: Gitk is a very helpful graphical tool for any work with Git.

Remote Repositories

You have already used one of the commands relevant for dealing with remote repositories: clone. For a beginner, it will be sufficient to add a new project at one of the websites offering Git project hosting. Most likely, you will end up at GitHub or Bitbucket. Get an account, log in, and add a new project; then, either read the guidelines or simply click on the "+" sign in the top right of the website's menubar. Next, you can name your project, and you will receive the URLs needed for your uploads. You'll be asked to enter your credentials regularly during your work with remote repositories, so prepare and remember the password.

If you haven't cloned a remote directory, and you want to upload your local files to an existing GitHub repository,

git remote add origin https://github.com/mfeilner/project0

defines the remote repository that will be connected with your local files. The command

git push -u origin master

pushes all the files you changed to the repository. Normally, a git pull should be run before you push any changes to the remote repository. If you work with somebody else's project, you'll have to clone the project before you start working with it, and Git will keep the remote repository's address.

Conflicts, Merges, Branches

As described previously, a merge brings together concurring versions from different branches and gives the committer options for how to solve the conflict. The basic command for merging a branch into the current branch is:

git merge <branch name>

The merging process can become much more elaborate with complex projects. See Git's Beginner's Guide for Dummies [6] for more on how to create and solve conflicts using git merge, and the tutorial from Atlassian [7] for more on the workflows behind Git and how they affect the way your team works. Developers use branches, merges, and special kinds of workflows to avoid and solve conflicts [8].

Git offers an API for merge tools that specialize in getting diverging branches back together. The commands

git mergetool --help
git config --global.merge.tool tool_name

show the man page and set your favorite program called by git merge. Apart from that, another way to combine changes of several people is to use git rebase, which basically goes back to the common ancestor, gets the diffs, and applies all changes since your branch took its own path.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Branch Management

    Git makes version control as simple as possible. To manage your Git branches successfully, you need to learn the ins and outs of git branch and git merge.

  • Git Ready

    If you develop open source software, you need to know how to use Git. Information on Git fills books, but this basic overview will get you started with a vital open source development tool.

  • Remote Git Repositories

    Software projects often comprise several code branches, some of which exist in parallel. Git supports community code development through remote repositories and code branching.

  • Tree View

    Complex Git projects sometimes require a better view of the dependencies and branches. Several tools offer GUI options for Git. We take a look at gitk, gitg, git-gui, and GitAhead.

  • Git 101

    When several people collaborate on source code or documents, things can get messy fast. Git provides a quick cure: The distributed versioning system reliably ensures the integrity and consistency of data with minimal effort.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News