Intermediate Git Commands: Reviewing History and Pushing to Origin

Intermediate Git Commands: Reviewing History and Pushing to Origin

In the last post, we covered the Git Basics – how to initialize, stage, and commit. In this post, I’ll be showing you how to check the logs, compare diffs, and push to remote repositories. Here we go! (And if you have questions, please contact us!)

Related Posts

Viewing the Commit Logs

Last time, we added only one commit to our local repository: the initial project and app files. Next, we’ll stage and commit a README.md file, then check the log and compare the two commits. Create the README.md file. This is for GitHub’s sake, and supports markdown formatting. This is where we put useful information like what the different functions of our app are, what the high level project roadmap looks like, and what problem we’re trying to solve with the app.

README_add_terminalREADME_add Go ahead and commit the README.md file to the local repository.

Verify the new commit in the log.

This outputs some really useful information. Note that the commits are displayed with the most recent commit first.

  • commit – this is a unique hash identifier for the commit
  • Author – user who pushed the commit
  • Date – date the commit was pushed
  • The message included in the commit

git_log

  NOTE: If you would like to view the log in a reverse order you can:

And to output the log to a text file:

Analyzing the Aftermath: Viewing the Differences Between Two Commits

Now we can compare our commits. To see the commit history, copy the two hashes into the following command. Highlight the second hash, right-click and choose copy, then right-click and paste (or CTRL+Shift+v) onto the new line entry. Repeat this for the first hash and run the command. You should see the addition of the README.md file!

git_diff

Everything’s Better Together: Setting Up a Remote Repository

What is a remote repository? A remote repository is a repository that’s hosted somewhere other than your local machine. Some good examples are GitHub or GitLab, both free. Remotes allow you to store your code in the cloud and retrieve it using any machine that has git installed. You can then make changes and push those changes to the cloud for later retrieval. Services like Heroku take this a step further, and actually compile your repository code onto a web server so that you can view your application on the web!

Setting up a Remote

First, I created a repository on GitHub for the mysite demo project. I didn’t initialize it with any README.md or .gitignore files since I’ve already created them, and I don’t want to have to pull before the initial push. Once I created the repository, GitHub provided me with a the URL I need to add a remote connection to my local repository. Replace the URL below with your own GitHub repository URL, and run it!

Verify that your remote has been added (-v indicates verbose, causing the console to print out the URL).

git_remote_add

Pushing to a Remote

Now, let’s push! Note that we don’t have to commit anything new. GitHub will upload the repository as-is, including the full commit history. When we added our repository, we used the word ‘origin’. This is an arbitrary name that we chose for our remote connection. We use it again here, and push to the master branch.

git_push_origin

The push was successful! Below, we can see the files we previously added to our local repository, with the latest commit for each file indicated in the column on the right. The README.md also loads nicely into a markdown-enabled viewer below the list of files, so anyone visiting the repository can quickly see what your project is about!

github_screen

That’s it! We have all of the tools we need to configure our app for deployment to Heroku! We’ll get to that a couple of posts from now.

One last thing: branching and merging

Branching a Repository

This will effectively copy your master branch into a branch named newbranch. Then when it’s time to push to the remote, we simply use newbranch in place of master.

Merging two Branches

When it’s time to merge the two branches back together, we perform the following steps.

More on branching and merging here. Want to be notified of new posts? Register for our newsletter!

(Posts) Sign Up for my Newsletter!
Sending

Join the Discussion

%d bloggers like this: