A simple step-by-step guide to using the Git version control system in everyday work. You will learn how to install and use Git in the terminal, working locally or with a remote repository such as the popular GitHub.
Why Git
Git is a tool that helps you develop and maintain projects. It gives one developer or whole teams a system for versioning files. Without it, many people working on the same code (on many versions of it) would be torture.
A real-life example. Say your job is making fixes to clients' websites. You do it in a not very safe but oh so common way - changing the live site directly, because there are no separate dev and prod versions. So you download a copy of the files (a backup), work on them and upload the changed versions to the server. What if you have to undo your changes? You have to dig the original files out of the backups. And sometimes it is the umpteenth fix in a row. Do you have a backup after every fix? Exactly 😉 All you need is to initialise a repository and save after every fix. It is a very quick and convenient process. Then a single command takes you back to whichever version you want!
Another example is working on a project (a CRM system, say) in a team. The days when everyone had their own FTP account and uploaded their modules, which admins then copied to the production server... I hope they are over. With the Git version control system, every developer adds their portion of code to the repository and sees straight away whether they changed something in the same file as another developer. They can react by adjusting their code, because they see the code and the changes of all their colleagues. If needed, they can undo changes easily, because the full history of changes is there. When the team decides the code is stable, it can be merged into the main branch, from where it can very easily go to the right server, e.g. a test one.
Git as a command-line tool, which is what this guide is about, lets a developer put code together, save it and send it to the server quickly and clearly. On the other side, on the server, there may also be a "layer" on top of it, such as GitHub, GitLab or something similar. It lets you browse and work with the code on the web, which makes things like code reviews or managing the whole project much easier.
Installing Git
Installation differs from system to system. This guide is aimed at Linux users because of the terminal examples, but Git installs without any trouble on Windows too (see the instructions on the Internet). There you can also use the same commands as on Linux if you use WSL.
On Debian/Ubuntu one command in the terminal is enough (you can open it with Ctrl + Alt + T):
sudo apt-get install git
The installation itself needs no extra setup, but we have to tell Git a bit about ourselves so it knows how to sign our changes 😉 So let's set the required email and name:
git config --global user.email "[email protected]"
git config --global user.name "First Last"
Great! Now when we save some changes, we will see our name and email address as the author:

This sets the name and email globally, that is for every repository we use on our user account. Instead of the --global switch you can use --system to change the settings for all accounts on the system. Most often, though, you will want the --local switch, which changes the settings for the repository you are in right now. After all, you may want to "sign" each repository differently (work / open source / private project and so on).
A local Git repository
A local repository makes working with your project's files much more convenient and safer. True, we are not sending our changes anywhere yet, so convenience beats safety - but it is the first step to working better with code. And sometimes enough, if you make backups the "traditional" way anyway.
In the terminal, go to the folder where you want to keep your project's files, or where you already have some files (downloaded from FTP, say) that you want to version. Then run the command that creates (initialises) a new repository:
git init
Whether the folder already had files or was empty, after this command Git creates a hidden .git subfolder in it (if it already exists, Git tells you so and stops). In this subfolder Git keeps all the information about the repository, such as the versions of the data and the settings.

That is how Git knows the folder is a repository, and it compares what is in the folder with what it keeps in its subfolder.
At this point we have a new repository that detects changes in files, but nothing has been saved yet - we should add the files to be saved and finally save them, which is usually just two commands. So let's move on to tracking files and committing changes.
To keep things simple, you can "walk through" this guide in a single terminal window, without distractions. Of course, you can also open the repository folder in your editor or IDE, such as Visual Studio Code, and create and edit files there.
Tracking files
The first step before saving is marking files as tracked by Git. Let's see where we stand by running:
git status
For now we only learn that we are on the master branch (the first, default branch), we have not saved anything yet and have no changes to commit. We will come back to branches later.
For a while now GitHub, and since version 2.28 Git itself (the init.defaultBranch setting), let you name the first branch main, and more and more projects do. This guide keeps master, but all the commands work the same - just swap the name.

Now, without leaving the terminal, let's create two files with the touch command and run git status again. This time we also learn there are untracked files, and get a hint on how to start tracking them:

Git suggests running "git add <file>". That way you can track single files, for example if you only want to save some of them later. To add both files, you can type the command twice:
git add index.php
git add readme.md
or list them separated by a space:
git add index.php readme.md
or, if you know you want to add all files, just use a dot:
git add .
All of these commands have the same result - both files are now tracked. Let's see what "git status" says now:

As you can see, Git now tracks our files. It also tells us we can stop tracking them with git rm --cached <file>. If we did, we would be back to the state before git add, with the file marked as untracked. You can also untrack all files with git reset.
Now let's change the content of one of them - add some text to readme.md and run git status again. To avoid opening an editor, I will do it from the terminal with echo, which, much like in PHP, prints a string. Then the "create or overwrite" operator > sends that string into readme.md instead of the terminal:

We added content to a previously empty file, so we changed a tracked file. Now Git tells us there are two tracked files and that one of them has changes not staged for commit. This means that when we ran git add, Git remembered the state of the files we gave it. If we change something in them again, Git notices that their content differs from what we "marked" with the tracking command. This is a good moment to save our changes for good.
The Git commands used so far:
git init- creates a repository in the foldergit status- checks the current state of the repogit add- adds files to tracking
Extra commands to undo git add:
git rm --cached <file>- stops tracking a given filegit reset- stops tracking all files
Saving changes
Files marked for tracking (shown in green after git add) can be saved for good as a commit. That creates a point in the repository's history that you can come back to when needed. To see your commits, run git log. Of course, our repository has none yet. So let's add the first commit (remember we have two "green" files and one "red" one).
You can add a commit in two ways. Simply by running the command:
git commit
This starts an "interactive" mode, i.e. opens the default editor (nano in my case), where you add a description of your change and save the commit with the editor's save option.
It is much quicker to use the -m (message) switch and give the description right in the command:
git commit -m "Init a git15 repo!"
Before we save our first commit, it is worth knowing how to describe it. It is extremely important, one of those "I know what this is about now, but tomorrow I may forget" things. So for your own sake and your colleagues', here is a great article on how to write good commit messages.
If, in the git commit -m ... command above, you press Enter before the closing quote, the command will not run but move to the next line - handy if you want to separate a title from a longer description (as the article on good commits suggests) without opening the editor.
So we run git commit -m "Init a git15 repo!". This time we also run git log to see what is saved (our current commit) and, as usual, check the state of the repo:

After the commit command, Git tells us it saved a commit on the master branch, which is the main branch (the root of our tree), that its short unique identifier (hash) is 4a7e419 and that its message is "Init a git15 repo!". Then we learn that 2 files changed (they did not exist in the repo, so they changed), with 0 lines added or removed (well, the files were empty). Finally, the files are listed with the operations done on them.
Next we open the change log (git log). There is one commit in it, with the identifier (hash) 4a7e419... (unlike the commit output, we see its full value here). We also see the author (as set during configuration), the commit date and the message.
The status shows that the "green" files are gone (they were committed). As you can see, the "red" file, the changes we made after git add ., was not committed and is still waiting. This means that after every change to the files you work on, Git expects you to decide whether to stage their current state (prepare it for the commit). That is why it is always worth checking the state of your repository before git commit, to avoid partial commits 🙂
When you finish a piece of code and want to save it to the repository, you do 3 steps:
git status- to see where you stand 😉git add <files or a pattern>- to pick the files to savegit commit -m "Description of the changes"- to save the changes
Undoing changes
We have made our first commits, and one "red" file, readme.md, is waiting to be staged and saved. What if we wanted to undo the changes in it but could not do it "by hand" in the editor (with Ctrl + Z, say), and did not know what was there before, so changing its content is not an option either?
Putting changes away "for a moment"
A very convenient and safe way of undoing changes is the stash. The stash is a place in Git where you can drop unrelated versions of files (unlike committing on a branch). It is very handy when you are in the middle of something and need to get back to a "clean" state for a moment, to fix something or look at the original files. Using it is very simple, just run git stash:
git stash

