1. Home
  2. Explore
  3. Computer Science
  4. Git Fundamentals

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Git Fundamentals

Speaker 1Speaker 2

Git Fundamentals

16:57
0:00-16:57
  1. Hope

    Welcome to our Git foundations episode. Whether you’re new to Git or have used it without quite understanding what it’s doing, we’ll build the core ideas step by step: what version control is, how Git records project history, and the everyday commands for making and reviewing changes.

  2. Simon

    And the goal isn’t to memorize a list of commands. It’s to understand the workflow well enough that the commands make sense—and that you can recover when something goes wrong.

  3. Hope

    Let’s start with the big question: what is Git? It’s a version control system. A version control system records changes to code over time in a repository, so you can inspect project history, see who changed what and when, and return to an earlier state if needed.

  4. Simon

    Without version control, people might keep entire project copies in different folders, or send the latest code around and merge everyone’s changes by hand. That gets slow and unwieldy, especially when several people are working together.

  5. Hope

    Version control systems are commonly described as centralized or distributed. In a centralized system, everyone connects to a central server to get the latest code and share their changes. Subversion and Microsoft Team Foundation Server are examples.

  6. Simon

    The drawback is a single point of failure: if that server goes offline, collaboration and saving project snapshots can be blocked until it comes back. In a distributed system, each team member has a copy of the project and its history, so they can save snapshots locally and, if the central server is unavailable, synchronize directly with others.

  7. Hope

    Git and Mercurial are distributed systems. Git became especially popular because it’s free, open source, fast, and scalable. Operations such as branching and merging are also much faster in Git than in systems like Subversion or TFS.

  8. Simon

    That popularity makes Git a practical career skill, too. The course material notes that Git is used in more than 90 percent of software projects and is mentioned in nearly every software developer job description. Knowing how to track history and collaborate is valuable well beyond learning commands.

  9. Hope

    There are several ways to use Git. You can type commands in a terminal or command prompt, use basic Git features built into a code editor or IDE such as VS Code, or use a dedicated graphical interface.

  10. Simon

    VS Code has a Source Control panel, and extensions can add more features. GitKraken and SourceTree are examples of dedicated Git GUIs. GitKraken works across platforms and has integrations with related products; SourceTree is free but is available for Windows and Mac.

  11. Hope

    This course focuses mostly on the command line, not because a GUI is wrong, but because GUIs have limits: they generally support the commands people use most, and sometimes you’ll need the command line for a particular task. A command line is also useful on a remote server where you may not be able to install a GUI.

  12. Simon

    On the other hand, some tasks are easier to understand or perform visually. The sensible principle is to use the right tool for the job. You don’t have to be a command-line purist—or avoid the command line entirely.

  13. Hope

    Before working with Git, check whether it’s installed. Open a terminal or command prompt and run `git --version`. If you need Git, download it from git-scm.com and follow the installation instructions for your operating system.

  14. Simon

    On Windows, Git installation includes Git Bash, a command-line window that emulates a Unix- or Linux-like environment. The lesson recommends using Git Bash for the course, so the commands and screen examples are more similar.

  15. Hope

    The first time you use Git, set up a few configuration values: your name, email, default editor, and how Git handles line endings. Configuration can be set at three levels. System settings apply to all users of the computer; global settings apply to all repositories for your user; and local settings apply only to the repository you’re currently in.

  16. Simon

    For example, a global name can be set with `git config --global user.name "Your Name"`, and an email with `git config --global user.email you@example.com`. Quotes are needed around a value with spaces, such as a name, but not around an ordinary email address.

  17. Hope

    You can choose a default editor with `git config --global core.editor "code --wait"`, assuming VS Code’s `code` command is available on your path. The `--wait` option tells the terminal to wait until you close that editor window. You can edit global settings with `git config --global -e`.

  18. Simon

    Another important setting is `core.autocrlf`, which controls line endings. Windows normally marks a line ending with carriage return and line feed, while Mac and Linux use line feed. If people on different systems collaborate, inconsistent handling can cause confusing changes.

  19. Hope

    The recommended setting in the lesson is `git config --global core.autocrlf true` on Windows. That lets Git remove the carriage return when storing code and put it back when checking code out. On Mac or Linux, use `input`: Git removes an accidentally present carriage return when storing the code, but doesn’t add one when checking it out.

  20. Simon

    For help, you can look up a command’s documentation online, run `git config --help` for the full manual in the terminal, or use `git config -h` for a short summary. In the full manual, Space moves to another page and `q` exits.

  21. Hope

    Now for Git’s core workflow. First create a project directory, move into it, and run `git init`. That initializes an empty repository in the directory.

  22. Simon

    Git creates a hidden `.git` subdirectory to store information about the project’s history. `ls -a` can reveal it when a normal file listing doesn’t. It’s an implementation detail, so don’t casually edit or delete it: removing `.git` removes the repository and its history.

  23. Hope

    A Git repository has a working directory—the files you’re actively editing—and a staging area, also called the index. When you reach a state you want to record, you stage the relevant changes, review them, and then commit them. A commit is a snapshot of the project at that point.

  24. Simon

    The staging area is an intermediate step: it’s the proposed contents of the next commit. That gives you a chance to choose which changes belong together. You can stage one part of your work and leave another part out for a later commit.

  25. Hope

    Here’s a detail that trips people up: after a commit, the staging area doesn’t simply become an empty, unrelated space. It represents the same project snapshot as the latest commit. When you edit a file afterward, the staging area still has its previous version until you stage the new change.

  26. Simon

    You can demonstrate the basics with ordinary text files, without needing a particular programming language. For instance, a shell command like `echo hello > file1.txt` writes text to a file. After `git init`, Git does not automatically track all the files in your folder.

  27. Hope

    Run `git status` to see what’s going on. New files appear as untracked until you add them. `git add file1.txt` stages one file; you can name several files, use a pattern such as `*.txt`, or use `git add .` to add changes throughout the directory.

  28. Simon

    Be thoughtful with `git add .`, because it can include files you don’t want in the repository. Large binary files, generated files, and logs may add size without being useful to share. We’ll cover how to exclude files with `.gitignore` in a moment.

  29. Hope

    Staging takes a snapshot of the file’s contents at that moment. So if you stage `file1.txt` and then edit it again, the staged version and working-directory version differ. Run `git add file1.txt` again to stage the latest version.

  30. Simon

    When the staged snapshot is ready, `git commit -m "Initial commit"` records it in the repository with a message. A useful message explains what the snapshot represents. If a short line isn’t enough, run `git commit` without `-m`: Git opens your configured editor, where you can write a short subject and, after a blank line, a longer explanation.

  31. Hope

    A commit message matters because it becomes part of the project’s history and helps you and your teammates understand the work later. The lesson recommends keeping the subject short—ideally under 80 characters—and writing clearly. Many people use the present tense, such as “Fix the bug,” but the key is to follow a consistent team convention.

  32. Simon

    Commit size matters too. A commit shouldn’t be so small that it records every tiny file update, but it shouldn’t bundle days of unrelated work either. Commit as you reach useful checkpoints, and make each commit represent one logically separate change—for example, keep a bug fix and an unrelated typo in separate commits.

  33. Hope

    There is a shortcut for committing without first staging: `git commit -a -m "Fix the sign-up bug"`. The `-a` option includes modified files that Git already tracks. It does not add new, untracked files, and you give up the usual opportunity to review exactly what you staged.

  34. Simon

    So treat that as a deliberate shortcut, not the default. Staging is valuable precisely because it lets you review and select the next snapshot. The lesson recommends staging first most of the time.

  35. Hope

    Removing and renaming files show why the working directory and staging area need to be understood separately. If you use a regular shell command such as `rm file2.txt`, the file disappears from the working directory, but Git still has the old version staged until you stage the deletion.

  36. Simon

    You can stage that deletion with `git add file2.txt`. Or use `git rm file2.txt`, which removes the file from both the working directory and the staging area in one step. Then commit the deletion like any other change.

  37. Hope

    A regular move command can rename a file in your working directory, but Git initially sees a deletion and a new untracked file. Stage both sides of that change, and Git can recognize the rename. A more direct option is `git mv old-name new-name`, which updates the working directory and staging area together.

  38. Simon

    Git also lets you ignore files and directories with a `.gitignore` file in the project root. For example, adding `logs/` tells Git to ignore that directory. You can include individual names or patterns such as `*.log`, and lines beginning with `#` are comments.

  39. Hope

    The important catch is that `.gitignore` doesn’t stop tracking something that was already committed. If you committed a generated `bin/` directory and only later add `bin/` to `.gitignore`, Git will continue noticing changes to tracked files there.

  40. Simon

    To stop tracking that directory but leave it on disk, use `git rm --cached -r bin/`. The `--cached` option removes it from the index—the staging area—without deleting the working copy, and `-r` makes the operation recursive. Commit that removal, and Git will then ignore future changes there.

  41. Hope

    You can find `.gitignore` templates for different programming languages on GitHub. Those templates often exclude generated files, such as Java class files, and common log files—things that don’t need to be shared in the repository.

  42. Simon

    For a compact view of changes, run `git status -s`. It uses two columns: the left column describes the staging area, and the right column describes the working directory. A red `M` in the right column means a tracked file is modified but those changes aren’t staged; `??` marks an untracked file.

  43. Hope

    After staging a modification, the `M` appears in the left column. If you edit the file again without restaging, you’ll see a staged change in the left column and another, unstaged change in the right. For a new staged file, the short status shows `A` in the staging-area column.

  44. Simon

    Status tells you which files changed, but not the exact lines. For that, use `git diff --staged` to compare the staged snapshot with the last commit—the changes proposed for the next commit. Use `git diff` with no arguments to compare the working directory with the staging area, which shows unstaged changes.

  45. Hope

    In a diff, minus signs indicate lines in the older version and plus signs indicate lines in the newer version. A new file has no old version in the last commit, so the diff reflects that. The header gives line-number context, and Git may show only the relevant chunk rather than an entire long file.

  46. Simon

    Terminal diffs are useful to understand, but visual diff tools can make comparisons easier. The lesson shows configuring VS Code as a diff tool with `diff.tool` and `difftool.vscode.cmd`, using `code --wait --diff` and placeholders for the local and remote versions. Then `git difftool` opens the visual comparison for unstaged changes, while `git difftool --staged` opens staged changes.

  47. Hope

    Next, inspect the history with `git log`. It lists commits from newest to oldest, including each commit’s identifier, author, date, and message. A commit ID is a unique 40-character hexadecimal identifier, not an incrementing revision number.

  48. Simon

    For a compact list, use `git log --oneline`; Git shortens the IDs, usually to seven characters when that’s enough to identify the commit. Add `--reverse` to show the history in the opposite order, from the earliest commit forward. Press Space to page through longer output and `q` to quit.

  49. Hope

    To inspect a particular commit, use `git show` followed by its ID. You can usually provide only enough starting characters to identify it unambiguously. You can also refer to the current commit with `HEAD`, or use `HEAD~1` for the commit one step earlier.

  50. Simon

    `git show` normally displays commit details and the changes made in that commit. To see a file’s exact contents at a particular commit, use a colon and its path—for example, `git show HEAD~1:.gitignore`.

  51. Hope

    For a list of files and directories in a commit, use `git ls-tree HEAD~1`. Git represents directories as tree objects and files as blob objects, each stored in its database with an identifier. Commits are objects too; Git can display an object’s contents with `git show` and its identifier.

  52. Simon

    Let’s finish with ways to undo changes safely. If you’ve staged something but decide it shouldn’t be in the next commit, use `git restore --staged file1.js`. This restores the staged copy from the last commit, leaving your change in the working directory unstaged.

  53. Hope

    That behavior follows the relationship between the areas: restoring the staging area uses the repository’s last committed version. If the staged item is a newly added file with no version in the last commit, `git restore --staged` removes it from staging and returns it to the untracked state.

  54. Simon

    To discard tracked changes in your working directory, use `git restore file1.js`. Git replaces the working copy with the version in the staging area. You can use a period to restore all applicable files, so be sure you really want to throw away those changes.

  55. Hope

    That command doesn’t remove untracked files, because Git has no earlier tracked version to restore. To remove untracked files, use `git clean -f`; add `-d` to remove untracked directories as well, as in `git clean -fd`. Git requires the force option because deleted untracked files can’t be recovered through Git.

  56. Simon

    Finally, suppose you committed a deletion and later realize you need the file back. You can restore it from an earlier commit without undoing the commit itself. For example, `git restore --source=HEAD~1 -- file1.js` takes the file from the commit before the current one and puts it back into the working directory.

  57. Hope

    That’s different from reverting the commit: here, you’re restoring a file’s contents from a chosen source. Afterward, check `git status` to see the resulting change and decide whether to stage and commit it.

  58. Simon

    So the mental model is: edit in the working directory, select changes in the staging area, and record a snapshot with a commit. Check status, review diffs before committing, and use history or restore commands when you need to understand or recover a state.

  59. Hope

    And as you practice, focus on understanding what each command changes—working directory, staging area, or repository—instead of trying to memorize everything at once. That foundation is what makes Git’s everyday workflow much easier to use with confidence.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent