Git is a program which can be used through terminal to manage repositories for storing files and saving different versions of a project. When used in conjunction with GitHub it gives you the ability to save your work at any point in time and note why a save was made. This can help you keep track of your personal project or work in collaboration with a team.
Git can be a very important tool because it helps us save versions of our work and track our progress. This means that if something isn't working or you change your mind on the direction of the project you always have a repository that allows you to return to a previous version of the file(s) without haveing to edit the master copy or completely start over.
Git has a number of commands that are important to know. Here are some of the basic commands we can use with Git:
git init
- Initializes Git directorygit status
- Shows the current status of Gitgit add
- Stages file(s)git commit
-m 'Version name' - Creates a commit of a filegit diff
- Shows changes made to the file(s)
The basic workflow of Git can be a bit confusing at first but it is actually relatively straightforward once you get the hang of it.
- Once you are in the correct directory you will always need to start by initializing Git. (
git init
command) - Make changes to the file.
- Add the file you wish to work on to the staging area using
git add file_name.txt
. - Make a commit using the
git commit -m 'Version name'
command.
Below is an example of what Git might look like in action from my terminal:
cameronmacrae~/git_homework[master]$ echo "random stuff" >> quotes.txt
cameronmacrae~/git_homework[master !]$ git diff
diff --git a/quotes.txt b/quotes.txt
index 3b0b06f..c4047f6 100644
--- a/quotes.txt
+++ b/quotes.txt
@@ -1,2 +1,3 @@
Generic quote
Randome new text
+random stuff
cameronmacrae~/git_homework[master !]$ git commit -m "New commit"
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
modified: quotes.txt
no changes added to commit
cameronmacrae~/git_homework[master !]$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: quotes.txt
no changes added to commit (use "git add" and/or "git commit -a")
cameronmacrae~/git_homework[master !]$ git add quotes.txt
cameronmacrae~/git_homework[master !]$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: quotes.txt
I found this diagram very helpful in understanding how Git operates and the different ways it can be used.