DEV Community

Aaron McCollum
Aaron McCollum

Posted on • Updated on • Originally published at

Introduction to Git: Part 2

This is part 2 of the introduction to Git. You can check out Part 1 of the Introduction to Git here, which goes over the history of Git and the theoretical way it works. In this post, it will be more brief with a step-by-step process on using Git.

As a quick note: anything inside these symbols (< and >) is meant to convey unique inputs, and the < and > symbols are not to be actually typed in the terminal.

Git’s main syntax is as follows: program | action | destination (git add <FILE NAME> as an example). You will see below this syntax play out in real time. Git is the program, add is the action, and the destination of the action is on the <FILE NAME>.

Getting Started on a project

When you create a new repository in Github, it will provide the SSH key for you to link to your local disk’s Git program. You can copy the link, then use:

git clone <URL Link>

This will clone your program onto your local computer for you to work on. You can then use the cd command to change to the project’s directory and start adding files, and then you can use the code command to open the files up in VS Code.

>touch index.html
>touch style.css
Enter fullscreen mode Exit fullscreen mode

Making Changes and Committing

Once files are added to your project in Git, they are either staged or not staged. Not staged means they will not be committed when you start your next commit push, while staged means they are ready to be committed. If a file is not staged, it will by default show up as red in your console, and files that are staged will show up as green. You can check the status of each file by typing:

git status

If a file is not staged but is ready to be staged, you can type the following command:

git add <FILE NAME> (you can list multiple file names separated by spaces)

Once you are ready to commit your staged files, you can use the following command:

git commit -m "COMMIT MESSAGE" (the -m flag is a message flag for note-taking)

At any point, if you would like to see a record of what you did, you can use the following command:

git log

A Few Other Odds and Ends

When you change a file you are working on that is part of your local project in Git, it will automatically change to the “modified” status when you use the git status command, since it’s being tracked by Git.

git add <FILE> will stage the individual file
git add . will stage everything in the current directory
git add -A will stage everything that’s unstaged in the entire project

Pushing to Github

When you are ready to push your changes and commits to Github, you can use the following command. This one is a little slower since you are connecting with a remote server:

git push origin main

Source of Learning

I have been learning Git on The Odin Project and have really enjoyed it. Their explanations are far better than mine and that’s a great resource if you are like me and starting out.

Also the Git website features a full version of the book “Pro Git” by Scott Chacon and Ben Straub, which I found very useful for a lot of information.

Top comments (0)