Git Explained: For Beginners
Join the DZone community and get the full member experience.Join For Free
i’m working with git now for about two years but only for my personal projects and those i have on github. at work we still use tfs and svn (as of now). recently paolo perrotta came to our company to hold a course about agile planning and since git was quite new to most of my mates, he also quickly explained git in the context of refactoring. i really liked his approach of explaining it and that’s why i’d like to replicate his explanation here.
just before we start..
how is git different from other vcs (version control systems)? probably the most obvious difference is that git is distributed (unlike svn or tfs for instance). this means, you’ll have a local repository which lives inside a special folder named
and you’ll normally (but not necessarily) have a remote, central repository where different collaborators may contribute their code. note that each of those contributors has an
of the repository on their local workstation.
git itself can be imagined as something that sits on top of your file system and manipulates files. even better, you can imagine git as a
each commit creates a new node
in that tree. nearly all git commands actually serve to navigate on this tree and to manipulate it accordingly.
git is just as legitimate in the enterprise as any other vcs.
as such in this tutorial i’d like to take a look at how git works by viewing a git repository from the point of view of the tree it constructs. to do so i walk through some common use cases like
- adding/modifying a new file
- creating and merging a branch with and without merge conflicts
- viewing the history/changelog
- performing a rollback to a certain commit
- sharing/synching your code to a remote/central repository
here’s the git terminology:
- master - the repository’s main branch. depending on the work flow it is the one people work on or the one where the integration happens
- clone - copies an existing git repository, normally from some remote location to your local environment.
- commit - submitting files to the repository (the local one); in other vcs it is often referred to as “checkin”
- fetch or pull - is like “update” or “get latest” in other vcs. the difference between fetch and pull is that pull combines both, fetching the latest code from a remote repo as well as performs the merging.
- push - is used to submit the code to a remote repository
- remote - these are “remote” locations of your repository, normally on some central server.
- sha - every commit or node in the git tree is identified by a unique sha key. you can use them in various commands in order to manipulate a specific node.
- head - is a reference to the node to which our working space of the repository currently points.
- branch - is just like in other vcs with the difference that a branch in git is actually nothing more special than a particular label on a given node. it is not a physical copy of the files as in other popular vcs.
i do not want to go into the details of setting up your workstation as there are numerous tools which partly vary on the different platforms. for this post i perform all of the operations on the command line. even if you’re not the shell-guy you should give it a try (it never hurts ;) ).
to setup command line git access simply go to git-scm.com/downloads where you’ll find the required downloads for your os. more detailed information can be found here as well .
after everything is set up and you have “git” in your path environment variable, then the first thing you have to do is to config git with your name and email:
$ git config --global user.name "juri strumpflohner" $ git config --global user.email "email@example.com"
lets get started: create a new git repository
before starting, lets create a new directory where the git repository will live and
$ mkdir mygitrepo $ cd mygitrepo
now we’re ready to initialize a brand new git repository.
$ git init initialized empty git repository in c:/projects/mystuff/temprepos/mygitrepo/.git/
we can check for the current status of the git repository by using
$ git status # on branch master # # initial commit # nothing to commit (create/copy files and use "git add" to track)
create and commit a new file
the next step is to create a new file and add some content to it.
$ touch hallo.txt $ echo hello, world! > hallo.txt
again, checking for the status now reveals the following
$ git status # on branch master # # initial commit # # untracked files: # (use "git add <file>..." to include in what will be committed) # # hallo.txt nothing added to commit but untracked files present (use "git add" to track)
the file for committing we need to
it to git using
$ git add hallo.txt
checking for the status now indicates that the file is ready to be committed:
$ git status # on branch master # # initial commit # # changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: hallo.txt #
we can now
it to the repository
$ git commit -m "add my first file" 1 file changed, 1 insertion(+) create mode 100644 hallo.txt
it is common practice to use the “presence” in commit messages. so rather than writing “added my first file” we write “add my first file”.
so if we now step back for a second and take a look at the tree we would have the following.
state of the repo tree after 1st commit
there is one node where the “label” master points to.
add another file
lets add another file:
$ echo "hi, i'm another file" > anotherfile.txt $ git add . $ git commit -m "add another file with some other content" 1 file changed, 1 insertion(+) create mode 100644 anotherfile.txt
btw, note that this time i used
git add .
which adds all files in the current directory (
from the point of view of the tree we now have another node and master has moved on to that one.
create a (feature)branch
branching and merging is what makes git so powerful and for what it has been optimized, being a distributed version control system (vcs). indeed, feature branches are quite popular to be used with git. feature branches are created for every new kind of functionality you’re going to add to your system and they are normally deleted afterwards once the feature is merged back into the main integration branch (normally the master branch). the advantage is that you can experiment with new functionality in a separated, isolated “playground” and quickly switch back and forth to the original “master” branch when needed. moreover, it can be easily discarded again (in case it is not needed) by simply dropping the feature branch.
but lets get started. first of all i create the new feature branch:
$ git branch my-feature-branch
$ git branch * master my-feature-branch
we get a list of branches. the
in front of
indicates that we’re currently on that branch. lets switch to
$ git checkout my-feature-branch switched to branch 'my-feature-branch'
$ git branch master * my-feature-branch
note you can directly use the command
git checkout -b my-feature-branchto create and checkout a new branch in one step.
what’s different to other vcs is that there is only one working directory . all of your branches live in the same one and there is not a separate folder for each branch you create. instead, when you switch between branches, git will replace the content of your working directory to reflect the one in the branch you’re switching to.
lets modify one of our existing files
$ echo "hi" >> hallo.txt $ cat hallo.txt hello, world! hi
…and then commit it to our new branch
$ git commit -a -m "modify file adding hi" 2fa266a] modify file adding hi 1 file changed, 1 insertion(+)
note , this time i used the
git commit -a -mto add and commit a modification in one step. this works only on files that have already been added to the git repo before. new files won’t be added this way and need an explicit
git addas seen before.
what about our tree?
so far everything seems pretty normal and we still have a straight line in the tree, but note that now
remained where it was and we moved forward with
lets switch back to master and modify the same file there as well.
$ git checkout master switched to branch 'master'
$ cat hallo.txt hello, world!
lets change and commit it on master as well (this will generate a nice
$ echo "hi i was changed in master" >> hallo.txt $ git commit -a -m "add line on hallo.txt" c8616db] add line on hallo.txt 1 file changed, 1 insertion(+)
our tree now visualizes the branch:
merge and resolve conflicts
the next step would be to merge our feature branch back into
. this is done by using the merge command
$ git merge my-feature-branch auto-merging hallo.txt conflict (content): merge conflict in hallo.txt automatic merge failed; fix conflicts and then commit the result.
as expected, we have a merge conflict in
hello, world! <<<<<<< head hi i was changed in master ======= hi >>>>>>> my-feature-branch
lets resolve it:
hello, world! hi i was changed in master hi
..and then commit it
$ git commit -a -m "resolve merge conflicts" [master 6834fb2] resolve merge conflicts
the tree reflects our merge.
fig 1: tree state after the merge
jump to a certain commit
lets assume we want to jump back to a given commit. we can use the
command to get all the sha identifiers that uniquely identify each node in the tree.
$ git log commit 6834fb2b38d4ed12f5486ebcb6c1699fe9039e8e merge: c8616db 2fa266a author: = <firstname.lastname@example.org> date: mon apr 22 23:19:32 2013 +0200 resolve merge conflicts commit c8616db8097e926c64bfcac4a09306839b008dc6 author: juri <email@example.com> date: mon apr 22 09:39:57 2013 +0200 add line on hallo.txt commit 2fa266aaaa61c51bd77334516139597a727d4af1 author: juri <firstname.lastname@example.org> date: mon apr 22 09:24:00 2013 +0200 modify file adding hi commit 03883808a04a268309b9b9f5c7ace651fc4f3f4b author: juri <email@example.com> date: mon apr 22 09:13:49 2013 +0200 add another file with some other content commit aad15dea687e46e9104db55103919d21e9be8916 author: juri <firstname.lastname@example.org> date: mon apr 22 08:58:51 2013 +0200 add my first file
take one of the identifiers (also if it isn’t the whole one, it doesn’t matter) and jump to that node by using the
$ git checkout c8616db note: checking out 'c8616db'. you are in 'detached head' state. you can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. if you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. example: git checkout -b new_branch_name head is now at c8616db... add line on hallo.txt
note the comment git prints out. what does that mean? detached head means “head” is no more pointing to a branch “label” but instead to a specific commit in the tree.
you can think of the head as the “current branch”. when you switch branches with
git checkout, the head revision changes to point to the tip of the new branch. … it is possible for head to refer to a specific revision that is not associated with a branch name. this situation is called a detached head . stackoverflow post
basically when i now change hallo.txt and commit the change, the tree looks as follows: detached head state
as you can see, the newly created node has no label on it. the only reference that currently points towards it is
. however, if we now switch to master again then the previous commit will be lost as we have no way of jumping back to that tree node.
$ git checkout master warning: you are leaving 1 commit behind, not connected to any of your branches: 576bcb8 change file undoing previous changes if you want to keep them by creating a new branch, this may be a good time to do so with: git branch new_branch_name 576bcb8239e0ef49d3a6d5a227ff2d1eb73eee55 switched to branch 'master'
and in fact, git is so kind to remind us about this fact. the tree looks now again as in figure 6.
jumping back is nice, but what if we want to undo everything back to the state before the merge of the feature branch? it is as easy as
$ git reset --hard c8616db head is now at c8616db add line on hallo.txt
the tree after the reset
the generic syntax here is
git reset --hard <tag/branch/commit id>
using “revert” to rollback changes the nice way
if you need to rollback an entire commit and (even worse) you might have synched it to a remote repository already, then using
git reset --hard
might not be so nice as in that way you’re somehow rewriting history which is a no-go if you synched your repo to a remote one already.
in such situation you can use the
command which basically creates a new commit undoing all changes of a specific commit you specify. for instance consider you want to rollback a commit with id
$ git revert 41b8684
sharing/synching your repository
ultimately we want to share our code, normally by synching it to a central repository. for doing so, we have to add a remote .
$ git remote add origin email@example.com:juristr/intro.js.git
to see whether i succeeded, simply type:
$ git remote -v
which lists all of the added remotes. now we need to
publish our local branch master
to the remote repository. this is done like
$ git push -u origin master
and we’re done.
the real powerful thing is that you can add multiple different remotes. this is often used in combination with cloud hosting solutions for deploying your code on your server. for instance, you could add a remote named “deploy” which points to some cloud hosting server repository, like
$ git remote add deploy firstname.lastname@example.org:juristr/myproject
and then whenever you want to publish your branch you execute a
$ git push deploy
similarly it works if you’d like to start from an existing remote repository. the first step that needs to be done is to “checkout” the source code which is called cloning in git terminology. so we would do something like
$ git clone email@example.com:juristr/intro.js.git cloning into 'intro.js'... remote: counting objects: 430, done. remote: compressing objects: 100% (293/293), done. remote: total 430 (delta 184), reused 363 (delta 128) receiving objects: 100% (430/430), 419.70 kib | 102 kib/s, done. resolving deltas: 100% (184/184), done.
this will create a folder (in this case) named “intro.js” and if we enter it
$ cd intro.js/
and check for the remotes we see that the according tracking information of the remote repository is already set up
$ git remote -v origin firstname.lastname@example.org:juristr/intro.js.git (fetch) origin email@example.com:juristr/intro.js.git (push)
we can now start the commit/branch/push cycle just normally.
resources and links
the scenarios above were the simples, but at the same time probably also the most used ones. but there’s a lot more git is capable of. to get more details you may want to consult the links below.
Opinions expressed by DZone contributors are their own.
What Is Envoy Proxy?
How Web3 Is Driving Social and Financial Empowerment
The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables