Use Git and Use It Well
0 Terminology
Git is a distributed version control system used to track changes in code and coordinate development among multiple individuals in a project.
- Repository: Also known as a repo, it’s the place where code and its history of changes are stored. Each project has a Git repository, which can be stored locally or on a remote server.
- Commit: A commit is a snapshot of code that records changes made to files. Each commit has a unique hash value to identify it.
- Branch: A branch is a divergent line of code that allows you to make modifications without affecting the main development. The main branch is typically the
master
(ormain
) branch, while other branches can be used for developing new features, fixing bugs, etc. - Merge: Merging is the process of combining changes from one branch into another to keep the code in sync.
- Pull and Push: Pulling retrieves changes from a remote repository to your local one, while pushing sends your local changes to the remote repository.
- Clone: Cloning is the process of copying a remote repository to your local machine, typically used to start new development work.
- Remote Repository: A remote repository is a repository stored on a network server, used for sharing code during collaboration.
- Conflict: When multiple branches modify the same file differently and an attempt is made to merge them, a code conflict may arise. Resolving conflicts involves manually editing files and choosing which changes to keep.
1 File’s Three States
In Git, files can exist in three different states.
- Modified: This is the initial state of a file, indicating that its content has been changed but these changes have not yet been saved to the version control system.
- Staged: After running
git add
on a modified file, the changes are added to the staging area (also known as the index). This means Git records the changes you want to include in the next commit. - Committed: When you run
git commit
, Git saves the changes in the staging area as a new commit in the repository. This commit has a unique hash value and a message you attach to describe the changes made.
The transitions between these three states are as follows.
- From Modified to Staged: Use
git add
to add the modified file to the staging area. - From Staged to Modified: Use
git reset
to revert the staged file back to its initial state. - From Staged to Committed: Use
git commit
to save the staged changes as a new commit in the repository.
2 Interacting with Remote Repositories
Use
git clone
to copy a remote repository to your local machine. Cloning creates a new local repository and sets up tracking of the remote’s main branch.Use
git remote add <remote-name> <remote-url>
to add a remote repository to the list of remotes for your local repository. You can provide a name for each remote repository to reference it later.About
origin
:origin
is the default remote repository alias used to represent the remote repository you cloned or fetched code from. When you clone a repository, Git automatically sets the URL of that repository as the URL for the default remote repository aliasorigin
.Use
git remote -v
to view the list of remote repositories associated with your current repository.Use
git remote remove <remote-name>
to remove a specific remote repository from your local repository.
3 Branches
3.1 Creating, Switching, Merging, and Deleting Branches
- Use
git branch <branch-name>
to create a new branch. - Use
git branch
to see a list of all branches. - Use
git branch -r
to view a list of remote branches. - Use
git checkout <branch-name>
to switch to a branch. - Use
git merge <source-branch>
to merge the current branch with<source-branch>
. - Use
git branch -d <branch-name>
to delete a merged branch. - Use
git branch -D <branch-name>
to forcefully delete an unmerged branch.
3.2 Pulling Branches
Use
git fetch
to get the latest status of remote repository without automatically merging it into your local branch.Use
git merge <branch-name>
to merge the latest changes from a remote branch into your local branch.Use
git pull origin <branch-name>
to fetch the latest changes from the remote repository and immediately merge them into your current branch. It’s a combination ofgit fetch
andgit merge
.fast-forward
andnon-fast-forward
:In Git,
fast-forward
andnon-fast-forward
are two different ways to merge branches. Afast-forward
merge happens when you’re merging a branch into another and the branch you’re merging into has no new commits of its own. In this case, Git simply moves the pointer of the merging branch to the latest commit of the merged branch.However, when you want to merge a branch into another and that other branch has its own new commits, Git can’t simply move the pointer, as it would result in a forked history. In this case, Git creates a new merge commit that combines the changes of both branches. This is called a
non-fast-forward
merge.
4 Considerations
- Divide related changes into small, logically coherent commits for easier tracking and history management.
- Write clear and concise commit messages that describe the purpose and changes of each commit.
- Use branches to isolate different features, fixes, and tasks. The main branch should remain stable while development is done on other branches.
- Use
git pull
before starting work to fetch the latest changes from the remote repository and avoid working with outdated code. - Avoid using force-push (
git push --force
) on shared branches to prevent unnecessary disruption to the commit history. - Add a
.gitignore
file to exclude files and directories that shouldn’t be tracked.