-
Notifications
You must be signed in to change notification settings - Fork 1
Beginner Project: Basic Git
This project will walk you through getting to know Git and Github by walking through...
- Creating a project on your computer
- Stage and committing files
- Branching your project and merging branches
- Revert changes that you made to your project
- Storing changes on Github
Note: When the guide refers to executing a command, it means to do so in the command line. On a Mac, you'll want to use Terminal or iTerm 2. On Windows, you should be using the Git Bash program that's installed if you follow these instructions.
Some terms to be aware of while reading through these instructions
Directory
- A folder on your computer
Home folder
- The folder on your computer that most of your personal files are kept in
- On OS X, this folder can be found at
/Users/{username}
- On Windows, this folder can be found at
C:\Users\{username}
The first thing that we'll want to do is create a new project to be tracked by Git. To start off, open your command line program and enter the following commands:
mkdir projects
cd projects
mkdir git-beginner-project
cd git-beginner-project
What this does is create a new folder called projects
inside your home folder, step inside of it using the cd
command, then do the same with the git-beginner-project
folder.
Now that we have a place to create your project, we want Git to set itself up to track the files inside this folder. To do that, we run the following command:
git init .
This tells Git to init
, or initialize, a new project inside the current directory (which is refered to by a .
).
If you see a message like this:
Initialized empty Git repository in c:/Users/alex/projects/.git/
Then you're all set!
Next, we'll add some files to the project and see how Git can keep track of them. In the command line, type the following command
touch hello.txt
This will go and create a new file called hello.txt
. Now, run the following command to see what changes Git is currently detecting:
git status
Let's take a look at the output and break it all down:
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing add to commit but untracked files (use "git add" to track)
This means that git sees no changes to files that it is already keeping track of, but does see a new file that it doesn't recognize. In Git, a "tracked" file is one that Git is currently watching for changes, and an "untracked" on is a file that it knows nothing about yet.
We want to get Git to start keeping track of our file. Do to that, run the following two commands: the first will add the file to those that Git keeps track of, and the second will output the status again
git add hello.txt
git status
Which should result in something like this:
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.txt
This is telling us that Git is now tracking one new file, called hello.txt
. Sounds about right to me!
You'll notice that Git mentioned a few new concepts, namely commit
and staged
(or, as you can see in the output above, unstaged
.
In Git, a staged file is one that has changes that you're going to be adding to your next commit (more on that in one second). An unstaged file is one that Git has noticed a change in, but that it is currently ignoring.
A commit is the basic building block of all of Git. By creating a new commit, you're bundling together a set of changes to your project: new files, additions, deletions, everything. By keep track of every change to every file in your project, Git can rewind your project to any point that you made a commit, almost like save points in a video game.
Now, we'll create the first commit for our project. Recall that our project has hello.txt
staged, so that's what will be including within our first commit. Creating a commit can be done a few ways, but the quickest way is like this:
git commit -m "My first commit message"
Where My first commit message
is a brief description of the changes you made within this commit. This message should be short and to the point -- there are ways to add more details about your changes, but we'll look at that at another time.
Now that we can make commits, it's important to know how to revert them. We'll quickly make a new commit using the following commands
touch revertme.txt
git add revertme.txt
git commit -m "Added commit to revert"
Now, you can check out the list of all your commits using the git log
command. There are many options that you can pass to it to print the list out in different ways, but we'll use git low --oneline
to keep the list simple and neat
$ git log --online
c9a3daa Added commit to revert
2d7390b My first commit message
So, by reverting the most recent commit, we'll remove the file that we just created.
- Beginner Level