This saves all the changed files to the stash. As the status shows, the working tree is clean. The stash works like a stack, so if you want to bring back the latest entry (which is what you do most of the time), just run:
git stash apply

We are back where we were a moment ago. Of course, in the long run it is hard to work only with the latest entry, so for changes that will stay around longer it is worth adding your own description, as with a commit. Let's create one more file with touch robots.txt and try the stash again, this time with a sample description. We use the push subcommand with the -m (message) flag.
git stash push -m "Fix #1 but #2 crashed"

This time the working tree is not clean - the newly created robots.txt is left. That is because by default git stash saves only tracked files (in general, Git only cares about tracked files). Let's go back with git stash apply, then add all files to tracking with git add . and stash the changes again:

Great, now everything is tidy 🙂 Let's see what is in the stash with git stash list:
git stash list

Now we see the stash stack. First we put in some changes from the master branch, which Git described automatically as "WIP on master <summary of the last commit>" (WIP = work in progress), and then we added changes twice with the message "Fix #1 but..." (because we were cleaning up a small mess).
To bring back a specific entry from the list, use its identifier "stash@{<int>}" in git stash apply. Let's go back to our first entry:
git stash apply stash@{2}

This loads the state of the files before our new robots.txt existed. It is not our latest state, and we could stash it again to load the newer version, but is another stash entry worth it? Let's move on to the next section and see how to drop changes in files for good.
To undo changes but be able to come back to them, use the stash:
git stash- stashes all tracked filesgit stash list- lists the stash stackgit stash apply- brings back the latest stashed filesgit stash apply stash@{<int>}- brings back a specific entry
Dropping changes in files
If you are just checking something quickly (debugging, say), or you are sure you will not need the changes, you can drop them for good. There are two ways. With git reset and the --hard option:
git reset --hard
As the name suggests, this option is for real tough guys only 😉 It is more precise to drop the changes in a specific file, and most of the time you change only a few files at once anyway. You can do it with the very handy git checkout -- <file> command. We will learn more about the command itself in the section on branches.
Newer versions of Git have a separate, more readable command for this: git restore <file> drops the changes in a file, and git restore --staged <file> takes it out of the staging area. More on undoing changes in Git - undoing changes.

So let's drop the changes in readme.md, which we loaded from a not-the-latest stash entry:
git checkout -- readme.md

As you can see, the changes in readme.md vanished into thin air and our working tree is clean. Let's move on and see how to undo changes we have already saved to the branch with git commit.
Undoing commits
Sometimes you want to undo a whole commit, because you made it by mistake, it has a bug, or you are unhappy with it for any other reason. The safe way is to reverse the changes instead of removing them. This command does exactly what you would do by hand: remove the changes the commit made and save again. Git creates a commit with the changes opposite to the given commit and saves it.
Let's add another file, bad-file.php, and commit it. Since it is a bad file, we now want to undo our last commit with git revert <hash>. To avoid looking up the hash of the last commit, we can use the HEAD pointer. Think of it as "what we are looking at right now". We always look at some branch (now master) and, if there is one, some commit. Wherever we go, HEAD goes 😉
git revert HEAD


After creating the file (the changes), staging and committing it, we felt like undoing the last commit. With git revert we said we want to reverse the last commit using HEAD, instead of looking up its hash in git log (here ed1084e). After the revert command, an editor opens (nano in my case), where you can describe why you are undoing the changes, or just carry on by leaving the editor (in nano, the shortcut shown is Ctrl + X). Then we check the status - the tree is clean. We list the files in the folder - bad-file.php is gone. We check the history with git log, this time with the --oneline option, which shortens entries to the hash and the title (the first line of the message). We see we are on the master branch (HEAD points to it) and that there is a new commit, whose title Git automatically prefixed with "Revert...".
The changes have been safely undone on the branch. We can still go back to them if we want, because we did not destroy any point in the history.
What if, especially when working locally, you want to remove saved changes for good? That is the next section.
Removing commits
Removing commits for good has its uses, for example when the message you added is "ugly" - a typo or the wrong word. But remember that it disturbs the history you have built, and if you later push your work to a remote server where someone else also works on the repo, it can cause trouble.
So let's remove the commit that reverted the changes, i.e. reset the history to the commit before it with git reset <hash>. It is the same command we used to undo git add, but this time we point to a specific commit (by default it is HEAD).
git reset ed1084e

We look at the history and take the hash of the commit we want to go back to, dropping EVERYTHING after it (you cannot just "pull out" one commit from the history). We run git reset <hash> and are told we have unstaged changes. That is because by default git reset runs with the --mixed option, which moves the reset changes (from the given commit) into our working tree. So bad-file.php is back, marked as deleted (because the revert deleted it back then). The point in history is gone, as the log shows.
To see how git reset --hard <hash> works, let's follow Git's advice and restore the file (undo its "deleted" status). Then commit it, so there is something to reset:

Now that there is something to reset again, let's try a hard reset:
git reset --hard ed1084e

We reset to commit ed1084e, this time with the --hard option. The working tree is clean and our previous commit is gone from the history. Since we had restored the "deleted" file earlier, it is back in the list, but as you can see it is empty - the changes added in the earlier step are gone.
To drop changes in the working tree for good:
git checkout -- <file>- drops the changes in a given filegit reset --hard- drops the changes in all files
To undo or remove commits for good:
git revert <hash>- reverses (undoes) the changes of a given commitgit reset <hash>- removes commits up to the given commit, keeping their changes in the working treegit reset --hard <hash>- removes everything up to the given commit
Working with branches
So far we have worked on the master branch, but as the name suggests, it should be the main branch of the repo, the one with the code in its best (production) quality. So let's see what Git offers.
Creating a new branch
I could suggest git branch <branch_name>, which says clearly what it is for, but it is much more convenient and intuitive to create a branch with git checkout, which moves you around, e.g. to another branch or commit (and wherever we go, HEAD goes ;)), and is quite versatile (we used it to drop changes, for example).
We are on the master branch, where we already have some work done. Naturally we want to keep building on it, so we want to base on this code. Let's run git branch <branch_name> to create a new branch:
git branch bad-branch
Then let's create another branch with git checkout (switching to another branch) together with the -b (for branch) flag and the name of the new branch:
git checkout -b develop
Then let's list our branches, finally using git branch:
git branch

This created two branches, bad-branch and develop. As you can see, the difference between the two ways is that the second one automatically moved us to the new branch (in the list, the active branch has an asterisk and is green). That happens to be exactly what you usually want, so since you use git checkout a lot anyway, the shortcut is worth using. Checkout also carries the tracked files along. 3 in 1 😉
Newer versions of Git also have git switch, a command only for switching branches: git switch develop switches, and git switch -c develop creates and switches. It does the same as git checkout, but its name says what it does.
Switching between branches
At work you switch between branches all the time, and the familiar git checkout <branch_name> with the name of the branch you want is all it takes. Since we are on the develop branch, let's go back to bad-branch:
git checkout bad-branch
Then let's make a bit of a mess by deleting index.php and committing. Then switch back to develop:

As you can see, if you want to work on a new feature or just test something, switch to a new branch and everything stays there. But keep your working tree in mind. Untracked files "move" between branches when you switch. Tracked changes move too, but Git tells you about it during or after the switch (if there were no conflicts).
Switching between commits
Just like between branches, you can "go back" to earlier commits, to look at the changes or to start a new branch from there. So let's switch to a specific commit with git checkout <hash>:

As you can see, Git tells us at great length that we are no longer on any branch (our HEAD pointer is detached somewhere else). git log shows the commit we switched to as the newest one (we do not see the newer ones). The status shows we are looking at ed1084e, but not at any particular branch.
Now what? You can run git checkout -b <new_branch_name> to create a new branch from this point in the log. Or you can look at the code at this point in your IDE (the files have been brought back to this state).
To leave the "detached HEAD" state, just switch to some branch, for example with git checkout develop. That's it, we are safe again 😉
Deleting branches
You will rarely delete branches, and you do it either to tidy up because there are too many of them or... no, I can't think of another reason. Plenty never hurts, and who knows when the code from a branch may come in handy 😉
You delete a branch with git branch and the -d (delete) switch, giving the branch name. We are on develop, so we can probably delete the inactive bad-branch:
git branch -d bad-branch

