Git Basics

Using Git to control changes.

Updated March 17, 2017

Using Git regularly can be really hard to get used to. You're trying to be productive, you want to get your work done, not mess around with some version control system. Its easy to forget to commit changes and track what you're doing. I honestly believe now , after much stubborness, that using git regularly in conjunction with good testing practices has increased my productivity greatly.

This Article assumes you have a git repository already set up and are now in a working project state.

First I'll check my repository to see if there are any files either staged for commit with the command:

git status

or not staged but changed since the last commit. If there are no changes, I should see this:

Quill Cloud

If I have changes that I do need to commit, I'll see this:

Quill Cloud

This is clearly showing me that I made a change to a file : 'dashboard.blade.php'

Now, If I want to know what those changes are, I'll run the command:

git diff <file path goes here>

Quill Cloud

As you can see, I made a small change by removing 'ed' from the word 'Edited' since my last commit.

I'm thinking this change was a dumb thing I did late at night and it made more sense with the word "Edited" as it was in the last commit. I'll revert back to the previous 'version' by running the following command:

git checkout .

Warning: This will change your data permanently!

Quill Cloud

What I just did was tell git that I want to revert back all unstaged changes in my repository. the "." is a way of saying 'do this to everything'. This is a great stackoverflow post about the different way handle changes, staged, unstaged, or both.

So now I have my clean directory. I'm going to do the unit of work that I talked about originally and then return to the git command line.

... * doing work *...

... * more work *...

... * forehead slap * ...

Okay, now my unit of work is done and tested. lets check the status of my project:

Quill Cloud

I made changes to two files. Now its time to commit them. I could run

git add .

to first stage the changes for commit. This will stage all the files:

Quill Cloud

If you're on a windows machine you might see the message: "warning: LF will be replaced by CRLF in ...". Don't worry too much about that. If you want to know what its all about: check out this stack overflow article.

You can see that the unstaged changes are now staged and highlighted in green. Now if you wanted to unstage one of the files you could use :

git reset <file path>

Quill Cloud

There are reasons to do this that we don't need to get into in this exercise. Lets add that one particular file back into the staging area with

git add public/js/p_data_mgmt.js

Quill Cloud

Okay now we are back to having both files staged for commit. However they are not commited yet. To commit them We'll run the command:

git commit -m "Added warning messages to quill editor toolbar"

Quill Cloud

Now our changes are commited. As you can see when I check the status, there is nothing to commit, but I am "ahead of origin/master" by 1 commit. If I had other units of work to do, I could repeat the above steps and then I would be ahead of the master by 2 commits.

Now I need to push my changes out the master branch of the repository. This is dreadfully simple with

git push

Quill Cloud

Now your changes are successfully pushed out to the master repository. I'll be following up this article with an article on how to rollback a repository and handle merging issues. Thanks!

Related Posts