Introduction
In this lesson, we’ll cover common Git commands used to manage your projects and to upload your work onto GitHub. We refer to these commands as the basic Git workflow. When you’re using Git, these are the commands that you’ll use 70-80% of the time, so if you can get these down, you’ll be more than halfway done mastering Git!
Learning Objectives
By the end of this lesson, you should be able to do the following:
- Describe how to copy an existing repository from Github onto your local machine.
- Explain the two-stage system that Git uses to save files.
- Describe how to upload your work to GitHub using Git.
- Describe how to check the status of your files and how to view your commit history.
Assignment
- Watch the next video by
Corey Schafer
for a great overview of some basic Git commands.
-
Read 5 Git Commands You Should Know, with Code Examples and spend some time practicing on the commands described in the post.
-
Follow this excellent guide on how to Learn Git and Version Control in an Hour by Amarachi Emmanuela Azubuike . Student Approved Resource
Cheatsheet
This is a reference list of the most commonly used Git commands. (You might consider bookmarking this handy page.) Try to familiarize yourself with the commands so that you can eventually remember them all:
- Commands related to a remote repository:
git clone git@github.com:USER-NAME/REPOSITORY-NAME.git
orgit clone https://github.com/user-name/repository-name.git
git push origin master
- Commands related to workflow:
git add .
git commit -m "A message describing what you have done to make this snapshot different"
- Commands related to checking status or log history
git status
git log
The basic Git syntax is program | action | destination
.
For example,
git add .
is read asgit | add | .
, where the period represents everything in the current directory;git commit -m "message"
is read asgit | commit -m | "message"
; andgit status
is read asgit | status | (no destination)
.
Live Session
Watch the following live session that was recorded on 23/09/2020 to understand more about the basic concepts of Git and walk yourselves through some basic examples.
Part I | Duration: 1h 33m
Part II | Duration: 1h 30m
Part III | Duration: 1h 24m
Conclusion
You may not feel completely comfortable with Git at this point, which is normal. It’s a skill that you will get more comfortable with as you use it. Therefore, we have a project coming right after this lesson where we’ll walk you through the entire Git workflow, which is the exact same process you would use in a real project.
The main thing to take away from this lesson is the basic workflow. The commands you’ve learned here are the ones you will be using the most often with Git.
Don’t worry if you don’t know all the commands yet or if they aren’t quite sticking in your memory yet. They will soon be seared into your brain as you use them over and over in future projects.
Additional Resources
-
Learn Enough Git to Be Dangerous is an introductory guide on Git by Michael Hartl.
-
An easy-to-read, pragmatic guide to using Git is available for free on Kindle.
-
The Git Cheat Sheet from GitHub provides quick instructions for using common commands (you can find a webpage version here).
-
Atlassian has a very thorough and well laid out Git tutorial.
-
For a more in-depth understanding of Git, read the free ProGit eBook.
-
Dangit, Git!?!: Fix common mistakes in git.
-
what the git: Enter a git command and have it explained to you
-
Oh, Shit Git!: Yes, you’ve guessed it.
-
This next video by
Jeff Delaney
has a fast-paced overview of Git.
Duration: 12 minutes
Knowledge Check
This section contains questions for you to check your understanding of this lesson. If you’re having trouble answering the questions below on your own, clicking the small arrow to the left of the question will reveal the answers.
What is the Git command used to get a full copy of an existing Git repository from Github?
- Use
git clone git@github.com:<your-github-username>/<your-respository-name>
to clone a GitHub repository onto your local machine.
What is the Git command used to check the status of your files?
- Use
git status
to see any changes made since your last commit.
What is the Git command used to track files with Git?
- Use
git add
to track files.
What is the Git command used to commit files?
- Use
git commit
to commit tracked files.
What is the Git command used to view your commit history?
- Use
git log
to view your commit history.
What is the Git command used to upload projects onto GitHub?
- Use
git push
to send your commit to GitHub.
Explain the two-stage system that Git uses to save files.
- A save in Git is divided into two terminal commands:
add
andcommit
. The combination of these two commands gives you control of exactly what you want to be remembered in your snapshot. - Staging: Think of
add
as adjusting the number of people or elements to be included in a photo. With Git, you can select the changes you want to save withgit add
. Imagine a project that contains multiple files where changes have been made to several files. You want to save some of the changes you have made and leave some other changes to continue working on them. - Committing: Think of
commit
as actually taking a photo, resulting in a snapshot. For example, to commit a file named README.md, typegit commit -m "Add README.md"
. The-m
flag stands for “message” and must always be followed by a commit message inside quotation marks. In this example, the commit message was"Add README.md"
.
Explain what origin
is in git push origin master
.
- In Git,
origin
is a placeholder name for the URL of the remote repository. Git sets up the origin by default when it clones a remote repository. You can useorigin
to access the remote repository without having to enter a full URL every time. This also means that you can have multiple remotes for a repository by giving each a unique name.
Explain what master
is in git push origin master
.
- In Git,
master
is the branch of the remote repository you want to push your changes to. We will get more into branches in a later lesson, but the main thing to remember is thatmaster
is the official branch in your projects where production-ready code lives.
Material based on Erik Trautman | The Odin Project
LOOKING FOR HELP?
When looking for help, try doing so in the following order:
- Did you try everything you could?
- Did you read the documentation?
- Did you Google for it?
- Did you post your question on Slack/Forum?
- Did you ask your fellow students for help?
- Did you ask your Mentors for help?
- Did you leave a comment on the comments section of this page?
- Did you ask your Instructor for help?
- Did you arrange and appointment with your instructor using Calendly? Visit this URL and set up an appointment: https://calendly.com/kostasx
- Is it urgent? Did you try reaching him on Slack
UPDATED: 10.03.2021
CONTRIBUTORS:
- Maria Barkouzou
- Alkis Green (for suggesting the "Learn Git and Version Control in an Hour" post)
- Vincent Le for pointing out broken links and suggesting git resources