How to setup a basic project using Git

Find out more

1Fabio

How to setup a basic project using Git

Git workflow, using stash and branches

Git is an amazing tool for programmers, and if you are not familiar with it, you should start using it now.

Accordingly to the official website, Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git is easy to learn and has a tiny footprint with lightning fast performance.

Wikipedia gives a further description Git: Git is a distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

To understand this guide if you are not familiar with git, you should first install it in your system, if can check the bitbucket documentation for it’s installation guide. The official download page is available here for Linux, Mac and Windows.

Let’s see how we can start using it in a simple project.

Note: don’t copy the dollar sign, it’s not part of the commands but a simple placeholder to represent the terminal. All code without the $ sign is the output of each command.

1. Create a basic project and add some files

I use Linux Ubuntu during development, the commands below are Linux command line commands.
First lets create a folder called basic-project in our projects folder. Then we add two sample files.


$ mkdir basic-project
$ cd basic-project
$ echo "this is a sample file" > test.txt
$ echo "this is another sample file" > test2.txt

The above commands needs to be typed in a terminal, ‘mkdir’ creates a directory in a Linux system, we  use ‘cd’ to enter in a directory, then using ‘echo’ we create a file and add some content between the quotes ” ” then with the ‘>’ sign we instruct the system to add the contents inside a file named ‘test.txt’ .

2. Initialise a git repository using `git init`
Initialise an empty git repository inside the basic-project directory, then using git status we can see what files are waiting to be added and committed.


$ git init
$ git status
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be committed)
test.txt
test2.txt
nothing added to commit but untracked files present (use "git add" to track)

As you can guess ‘git’ is a git command, you can see a list of commands available using ‘git –help’.

3. Add files to the repository using ` git add `
Now we are ready to add the two files to our git repository then we will use another command, git commit to store the changes.


$ git add .
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: test.txt
new file: test2.txt

All files are now added to the repository, the output of git status will be different now, as you can see if you are following along the files are now will be colored in green instead of red.

4. Do the first commit using `git commit`
Git commit lets us add a short message without body using the -m option or we can include more details without this option

$ git commit -m "Initial commit"
$ git status
On branch master
nothing to commit, working tree clean

Try to keep the commit descriptive and explain what you are doing using only the present tense.
Running git status now will output a clean working tree.

Make changes to the code, and then use git branch and git stash

When we work on a project the best practice is to create branches for new features, bug fixes etc then merge this changes with the master branch.

If you have a project and started working inside the master branch and did not create a features/ branch then it’s a good practice to create one.
We can do so using git stash to save the changes aside, then we create a feature branch and restore the files inside this feature branch using git stash apply command.

Following our previous basic-project example we now:

5. Create a new file inside the master branch

Typing git branch we get a list of all branches. The current branch has an * and is colored in green.


$ git branch
* master
$ echo "adding a features file for a features branch" > features.txt
On branch master
Untracked files:
(use "git add ..." to include in what will be committed)
features.txt
nothing added to commit but untracked files present (use "git add" to track)

6. Adding untracked files to the repository before using git stash

Now we need to add all files changed or added to the repository but instead of running git commit we will use git stash, it’s like save them on aside.


$ git add .
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file: features.txt

7. Stash changes using git stash

The stash command will clear the working directory and store all stashed files separately. We can then use git stash list to see all stashed changes.
Now we can use git stash to restore the working tree as it was before, create a features branch and restore the files inside it using git stash apply.


$ git stash
Saved working directory and index state WIP on master: 8d7844b initial commit
$ git status #git status shows that the working tree is now clear
On branch master
nothing to commit, working tree clean
$ git sash list # show all stashed changes.
stash@{0}: WIP on master: 8d7844b initial commit

After that we create a new branch we will then use the git stash to apply the stash@{n} and restore the files inside our features branch working tree.

8. Create a new features branch
using git branch we can create a new branch that we can name as we like, a good practice for new features is to create brances like so: features/my-feature. We create a branch then display a list of all branches using git branch.


$ git branch features/my-cool-feature
$git branch
features/my-cool-feature
* master

