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.
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).
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.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs