Let's set up git, and see how does it work.
Git is a version control manager for your source code. When you are working on some project, you will have multiple branches, lots of versions, some co-workers that will modify the source code, and you will need to handle it somehow.
The easiest way to describe why we need version control is to imagine that you work on a project with your friend at the same time. Both of you were editing the same file, on different computers. How are you going to merge changes later into one file?
Another example is an experimental feature that you are going to add to your project, that you want to ship only to 20% of your customers. Making duplicate folders for each version won't work well. Especially if both branches will be maintained separately, and later you are going to merge them into one project again.
Git helps you to do all of it. Git is a console tool. However, you can find lots of graphic interfaces for it. For example Github Desktop
The terminal is enough in 90% for git - there are just 3-5 commands user needs to know. Graphic Interface can be useful when you want to see changes done to the code; browse commits history, etc.
To install git with Homebrew, just run next commands:
brew update
brew install git
Next, you may want to set up an account on some source code hosting service like Github or Bitbucket. You may need it in the future to upload your code, or to collaborate with your colleagues. Account setup is very well explained on their websites: Github or Bitbucket
Ok, so Git is installed, let's try to learn how to use it now with the terminal. Let's say we have three tasks:
Let's setup this small project:
mkdir ~/Documents/git-lesson
cd ~/Documents/git-lesson/
git init
echo "INIT" > secret_data
ls
command in the terminal.
git status
command:
git add secret_data
git add .
.
stands for all files and folders in your current directory.
git commit -m "Secret file added"
git log
command:
echo "My name is John" >> secret_data
git status
.
If you want more info on what has been changed, try
git diff
git add .
git commit -m "Added name"
git checkout -b "dev"
git branch
git status
output, or view changes in any git app:
git add .
git commit -m "Added job title"
git log
git checkout master
git merge dev
git log
Obviously, these are not all git commands that you will use, but they
are most important ones. Later you will see that merging branches
can result in conflicts (when the same line in the same file
has been changed in both branches). You will learn how to resolve
those conflicts, will figure out how to switch between commits (
very easy actually, just use
git checkout XXX
command, and replace XXX with commit hash,
that you see in git log output), and much more.
You can try to experiment with git a little bit more: try to clone existing repositories from Github, try to make some merge conflict on purpose, etc.
And whenever you feel ready, let's move on to the next chapter.