The output shows that we have two branches, the one we are currently in is the *master.

9. Checking out the features/my-cool-feature branch

Using git checkout we can move from a branch to another and work there. Each time the working tree will change accordingly.


$ git checkout features/my-cool-feature
$ git status # will display a clean working tree and our current branch
On branch features/my-cool-feature
nothing to commit, working tree clean

10. Using git stash to restore stashed files

Now we are ready to restore the files we were working on but instead of having them inside our master branch we will keep them inside a dedicated features/my-cool-feature branch and when ready we can merge the two branches.
Using git stash list we can retrive all stashed changes than using git stash apply we can restore them to the current features branch


$ git stash list # diplay a list of all stashed changes
stash@{0}: WIP on master: 8d7844b initial commit
$ git stash apply stash@{0}
On branch features/my-cool-feature
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file: features.txt

The git stash apply command will restore the files here in the features branch so we can commit them separately from the master branch.
This will leave the master branch clean so we can make changes to it incase we need to adjust something but we are not ready yet to commit the new features we are working on.

11. Commit changes to the features branch
Now we can commit changes inside the master branch

$ git commit -m "working on a cool feature"
$ git status # will display a clean working tree
On branch features/my-cool-feature
nothing to commit, working tree clean

12. Using git log to see all commits
we can use git log to see the different commits on each brach.
Using git branch we can see what branch we are currently working on then with the log we can see all commits to the current branch.


$ git branch
* features/my-cool-feature
master
$ git log
commit efd06dd2cc0533face55e548e7980bb129a6bec2 (HEAD -> features/my-cool-feature)
Author: Fabio Pacifici <mail@fabiopacifici.com>
Date: Sun May 5 15:13:22 2019 +0100
working of a cool feature
commit 8d7844b55b4230cd7ab2254f9acb19737bb767a3 (master)
Author: Fabio Pacifici <mail@fabiopacifici.com>
Date: Sun May 5 14:16:58 2019 +0100
initial commit

The log in the current features/ branch will show two commits while only one in the master branch


$ git checkout master
$ git log
commit 8d7844b55b4230cd7ab2254f9acb19737bb767a3 (HEAD -> master)
Author: Fabio Pacifici <mail@fabiopacifici.com>
Date: Sun May 5 14:16:58 2019 +0100
initial commit

Remember : If you are working inside the master branch on a new feature it’s not a good practice. Instead you want to create a separate branch and work there. Useing git stash, if you have already started working inside the master branch, will save the changes and restore the master to it’s previous state (commit). Then you want to create a branch using git branch and use git stash apply to restore the files inside the feature branch.
the next step is to merge changes using git merge

Merge Changes using git merge

Now that we are ready we can merge all the changes with the master branch by simply using `git merge [name of the branch]`

For examle:

to merge the changes of the features/my-cool-feature inside the master branch we can simply do:


git checkout master
git merge features/my-cool-feature

The command above will merge all changes, note that after the merge the files inside the current working directory will include all files inside the features/my-cool-features branch and modify any existing file accordingly.


The Author

Written by:

I am a senior entrepreneur, full-stack php developer, Laravel, WordPress, Magento experience with knowledge responsive web design, frameworks and SEO techniques. Several years of experience as CAD Designer and website-eCommerce administrator with digital marketing experience. Analytical mindset with problem solving skills. Productive, self motivated and well organised. I’m a bilingual Italian-English with a strong personality, very flexible, and enthusiastic to work in other Cities or Countries. Loving knowledge and new challenges if there is anything that I don’t know how to do, I’m always likely to learn it. I put passion and care in everything I do. I’ve a strong sense of responsibility, ethics and duty. I’m a friendly, enthusiastic open minded person, always happy to work in a team but also capable of working on my own.


Related Posts

GitLab installation Cover

How To Install a GitLab Server in 20 Minutes

Mobile application development with flutter

Build Mobile Apps With Flutter

Multi-language-laravel

Laravel 5.7 Multi-language | Complete guide


Need Help?

Just get in touch! I'm always happy to help.


Get professional advice today on a wide range of solutions for your business.

Contact Fabio