Git is used to keep track of, and allow access to, multiple saved versions of a file or project (multiple files) over time. In other words, Git allows you to go back in time to a previously saved version of a file, in the event that you made a mistake or unwanted change in a more recent version.
Before a project is uploaded to a GitHub repo, it begins locally on one person's machine. This is the level where Git itself operates. Git keeps track of the file changes within your own computer based on a "commit." Essentially, you can create a marker in time for a specific version of a file or set of files by creating a commit in Git. This will allow you to go back to that point in time for a specific file should you make a mistake or unwanted change.
Note: Git is different from GitHub. GitHub is used as an online repository, or storage space, for your Git projects. This allows you to have a non-local backup of your commits. More importantly, GitHub allows collaboration between multiple people on a single project. Each collaborator can download or "pull" a copy of the project from GitHub to work on it locally. Once someone has made changes they want to share with the other collaborators, they can then "push" or upload their local commit(s) to the GitHub repository for the others to see. Other people can then add on to those changes, allowing multiple people to work on a single project.
Each of these steps is explained in greater detail below.
- Create or identify the folder containing files where you want to use Git
- Initialize Git within the desired folder
- Add files to Git staging
- Commit files to Git
- Use Git status and/or diff commands
If you already have a folder created, skip the first sub-step of this section
- Using your computer's terminal, create a directory / folder where you'd like to have your files which will be tracked by Git. To do this, use the
mkdircommand. - Navigate into the desired folder using the
cdcommand - Make note of the folder path for future reference using the
pwdcommand - Determine the contents of the folder using the
lscommand
- Once you have navigated into your desired folder using the terminal, type
git init - To check if Git was successfully enabled, type
git status. If successful, you should see the following message:
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
Now we'll add files to Git's staging area. The staging area is where you prepare your files to be tracked by Git.
- Type
lsto list the contents of your current folder - Make a note of the names of files - and their extensions - which you want Git to keep track of. In this example, we will be using a files named "testfile.txt" and "testfile2.txt"
- Type "git add (name)" once for each file, replacing (name) with the actual file name. For example, type
git add testfile.txtandgit add testfile2.txtto add both files to Git's staging area. - Since you will not receive a confirmation message when adding files to the staging area, you can use Git Status. Type
git statusto confirm the files have been added to Git's staging. You should see something similar to the following message:
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: testfile.txt
new file: testfile2.tx
Commit is used when you want to create a "save point" or a marker in time for the current version of your staged files
- Each commit should be accompanied by a commit comment, which gives some information on what was done on that commit.
- Commit comments always begin with a capital letter
- Commit comments should give a general idea of what was done on that commit
- For instance, you should note if you have added or changed a file.
- The argument to create a commit comment is
-mfollowed by the commit message in quotes. - The comment for your first commit should always be "Initial commit."
- Since this is our first commit for this project, type
git commit -m "Initial Commit."You should see something resembling the following message as a confirmation:
2 files changed, 2 insertions(+)
create mode 100644 testfile.txt
create mode 100644 testfile2.txt
- For subsequent commits, you will need to re-add the desired files to staging, then commit them using a different commit message outlining the changes.
As already mentioned abvove, git status can be used to display the current status of your Git management.
Additionally, we can use git diff followed by a filename to show us any differences between files which are currently staged, versus their previously commited versions.
Since I could not find an appropriate / relevant place to add an image above, please accept this picture of my dogs as proof of my ability to embed an image in a markdown document:
