Git workflow and common practices
Contents
2.2. Git workflow and common practices#
2.2.1. Initializing a Git repository#
The first step is to tell Git that we want to keep track of files in this repository. To initialize an existing directory as a Git repository, use the following command
$ git init
Note: it is also possible to initialize a Git repository at a given location using
$ git init <directory_path>
Best practice: after creating a new project it is recommended to create an empty commit. The rational will be explained later in this tutorial. This can be done using
$ git commit --allow-empty -m "[EMPTY] Initial commit."
2.2.2. An overview of the different Git states#
There are two main states in Git: tracked and untracked.
Tracked files are files from a previous snapshot or that were newly staged; they can be unmodified, modified or staged. i.e. those are the files that Git knows about.
Untracked files are every other files. They are files in your working directory that Git is not tracking.
source: https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository
2.2.3. Keeping track of files#
After initializing a repository, we will wanted to start tracking files in Git.
To view the status of a repository including which files are untracked or modified, use the following command
Note: Optionally, a path can be specified to only get the status of a specific file or sub-directory.
$ git status [path]
To move an untracked or modified file to the staging area, use the following command
$ git add [file]
To snapshot the currently staged files in a Git repository, use this command
$ git commit
You will be prompted to enter a commit message.
Best practice: write a concise and descriptive message for you commits. Others, and your future self, will thank you.
Note: It is also possible to write the commit message as part of the command, using the the -m
flag follow by the message.
$ git commit -m "[descriptive message]"
Looking at the status of the repository again, we see that the files we committed are no longer untracked.
$ git status
As a project progress, you might want to look back at its history. This will be useful to see when changes were made and who made them. To list the log of a Git repository, use the following command
$ git log
commit 00200f9b51a2a859b8af33f6ad7c139ebc984ee3 (HEAD -> main)
Author: mathdugre <mathdugre@pm.me>
Date: Thu Jan 5 15:47:13 2023 -0500
[EMPTY] initial commit.
2.2.4. Rolling back changes#
Having an history for our file is important, but being able to go back to specific state is even more critical.
For example, when developing a project, some modifications might need to be undone.
The command git checkout [commit-sha]
provides a way to go back to a specific state in history.
Using the git log
command, we can find the sha of the commit we want to jump to.
$ git checkout 00200f9b51a2a859b8af33f6ad7c139ebc984ee3
Another common use case is going back a specific numbers of commit back into the history.
For example, rolling back one commit is done as follow:
$ git checkout HEAD~1
Note: In git, HEAD
reference to the current commit.
2.2.5. Working on different features concurrently#
A critical component of Git are branches. They allows the creation of the repository that diverges from the main project. In other words, branches in Git let you work on different features of a project concurrently. Moreover, it keeps the code of in-development features independent from the main code base as well as other in-development code.
source: https://www.atlassian.com/git/tutorials/using-branches
To list all the current branch of the local repository, use the following command
$ git branch
Note: If your default branch is named master
rename it to main
, using this command
$ git branch -m master main
To create a new branch from the current commit, use the command
$ git branch [branch-name]
To checkout (switch) from one branch to another, use this command
$ git checkout [branch-name]
To delete a branch, use this command
$ git branch -d [branch-name]
Note: when creating a new branch and directly checking out to it, this shortcut is convenient
$ git checkout -b [branch-name]
2.2.6. Other useful commands#
To remove files from the staging and keep the change in the working directory, use the following command
Warning: Misuse of the git reset
command could result in loss of work. Moreover, AVOID using git reset on public history (shared with others).
$ git reset [files]
It is possible to merge the history of another branch into the current one, using this command
$ git merge [branch]