Learn the basics of Git, including installation, essential commands like init, clone, add, commit, push, and pull, and key Git concepts such as repositories, branches, and merges. Ideal for beginners looking to get started with version control and code collaboration.
This is the first ever blog post of our new software engineering interns—Daniel, Simon, and Till. We'd love to hear your feedback!
Git is an open source version control system, used to store and manage code, maintain different versions of the program, share and collaborate with others and restore lost or broken code. While it offers a wide range of functionalities and options, only a few basic commands are necessary for the most common types of operations and becoming productive quickly.
Hint: You can find the official documentation at git-scm.com/docs.
Before you can start using Git, you need to install it on your computer. You can find the installation instructions for your operating system at git-scm.com/downloads. In general, a good starting point would be to have:
Before we begin creating our first repository, we need to configure our git user profile. This will later be displayed in commits, to track which user made a specific change. In our command line we run the following commands (substitute your own e-mail and name):
git config --global user.email "you@example.com" git config --global user.name "Your Name"Then, we can check if the configuration was successful by running:
git config --list
This should display the user name and e-mail address we just set.
Before we start, let’s clarify some terminology:
git init
The init
command turns the current directory into a new Git repository. Let's first navigate to a folder
that we want to turn into a repository and run the command:
git init
The chosen directory should now contain a single folder, '.git', which contains everything Git needs to track changes to this directory and manage branches (the folder is hidden and may not be displayed in your OS by default). For now this is only a local repository, since it has not yet been connected to any remote Git storage such as GitHub, GitLab or similar.
git clone
Instead of initializing our own fresh repository, we can also use the clone command to download a copy of an existing repository. In a new empty directory, run for example:
git clone https://github.com/golang/go.git
This will create a local copy of the Golang repository with all of its branches. The URL can be found on the main page of the repository, and can be copied by clicking on the green "Code" button. Cloning can also be done using SSH, if you have set up SSH keys for your GitHub account. In this case, the URL would look different.
git add
Let's navigate back to our first directory and create a new file:
touch test.txt echo "Hello World!" > test.txt
If we now run the command git status
to see the current state of our repository, we will see that the
file is untracked. This means that Git is not yet aware of it, and it will not yet be included in the next
commit:
Untracked files: (use "git add <file>..." to include in what will be committed) test.txt
We have not yet told git which files should be included in its tracking. To start tracking our newly created text file, we can run:
git add test.txt
– or we are feeling lazy and simply want to add everything in the directory at once, we could use
git add .
to recursively add everything new or changed in the directory and its subdirectories:
If we now run git status
again, we will see that the file is now staged for commit:
Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: test.txt
The file has been staged, i.e. queued up for inclusion in a new version/revision of our repository, but not yet committed.
git commit
We use the commit command to take all our staged changes and create a new commit, essentially a snapshot of the current state of our project that we can build on and possibly revert to at a later point. Let's do this in our repository now:
git commit
If no options are given, git will open the configured default editor for writing a commit message. Here we could add a title and description below the commented lines like so:
# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # On branch master # # Initial commit # # Changes to be committed: # new file: test.txt # Added test.txt to the repo This is the description, explaining the commit in more detail. Anyone else reading this should be able to tell what the commit contains and why it was made.
By convention, the first line should be the title, a single sentence explaining what the commit is about. The following text, separated by a blank line, can be used to further describe and explain the commit. Let's save the file and then close it. Git should recognize this and create the commit.
Note: If we want to change the default editor to, for example VS Code, we can do so by running
git config --global core.editor "code --wait"
. Other editors can be set in a similar way, e.g.
git config --global core.editor "nano"
for nano or git config --global core.editor "vim"
for
vim.
git remote
At this point, our repository still exists only on our local machine. For purposes of backup and collaboration, we may want to maintain our repository on some kind of online platform. For this example we will use GitHub, but any alternatives such as GitLab or Bitbucket will work as well, as long as you have a working link and access to an online repository.
Let's assume we have set up a fresh repository at https://github.com/gentlent/test_project.git
. We can
now run the remote command, connecting our local repo to the remote one:
git remote add origin https://github.com/gentlent/test_project.git
We then use git remote -v
to verify. It should show `origin` followed by our URL.
git push
We are now ready to push our previously created commit to the new remote repository and synchronize with it. Let's run
a git push
with some flags—origin
being the default name for our remote, and
main
our own
branch name:
git push -u origin main
Note: The default branch name used to be master
in the past. Many platforms are nowadays
defaulting to main
instead. If the above command causes an error, check your branch name with
git status
.
Using the -u
option flag causes git to associate the local branch with the remote repository. Any future
push
command will automatically push from main
to origin/main
without us
needing to specify the targets.
Checking our remote repository on GitHub, we can now see our text file has been uploaded.
git pull
If we have made changes to our remote repository, we can pull them into our local repository using the
git pull
command. This will download any changes made to the remote repository and merge them into our
local copy.
For example, if we have made changes to the test.txt
file on GitHub, we can run:
git pull
This will download the changes and merge them into our local copy of the repository. If there are any conflicts, Git will prompt us to resolve them before completing the pull. If we do this now, we will get the message Already up to date, because Git has fetched, determined that there were no new changes and skipped merging anything.
The above covers a bare minimum of functionality to start interacting with the git ecosystem. There are many more
useful commands, particularly in regards to managing different branches, which are not explained here. You can find a
more complete list of commands and their descriptions in the official documentation at git-scm.com/docs or by trialing them out in your command line. If you ever have
a question about a command, you can run git help <command>
to get a short description of what it
does and how to use it.
Daniel Stienen
External
Simon Reher
External
Till Preuß
External
Gentlent
Customer Support
support@gentlent.com