Version control systems are software that help track changes make in code over time. As a developer edits code, the version control system takes a snapshot of the files. It then saves that snapshot permanently so it can be recalled later if needed.
Without version control, developers are tempted to keep multiple copies of code on their computer. This is dangerous because it's easy to change or delete a file in the wrong copy of code, potentially losing work. Version control systems solve this problem by managing all versions of the code, but presenting the team with a single version at a time.
Git is a distributed version control system, which means that a local clone of the project is a complete version control repository. These fully functional local repositories make it easy to work offline or remotely. Developers commit their work locally, and then sync their copy of the repository with the copy on the server. This paradigm differs from centralized version control where clients must synchronize code with a server before creating new versions of code.
Git's flexibility and popularity make it a great choice for any team. Many developers and college graduates already know how to use Git. Git's user community has created resources to train developers and Git's popularity make it easy to get help when needed. Nearly every development environment has Git support and Git command line tools implemented on every major operating system.
Git works by remembering the changes to your files as if it's taking snapshots of your file system.
git status
The first and most commonly used Git command is git status
.
git status
displays the state of the working tree. It lets you see which changes are currently being tracked by Git, so you can decide whether you want to ask Git to take another snapshot.
git add
git add
is the command you use to tell Git to start keeping track of changes in certain files.
The technical term is staging these changes. You'll use git add to stage changes to prepare for a commit. All changes in files that have been added but not yet committed are stored in the staging area.
git commit
After you've staged some changes for commit, you can save your work to a snapshot by invoking the git commit
command.
Commit is both a verb and a noun. It has essentially the same meaning as when you commit to a plan or commit a change to a database. As a verb, committing changes means you put a copy (of the file, directory, or other "stuff") in the repository as a new version. As a noun, a commit is the small chunk of data that gives the changes you committed a unique identity. The data that's saved in a commit includes the author's name and e-mail address, the date, comments about what you did (and why), an optional digital signature, and the unique identifier of the preceding commit.
git log
The git log
command allows you to see information about previous commits. Each commit has a message attached to it (a commit message), and the git log
command prints information about the most recent commits, like their time stamp, the author, and a commit message. This command helps you keep track of what you've been doing and what changes have been saved.
git help
Use this command to easily get information about all the commands you've learned so far, and more.
Remember, each command comes with its own help page, too. You can find these help pages by typing git <command> --help
. For example, git commit --help
brings up a page that tells you more about the git commit command and how to use it.
The following sections describe how to install and maintain Git for the three major platforms.
Install Git for Windows
Download and install Git for Windows. Once installed, Git is available from the command prompt or PowerShell. It's recommended that you select the defaults during installation unless there's good reason to change them.
Git for Windows doesn't automatically update. To update Git for Windows, download the new version of the installer, which updates Git for Windows in place and retains all settings.
Install Git for macOS
macOS 10.9 (Mavericks) and higher installs Git the first time you attempt to run it from the Terminal. While this method is an easy way to get Git on a system, it doesn't allow for control over how often updates or security fixes are applied.
Instead, it's recommended that you install Git through Homebrew and that you use Homebrew tools to keep Git up to date. Homebrew is a great way to install and manage open source development tools on a Mac from the command line.
Install Homebrew and run the following to install the latest version of Git on a Mac:
> brew install git
To update the Git install, use Homebrew's upgrade option:
> brew upgrade git
Install Git for Linux
Use the Linux distribution's native package management system to install and update Git. For example, on Ubuntu:
> sudo apt-get install git
If you'd like to dig deeper, check this Introduction to Git recap video with Dr. G.
Here are more resources:
-
Visit the Everyday Git site or use the
git help everyday
command. - Review Git and GitHub learning resources.
- Check out the documentation section of Git's official website.
The content presented in this documentation site was retrieved from:
- Microsoft learn: Learn Git.
- GitHub Foundations Learning Path Part 1: Introduction to Git.