Git
Git manages source code and tracks changes in your repository. As a distributed version control system, Git allows engineers to work together to minimize friction, maximize flow, and provide simpler collaboration and version history.
- changes are tracked as commits and contain messages, author, and timestamp
- utilizes branches to allow for parallel development
- ability to rewind to a previous point in time when necessary
- maintains version history
- secure and resistant to corruption
All history and related commit metadata is contained within a hidden .git
folder.
What is a commit? The
git commit
command captures a snapshot of the project's changes. Committed snapshots can be thought of as “safe” versions of a project.
What does Git track?¶
By default, Git will track everything in your working directory, including all files of all filetypes, folders and subfolders.
- application code (source, test)
- infrastructure code (Configuration management, Dockerfiles)
- configuration (pipeline scripts, environment properties)
- metadata (READMEs, manifests)
Everything that requires your application to run should be stored, tracked, and versioned in Git
Although Git can watch everything in your working directory, not everything on your local machine should be stored in Git. - built artifacts and test results - binaries - local system properties from IDE - secrets, passwords, and other security-sensitive data
Add a list of files or folders in
.gitignore
to instruct Git to not track changes.
Developer Workflow & Git Lifecycle¶
A typical developer workflow is centered around using Git. Using TBE principles, engineers create a branch tied to the work item, commit changes locally, and submit a pull request on the remote Git server.
Git Commands (CLI)¶
git clone url-of-repo
- place a copy of the remote git repository onto your local machine.
git checkout name-of-branch
- change to a different branch for development.
git add
- place changed files in the staging area.
git commit -m "describe your change"
- commit your changes to git's history
git push
- push your changes to the remote git server.
git status
- show the state of git (which files have been modified or staged)
git branch
- display which branch you are currently working on
git log
- show a history of commits on your branch
git stash
- temporarily store changes and then git stash pop
to restore changes.
Tools¶
Git is a native command-line (CLI) tool. Although IDEs and GUIs can include Git integrations, engineers should be comfortable interacting with Git from the terminal.
- Mac users, use terminal with Xcode extension.
- Windows users, use GitBash.
Most IDEs have built-in extensions to use Git, including resolving merge conflicts and complex actions like rebase. As with all IDEs, each software has its own interpretation of how to interact with the underlying tool. We recommend engineers be comfortable with Git on the CLI and its integration with their IDE of choice.
Git in CLI (left) with equivalent view in Visual Studio Code (right) |
Changing Files in Git¶
There are three states of changed files within Git. Newly created files will be labeled as untracked, whereas changed files that Git has been already tracking will be either in an unstaged or staged state.
- Untracked - new file is not watched by Git
- Unstaged - file is modified but not added to staging
- Staged - file is modified, in staging, and ready to be committed
At any point in time, use git status
to see the state of changed files within Git. It will tell you which files are in which state and provide helpful color-coded guides, while provided instructions on what you can do next.
Staging Changes¶
The staging area is a temporary location in Git that allows you to continue making changes to the working directory. When engineers are ready to attach their changes to version control, they will make a meaningful commit from the staging area, resulting in a clear, distinct moment in time reflecting a unique version. Remember that files must be added to the staging area before committing them.