Using Git
What is git?
Git is a distributed version control system. It is a tool that helps you keep track of changes in your codebase. It is a tool that helps you collaborate with others on a project. It is a tool that helps you manage your codebase.
Distributed means that every developer has a copy of the repository on their local machine.
Difference of git and GitHub:
- Git is a version control system, installed on your local machine.
- GitHub is a web-based platform that hosts your git repositories. (Consider it as a cloud storage for your codebase)
Why use git?
- Collaboration: Git allows multiple developers to work on the same project at the same time.
- History: Git keeps track of every change made to the codebase.
- Branching: Git allows you to create branches to work on new features without affecting the main codebase.
- Revert: Git allows you to revert to a previous state of the codebase.
- Backup: Git is a great way to backup your codebase.
Git is really suitable for text based project such as codebase. It is not suitable for large binary files such as images, videos, etc.
Git concepts
graph LR
A[Commit 1] --> B[Commit 2]
B -->|Master| C[Commit 3]
B -->|New_branch| D[Commit 4]
C --> E[Commit 5]
E --> F[Commit 6]
D -->|Merge| F
F --> G[Commit 7]
- Repository: A repository is a collection of files and folders that are being tracked by git.
- Commit: A commit is a snapshot of the codebase at a specific point in time.
- You can think of a commit as a save point in a video game.
- We can revert to a previous commit if needed.
- Each commit has a unique identifier called a hash.
- Branch: A branch is a separate line of development.
- The main branch is called
master
.
- The main branch is called
- Merge: Merging is the process of combining two branches into one.
- Conflict: A conflict occurs when two branches have made changes to the same line of code.
- We have to manually resolve the conflict.
- Remote: A remote is a copy of the repository that is hosted on a server.
- GitHub is a popular remote.
- Pull: Pulling is the process of downloading changes from the remote to your local machine.
- Push: Pushing is the process of uploading changes from your local machine to the remote.
- Clone: Cloning is the process of downloading a copy of the repository from the remote to your local machine.
- Fork: Forking is the process of creating a copy of a repository on GitHub.
Basic git commands
Nowaday, many IDEs and code editors has built-in GUI for git. But understand the basic git commands could help you understand what is happening behind the scene.
git init
: Initialize a new git repository.git clone <url>
: Clone a repository from a remote.
Create a new commit (Save point):
git add <file>
: Add a file to the staging area.git add .
: Add all files to the staging area.git commit -m "message"
: Commit the changes to the repository.
Add remote:
git remote add origin <url>
: Add a remote to the repository.
Push to remote:
git push origin master
: Push the changes to the remote.git push -u origin master
: Push the changes to the remote and set the upstream.git push
: Push the changes to the remote.
Pull new change:
git pull
: Pull the changes from the remote.
Create a new branch:
git branch <branch-name>
: Create a new branch.git checkout <branch-name>
: Switch to a branch.git checkout -b <branch-name>
: Create a new branch and switch to it.
Merge branches:
During the merge process, you may encounter a conflict. You have to manually resolve the conflict. Merging operation is not always successful.
git merge <branch-name>
: Merge the branch into the current branch.
Revert to a previous commit:
git log
: Show the commit history.git checkout <hash>
: Revert to a previous commit.
Deletion:
git branch -d <branch-name>
: Delete a branch.git push origin --delete <branch-name>
: Delete a remote branch.git rm <file>
: Remove a file from the repository.
Typical git workflow
- Create a new branch for a new feature.
- Make changes to the codebase.
- Add and commit the changes.
- Push the changes to the remote.
- Create a pull request.
- Review the changes.
- Merge the changes into the main branch.
Tips
- Pull before work: Always pull the latest changes from the remote before starting to work.
- Commit often: Commit your changes often. It is easier to revert to a previous commit if needed.
- Write meaningful commit messages: Write a meaningful commit message that describes the changes you made.
- Use branches: Use branches to work on new features without affecting the main codebase.
- Review changes: Review the changes before merging them into the main branch.
- Resolve conflicts: Resolve conflicts as soon as possible.