Git - Source Code Management for FOSS
Get started using Git in the command line on any operating system.
Introduction #
Before Git, most developers used BitKeeper. Developers contributing to a project needed the repository's owner to approve branch creation. You couldn't drive the project in another direction on your own. Taking a project in a different direction provides the foundation for FOSS development. Without it, development would become too centralized.
Git solved this with a distributed model, letting developers branch whenever and however they want. While cloning became necessary to contribute to a repository, branching and merging skyrocketed in efficiency, which let FOSS and proprietary software development skyrocket.
This tutorial will help you get started using Git.
This tutorial covers git init
, git add
, and git commit
.
Those commands are enough to get you started using Git for your project.
Requirements #
- Git
- terminal application
- POSIX-compliant shell (e.g. Bash)
- GNU Coreutils
Author Recommends
This tutorial will assume you use a POSIX-compliant shell on a Linux system with GNU coreutils because I recommend you run a distribution of Linux.
Windows: To use Bash and GNU coreutils on Windows, install the Linux Subsystem for Windows
OS X: Bash and the FreeBSD coreutils are already installed on OS X, and the commands listed in this tutorial should work as listed, but you can install the GNU coreutils with the link above.
Get Started #
Terminal #
Open a terminal in a terminal app. You will need a terminal for this tutorial.
Verify Git Installation #
Git should be installed. To verify if git is installed, enter the following command in the terminal:
$ git --version
git version 2.45.2
If the output isn't similar to the above output, install Git.
Project Directory #
Create a directory where the project will live. To use Git for the project:
- Navigate to where the project directory will be created.
- Create the directory to use for the project.
- Navigate to the newly created directory.
Create Directory #
To navigate to where your project's directory will be created, run cd
.
To navigate to a specific directory, include it as an argument to the command.
This command will navigate to the user's home directory.
$ cd
To create a directory for the project, run mkdir
.
This command will create a directory named project-1
.
$ mkdir -p project-1
Navigate to Project #
Navigate to the newly created project directory with the cd
command.
$ cd project-1
Create Repository #
To use Git in our project, initialize a Git repository at the root of our
project with the git init
command.
$ git init
Initialized empty Git repository in .../project-1/.git/
If the output returns an error, check to see that you have permission to write to the current directory.
To verify, use the git status
command.
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
Author Recommends
I recommend thinking of Git as a "version manager" that lets you register new versions, branch from other versions, and change the current version of a project. People can work on different versions at the same time. Teams can take the project in multiple directions and implement features or fix bugs without each other's approval.
Create New Version #
Git keeps track of the state of the repository as a whole, not by file. A change in state of the project can be adding, modifying or deleting one or more files. Git lets you register this change in state as a new version.
To create a new version for your repository:
- Modify project state (add / modify / delete at least 1 file)
- Track the state change with
git add
- Create the new version with
git commit
1. Modify Project State #
To modify the project state, add, modify, or delete one or more files.
One way is to add a file named file.txt
with the line "This is file.txt"
written in it.
This can be done in one command shown below:
$ echo "this is file.txt" > file.txt
To verify that the project state has been modified, run git status
.
The output should contain Untracked files
.
$ git status
...
Untracked files:
(use "git add ..." to include in what will be committed)
file.txt
...
2. Track State Change #
To track the state change, run git add
and add any untracked files as
arguments.
Add the newly created file.txt
as shown below.
$ git add file.txt
To verify that the state has changed, run git status
.
The output should contain Changes to be committed
.
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: file.txt
3. Create New Version #
To create a new version, use git commit
.
You can add a commit message to a commit with -m
as an option and specify the
commit message as an argument.
This command commits the state changes to a new version and adds "Added
file.txt" as a message.
$ git commit -m "Added file.txt"
[master (root-commit) 7166762] Added file.txt
1 file changed, 1 insertion(+)
create mode 100644 file.txt
You can continue making edits and new versions to eternity.
Conclusion #
Git can enhance your experience managing versions of your software projects. Git's main use case lies in collaborative software development. Git serves as the backbone of a majority of the world's development, and according to this survey from StackOverflow, is preferred by software developers over other version control managers. In future tutorials, we'll cover branching, remotes, and fancier Git functions.
License #
This work is licensed under CC BY 4.0.