Git Essential Training: The Basics

  1. What is Git?

    • Version Control: Git is software that keeps track of changes to files and other related things, but it is especially good at keeping track of text changes. Git is a Version control System (VCS), and there are many other programs like it. There is also another system of programs called Source Code Management (SCM).
    • The history behind Git: There are 5 important version control systems that came before Git: Source Code Control System (SCCS): which allowed you to save a file and instead of saving the same file multiple times, it saved the original, then the sets of changes that came with it; Revision Control System (RCS): was a lot faster and could save the current file along with the sets of changes; Concurrent Versions System (CVS): allowed you to modify multiple files at once, which was better than SCCS and RCS, because they could only modify one file at a time. Another benefit of CVS was that mulitple people will also be able to modify the same files. Working with remote repositories was further improved when Apache Subversion (SVN). SVN could track text, images, and even the entire directory. Bitkeeper SCM was a closed, proprietary source. There were two main versions: a "community" version and a paid version. The community version had less things available than the paid version, but it still was a great software for web design. Git came around the time that Bitkeeper stopped being free, and it was a "replacement" for Bitkeeper.
    • About distributed version control: Different users maintain their own repositories, no central repository, and changes are saved as sets (tracks changes, not version). They are mostly working copies. We also don't need to have it communicate with a central server, which makes it faster, and no network access required, and there is no single fail point.
  2. Install Git

    • Install Git on a Mac: There are two ways to install Git on a Mac: an installer or Homebrew. To get the installer, go to the main GitHub website, and here is a link to download the version for Mac. If you would like to install Homebrew to get Git, here is the link.
    • Install Git on Windows: To get the installer for Windows, go to the main GitHub website.
    • Install Git on Linux: To get the installer for Linux, go to the main GitHub website, and here is a link to download the version for Linux.
    • Basic git configuration: Here are three basic commands that enable you to modify your Git configurations:
      System
      git config --system
      User
      git config --global
      Project
      git config
      

      To get it started you would need to type in the git config that you are going to use, then user.name "Your user name", same for email: user.email "Youremail@email.com", then finally type git config --list, and it will list all the Git default configurations.
    • Git auto-completion: For git-completion it is a seperate file from the rest of Git for Mac and Linux, Windows should already have it installed. you will have to go to this site to get the code. Once you have the file, do the move command, and put a (.) in front of the rename section.
    • Git help: To access the Help section, just type in git help. You can also type the command you want to know more about after you type help.
  3. Getting Started

    • Initialize a repository: To initialize a repository, you would need to navigate to where you would want to put that file, and type git init.
    • Where Git files are stored: all files are stored more or less in the .git directory, along with all of the tracking files, and if you delete these files, git will no longer wok on your computer, so DO NOT DELETE THEM.
    • Your first commit: When you make a commit, the steps to do it are very simple: Make a change, Add the changes, and then Commit them with a message.
    • Write a commit message: Usually commit messages are short single lines of text, but to make a more descriptive message, put a blank line then the description. The description should at least be less than 72 characters. And write your commit messages in present tense, not past tense. Bullet points are hyphens or asterisks. Finally be clear and descriptive with the commit messages.
    • View the commit log: To view the commit log, type git log.
  4. Git Concepts and Architecture

    • The three trees: There are two trees for most version control systems: repository and working, and to bring a file from the repository, it is called checkout, and to put a file into the repository, it is called commit. There are three trees for Git: Repository, working, and in between is the staging index, and to put a file in the repository, there is an extra step: the add command is before the commit command.
    • Git workflows: When we make a file or take one from the repository, it is in the working area, then when we are done with the changes we made, we type git add file.txt, then when we are ready, we type git commit -m "message", and now it is in the repository. Now that we have made more changes to it, we use git add file.txt to put it in the staging area, then we do git commit -m "message", and it is in the repository, along with the first set of changes. Now if we had a third set of changes, then we would do the same thing as the first two times, and now we have a third set of changes in the repository.
    • Hash values (SHA-1): Git generates a random mix of numbers and letters called a checksum, as the name for each change set. Same data = same checksum. If you change the data inside of a file, it changes the checksum.
    • The HEAD pointer: Is the last state of repository, a pointer to current branch in repository.
  5. Make Changes to Files

    • Add files: To add files, you do the steps from the Git workflows section, but to see what needs to be staged and what needs to be commited, you use the git status.
    • Edit files: To edit files, it is the same as adding files.
    • View changes with diff: If you make changes but don't remember what they were then you could use the git diff, and it will show the changes that have been made.
    • View only staged changes: To view only the changes between the staged and repository files, use git diff --staged/--cached, and it will only show the changed files in the staged area even if there are changed files in the working directory.
    • Delete files: To delete files there are two ways: Manually delete it, or have Git delete it with the rm command.
    • Move and rename files: To move the file is the same as renaming it, which bth are easy to do, just use the mv command for both, but for moving any files put the name of the folder in front.
  6. Use Git with a Real Project

    • Initialize Git: We first initialize Git into our project.
    • View File Edits: To view file edits you just use the git diff command.
    • Stage and commit shortcut: The shortcut for staging and committing is git commit -a/git commit --all
    • View a commit: To view a commit you can type git show (commit #)
    • Compare commits: To compare two commits, use: git diff (oldest commit #)..(commit to compare #)
    • Multiline commit messages: To make a multiline commit message, type in the basic commit, but don't type the -m for commit message, hit Enter, then type a more detailed description of the commit (but hit enter for a space between the single line then the multiline), when your text editor pops up.
    • Make atomic commits Atomic commits are small, affecting one aspect, and are easier to work with, and find bugs.
  7. Undo Changes

    • Undo working directory changes: If you did something accidental, like removed your navigation or something, and closed the file after you saved it. you can bring it back with Git. When you type git status, it will show that a change occured and it has a couple of commands above it that could help with getting it ready to commit, or revert those changes.
    • Unstage files: To unstage files, it is very similar to undoing the working directories. simply use the command above the results fro the git status command.
    • Amend commits: In case you forgot to add something to a commit that should have been there, you can amend the commit and add your change to it. The command to type is git commit --amend -m "message".
    • Retrieve old versions: To retrieve old versions of files you type git checkout (commit#) -- explorers.html.
    • Revert a commit: To revert a commit, you type git revert (comit#).
    • Remove untracked files: To remove untracked files that you don't want anymore, you can just do git clean with extension: -i, -n, or -f.
  8. Ignore Files

    • Use .gitignore files: To get Git to ignore files, you can use a .gitignore folder, or use certain charcters like these: *? [aeiou] [0-9]
    • Ideas on what to ignore: Here are some ideas of files to ignore: compiled source files, packages and compressed files, logs and databases, operating system generated files, and user-uploaded assets (images, PDFs, videos). Here is a site of .gitignore file templates
    • Globally ignore files: To make a file that would make Git ignore files globally, use this command: git config --global core.excludesfile
    • Ignore tracked files: To make git ignore files that it is already tracking, just put the names of the files in your .gitignore file. Then type git rm --cached (filename).extension
    • Track empty directories: To track an empty folder, the best way is to put a file called .gitkeep and leave it empty.