Loading video player…

Git Version Control Basics

In this lesson, you’ll run through an example to see git in action. You’ll learn a few git commands and the basics such as:

  • How to get git
  • How to configure git
  • How to initialise a new repository
  • Check status of a repository using git status
  • Add new files to a repository using git add
  • Committing changes

Along the way, you’ll be working with the Zen of Python, so you can remind yourself of its guidance.

00:01 Let’s start digging in to the basics of Git. So, Git is conceptually different than any other version control system out there. Git is a VCS and it’s a mini file system. It stores lots of metadata about each file.

00:20 Other VCSs only track the changes or deltas to a tracked file or set of files. Git, when you save—this is also known as a committakes a full snapshot of all tracked files regardless of if they’ve changed.

00:37 This might seem a little funky because only one file is changing but Git is taking a snapshot of all of them, but this is a big part of the resilience of Git.

00:46 I took a stab at simplifying the basic workflow of Git. If you think of your Git repository, which will essentially be a folder or file directory, each file or directory within your Git repository can be modified.

01:03 Git knows when those files have been modified. Once you modify a file, files, or directories, you’re going to stage them for changes. And this is up to you and Git to choose which files you select for staging.

01:18 Staging just means it’s waiting to be committed to the next version, if you will. And finally, when you’re ready, you commit your changes to the repository.

01:27 So, let’s set up an example so you can see Git in action. We’re going to assume in this tutorial that you’re using Git for the first time, so you’ll need to install it. If you’re using Linux, it may be already installed or you can install it with your package manager of choice.

01:46 If you’re on Mac or Windows, you can browse to git-scm.com and it will actually detect your operating system and direct you to the latest source. While you’re there, you should check out Pro Git. It’s a free book and will teach you everything you need to know and more about advanced usage of Git.

02:08 So, once you have Git installed, let’s hop into the demo and get into the good stuff here. If you’re on Linux or Mac, go ahead and open your terminal. If you’re on Windows, like me, when you install Git you get a program called Git Bash, and Git Bash is a Linux shell emulator.

02:27 This is Git Bash. So, on a fresh install of Git, the first thing you should do is set up your identity. And for this, we use the git config command.

02:40 You’re going to give it a switch --global, user.name, your name in quotes.

02:54 We’re going to give it another config command. Again, --global. You’re going to give it your email.

03:04 That doesn’t have to be in quotes. And now you’re ready to go. That’ll come into play later. The first thing we need to do is make a directory that will become our Git repo.

03:19 Let’s change the directory and get into it.

03:23 And I want you to run this command now that we’re in the repo, git init.

03:30 So you can see now, you’ve got an empty repository and it actually puts a hidden file in there called .git. It also changed our cursor line in the shell to (master), and we’ll talk about that more in coming videos.

03:45 So we have our Git repository now and a command you will probably use a lot and find helpful is git status. Go ahead and run that. And you can see it’s giving you a few pieces of info.

03:59 It tells you which branch you’re on, the last commit status, and that you have nothing to commit. We’ll talk about all these things in coming videos. So, this is a great start.

04:09 Let’s add our first file to the repository. So what I want you to do is open up a Python console. I’m going to show you an Easter egg here. I want you to type import this, hit Enter, and this brings you up The Zen of Python, a little essay by Tim Peters.

04:26 You should check this out. We’re going to use this as the text for the file that’s going to go into our repository. So let’s copy the first seven lines out of The Zen of Python—one, two, three, four, five, six, and seven.

04:45 I’m going to copy it and you can paste it into whatever text editor you’d like, and then save it into your repository. I’m just going to use VIM, feel free to copy me.

05:02 And then just paste it right in there, Shift, colon (:), and wq to exit. Enter.

05:12 So now, if I list this directory, we’ve got a file in there—our zen_of_python file. Let’s run git status again.

05:23 We see the file now in git status, but it says that it’s untracked. So we’re going to use another command called git add, and it’s going to be followed by the filename, which is zen_of_python.

05:37 This will add the file to Git’s version control tracking. Hit Enter. That’s fine, it’s going to replace the line endings. And then run git status again. Cool!

05:50 So we have a file that’s been modified and it’s now staged and ready to be committed. When we commit, it tells Git to take a snapshot of the whole repo. We do this easily enough with the git commit command, and then we’re going to do -m. We’re going to give it a commit message.

06:19 So, what’s up with this formatting on this commit message? If you read through the documentation, it says to use the imperative form to format your commits. Imperative form basically means to give a command.

06:32 So you might say “Fight the zombie,” “Run away from the horde,” “Find a shelter.” That’s enough with the grammar lesson. Let’s run this commit now and run git status one more time.

06:45 And we’re showing a clean working tree, so that means you just committed your first file in Git. Good job!

Avatar image for Violet

Violet on July 2, 2019

Hi Paul,

I tried the git config command as you instructed and got this message: fatal: not in a git directory

What should I do then?

Avatar image for Paul Mealus

Paul Mealus on July 3, 2019

Hey Violet. Can you verify you had the global switch on? The config command should look like this…

git config --global user.name "Violet"

Avatar image for Kevin Dienst

Kevin Dienst on Sept. 4, 2019

Per chris.beams.io/posts/git-commit/ it seems that you should avoid adding periods in your commit messages.

Avatar image for bgersten

bgersten on Jan. 23, 2024

Hi,

The two clippings below of the output of the commands $ git log and $ git status show the position of HEAD on the master branch. I never saw (master) before the $ and after the zen_repo in the prompt at any time during the course. I have done some customization of the prompt previously. My current prompt customization in .zshrc file is PROMPT="%F{cyan}%n%f@%m %W %1~ $" on macOS 12. Is there any adjustment needed to show (master) in the prompt when HEAD is on the branch master?

(venv) briangersten@Brians-Mac-Studio 01/23/24 zen_repo $git log 

commit 9f29fa073eb046c2dc90896c0dafb56175a3c731 (HEAD -> master)
(venv) briangersten@Brians-Mac-Studio 01/23/24 zen_repo $git status 

On branch master

nothing to commit, working tree clean
Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Jan. 24, 2024

@bgersten If you use Zsh as your shell, then a quick way to reveal the current Git branch in your prompt is by installing Oh My Zsh and enabling the git plugin. Alternatively, you can try implementing a custom shell function in your .zshrc configuration file and setting the prompt accordingly:

# .zshrc

# ...

function git_branch_prompt {
  local git_branch="$(git rev-parse --abbrev-ref HEAD 2> /dev/null)"
  if [[ -n $git_branch && $git_branch != "HEAD" ]]; then
    # echo "($git_branch)"
    echo -e "\033[0;32m($git_branch)\033[0m"
  fi
}

export PROMPT='%n@%m:%~ $(git_branch_prompt)%# '

When you reopen your terminal or manually source the shell configuration, it’ll show you the branch name:

user@computer:~ % cd ~/Git/my-project
user@computer:~/Git/my-project (main)% 

Become a Member to join the conversation.