After the delete command, Git tells us the branch is not fully merged and that we should use another option (which we do). That is because when deleting, Git checks whether the branch is up to date with HEAD (where we are now - the develop branch). Changing the lowercase "d" to an uppercase "D" did the same as in many other Linux commands - it added a "force" mode.
Merging branches
The very essence of Git is merging branches together. It is how the work of many people becomes one coherent whole.
So let's create a new "development" branch, add something to index.php, commit, and merge the develop branch into master with git merge <branch_name>.
git merge develop

We added some text to index.php and saved the changes the usual way. A new point appears in the log - HEAD points to develop, and the last commit shared with master is below it. We switch to master, where we see we do not have the newest commit, so we run git merge <branch_name> to merge the changes from that branch. Git says it is updating from <hash> to <hash>, and then that it used the "fast-forward" mode. Since our commits came one after another, Git did not have to merge anything inside the files, so it just "fast-forwarded". Then we see the list of changed files, the number of modified lines and a graphic of the lines added and removed. One plus or minus is one line, and if there were more than fit in the terminal line, the pluses and minuses only show the proportion of added and removed code.
Avoiding conflicts
The road to a merge is sometimes full of conflicts (Git cannot tell which version is the newest). But you can make the problem almost disappear. When you create a new branch from, say, master and work on it for a while, it is practically certain (especially with a remote repo, i.e. other people) that master will have changes your branch does not. Merging into master then would bring a series of conflicts in the files, which you would have to fix by hand.
A good practice is to regularly (e.g. every morning when you start work) merge the "base" branch into your "working" branch. Then you stay aware of what is changing and can resolve any conflicts in small doses, right away.
An even better practice is to do the same right before you try a merge (locally) or push your changes to the server (with a remote repo).
Working this way is highly recommended, for your own sake and your colleagues' 😉 You resolve conflicts as they come, and merges go through without trouble. You can also deliver changes faster when something critical happens in the project and something is needed "right now".
To create or delete a branch:
git branch <branch_name>- creates a new branchgit checkout -b <branch_name>- creates a branch and switches to itgit branch -d <branch_name>- deletes a branch (while on another branch)
To switch to another branch or commit:
git checkout <name/hash>- switches to another branch or commit (and wherever we go, HEAD goes 😉 )
To merge branches:
git merge <branch_name>- merges the branch <branch_name> into the branch you are on
A remote Git repository
Now that we know how to work with a local repository - add, undo and commit changes - it is time to connect to some nice server and push our work there. That keeps the code safer and, of course, lets you share it or work on it together. A remote repository can live, for example, on a company server or with a service provider. You can log in with a username and password or (safer and more convenient) an SSH key. Then you can fetch changes from it and push changes from your local repository.
Creating a remote repository
For a start, let's create a new repository on GitHub, which is very simple - you click through everything in the browser. You can create public and private repositories there, so it is a great place to share your work or keep your projects. Such a repository is no different from the one you will meet at work, so it is a good choice.
After creating an account, I suggest adding your SSH key in the account settings. Then you do not have to keep typing a username and password, and working is much more pleasant and safer.
Once everything is set up, use the "New repository" option and give it a name:


After naming it, you can basically click "Create repository", or first change the visibility from (the default) public to private. You will be taken to the main page of your repository, which shows its address:
[email protected]:duch512/git15.git
Adding the remote server to the local repo
With its address, we can add the remote server to the repo we have been working on:
git remote add origin [email protected]:duch512/git15.git

