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 addon 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 addto add the modified file to the staging area.
- From Staged to Modified: Use git resetto revert the staged file back to its initial state.
- From Staged to Committed: Use git committo save the staged changes as a new commit in the repository.
2 Interacting with Remote Repositories
- Use - git cloneto 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:- originis 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 alias- origin.
- Use - git remote -vto 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 branchto see a list of all branches.
- Use git branch -rto 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 fetchto 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 of- git fetchand- git merge.- fast-forwardand- non-fast-forward:- In Git, - fast-forwardand- non-fast-forwardare two different ways to merge branches. A- fast-forwardmerge 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-forwardmerge.
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 pullbefore 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 .gitignorefile to exclude files and directories that shouldn’t be tracked.