Intro

GIT is a version control system developed by Linus Torvalds in 2005 and maintained by Junio Hamano ever since. It is used to keep track of code changes and to let developers collaborate on code together through the use of repositories (repos) and branches.

After following along with this technical documentation page you will know how to:

Installing

You can download GIT at https://git-scm.com After installing, you can confirm it's installed by typing git --version in your CLI. It should show git version x.y

You'll want to configure GIT with your username and email. You can do that by typing git config --global user.name "FirstName LastName" and git config --global user.email "UserName@Email.com"

Creating a Local Repo

Create a directory and/or navigate to the directory you want to use GIT with in your CLI. Type git init in your CLI. You now have what's called a local repo (repository). Git created a hidden folder that keeps track of code changes.

Adding Files to Staging

If you type git status in your CLI, it should show On branch master No commits yet Add at least one file to the directory and type git status again. This time, it should show On branch master No commits yet Untracked Files: and you should see the file(s) you added to the directory. Now GIT knows about the files, but they haven't been added to your repo yet.

Type git add and the name of the file you want to add. You can also type git add . to add all the files. Now when you type git status it should show On branch master No commits yet Changes to be committed: and show the file(s) you added. The file(s) have been added to what's called a staging environment and are ready to be added to your repo!

Adding Files to Repo

Now it's time to move the file(s) in the staging environment to the repo. This is called adding a commit. Think of a commit as a "Save Point". To commit type git commit -m "Added files to repo" The m in the line above stands for message. You should always add a message to your commit describing the changes you made. If you type git log in your CLI, you can see a list of your commits (you'll only have one for now).

Working on Branches

A branch is a new version of the main repo where you can work on a new feature without making changes to the main branch. You can have one or multiple branches. When you finish the new feature, you can merge the feature branch with the main branch.

To create a new branch, type git branch new-feature in your CLI. You can call your new branch whatever you want. To confirm it was created, type git branch in your CLI again. It should show new-feature * master The * by master means that that is the current branch. To change branches type git checkout new-feature in your CLI, and it should show Switched to branch 'new-feature' Now you can add files to this branch just like you did to the master branch before. After you've committed your changes, your new feature-branch will be different from your master branch, and you can merge the two branches.

Merging Branches

To merge the new-feature and master branches, first switch back to the master branch by typing git checkout master in your CLI. It should show Switched to branch 'master' Now type git merge new-feature It should show Updating followed by the changes that were made. After you've merged the two branches, you can safely delete the new-feature branch by typing git branch -d new-feature in your CLI. It should show Deleted branch new-feature

Merging Conflicts

Something you may run into from time to time is merging conflicts. Sometimes you may have multiple branches going at the same time. After you've merged one with the master branch, when you go to merge another one with the master branch, your CLI will say Auto-merging [...] CONFLICT (content): followed by the name of the file(s) where the merge conflict was found. You'll have to open the file(s), see the differences between the two versions, and edit the file(s) the way you want before being able to commit the file(s) to your repo and complete the merge.