With git remote add <origin> <address> we added a remote server called "origin" with the address "[email protected]:duch512/git15.git", accessed over SSH. Now authentication happens in the background, using our key.
If you want to log in the traditional way instead (giving a username and password for remote operations), use the HTTPS address:
git remote add origin https://github.com/duch512/git15.git
GitHub no longer accepts your account password here: instead of the password you give a personal access token, created in the account settings (or a credential manager does it for you). That is one more reason to use an SSH key.
As you may notice, the remote server has a name ("origin"). This means you can add many remote repos to one local repository and push and pull to and from whichever you like. In a typical project, though, you will use just one remote repo.
Pushing changes to the server
The repository we have just created and connected as origin is completely empty.
So let's push our changes to the server with git push:
git push

We check the status - we are on the master branch, which we want to push. We run git push, but Git tells us our branch is not linked to any branch in the remote repository. So the name of your local branch does not have to match the remote one, though keeping identical names is good practice. So we use the suggested solution: we add the -u flag (set upstream) to the command, then give the name of the remote server (origin) and the name of the remote branch (if it does not exist, it will be created). After running it, we see information about the upload, including the fact that a new remote branch was created and that our local branch now tracks it.
Let's push our second branch too:

Since the branch pushed earlier had the same commits, only metadata was sent, hence all the zeros in the statistics. Besides the usual messages, the "remote" block also has extra info from GitHub, suggesting we create a pull request in the browser. It is a tool that helps a team manage a repository where not everyone may change the production master branch (understandably, for safety). A pull request (also known as a "merge request") is simply a request to merge the changes from your branch into another, more important or more stable branch.
Let's make a few more changes on develop and push them to the server. Remember our stash? There are changes waiting there that will come in very handy now:

As always, Git told us what we can do next, e.g. that 1 commit is waiting to be pushed to the server. Now there are even more changes on the server 😉
Cloning a remote repository
If you join an existing project or want to download a repo from the Internet, you do not need to initialise a local repository first. All you need is the repo's address (the https or ssh version) and the git clone command.
git clone [email protected]:duch512/git15.git
Let's create a new folder and clone the repo we pushed earlier (the repo in the example is public, so you can download it to your computer - you will find all the files from this guide there).

We went one folder up and created a new one, bwrite-clone, where we ran git clone <address>. Git downloaded the repository from the address and created a git15 folder (that is the repo's name) for it. Inside, we are on the master branch, and it is up to date with origin/master - so we track the remote repo automatically. The list of local branches shows only master. So we list the branches with the -a (all) switch to see the remote branches as well. Trying git push tells us everything is up to date.
Creating a new folder just to have another folder inside it - not very convenient, right? Let Git do it for us! Let's go back to our "main" folder and give the name of the folder for the repo after the command we used before:
git clone [email protected]:duch512/git15.git tutorial-git

Fetching changes from the server
It is hard to fetch anything "new" from a server that only we push to, so while writing the section on pushing, I made one more clone of the repo, which is now "behind" the server. Let's switch to the develop branch and run git pull, which fetches the changes from the server (you can do that with git fetch, but why bother? 😉 ) and runs git merge, combining the remote content with your local one. 2 in 1 😉
git pull

We switched to the develop branch - it exists in the remote repo, so we could switch to it and it was immediately set to track its remote counterpart. Then git pull fetched and merged the changes. git log shows we are on develop, up to date with origin, and the commit below is up to date with both masters.
To start working with a remote repository you will need:
git remote add <origin> <address>- connects a remote repo to your local repogit clone <address>- copies an existing repo
When working with a remote repo you will most often use:
git checkout <branch_name>- switches to a remote branch (if it exists) and tracks itgit pull- fetches and merges changes (runsgit fetchandgit mergefrom the remote branch)git push- pushes changes to the remote branchgit push -u <origin> <branch_name>- creates a remote branch, pushes the changes and sets up tracking of the remote branch
A final word
Git is great, isn't it? 😉 I hope it got you hooked enough that you do not hold the title "Git in 15 minutes" against me. Reading this, let alone working through it, takes a bit more than the promised quarter of an hour, but I hope such a solid portion of knowledge turns out useful. Of course, the paragraphs above are the bare minimum, and it is worth knowing more. So I encourage you to use Git for your side projects and in your daily work, even if your employer or your colleagues do not see the need for a version control system.
Bye!