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
-
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
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.