Git is a powerful version control system that has become an essential tool for developers worldwide. In this article, we will explore Git's core concepts, commands, and workflows with real-world use cases to help you understand its functionality better.
What is Git?
Git, short for Global Information Tracker, is used to track files, maintain multiple versions of files, and collaborate efficiently on projects. Unlike other version control systems such as SVN or Mercurial, Git stands out for its distributed architecture, speed, and robust branching and merging capabilities. This makes it an ideal choice for both small teams and large-scale open-source projects. It is:
Platform-independent
Free and open-source
Efficient in handling larger projects
Written in C programming and released in 2005
Git Workflow in Real-Time
Git workflow typically involves the following stages:
This diagram illustrates the transition between key Git stages: Working Directory, Staging Area, and Repository. Let’s break them down:
1. Working Directory:
This is where your project files reside. Git is initially unaware of the changes until files are staged or committed.
Example: You’re working on a web project with HTML, CSS, and JavaScript files. You add a new feature in script.js
, but Git doesn’t track this change until you explicitly stage it with git add script.js
.
2. Staging Area:
This is like a draft area where you prepare changes before committing.
Example: You’ve updated style.css
and index.html
. Instead of committing these files directly, you add them to the staging area using git add style.css index.html
. This ensures only the selected changes are part of the next commit.
3. Repository:
The repository is your project’s database. It contains the complete history of your changes.
Example: After staging your changes, you commit them to the repository using git commit -m "Added responsive design"
. This creates a checkpoint you can revert to if needed.
Real-Time Case Study: Collaborative Development
Imagine a team of developers working on a mobile application. They use Git to manage their project efficiently, enabling seamless collaboration by allowing each developer to work independently on different features. By regularly committing changes and using branches, the team minimizes the likelihood of merge conflicts. In case of overlapping modifications, Git’s conflict resolution tools highlight discrepancies, making it easier to integrate changes without overwriting anyone’s work. This collaborative workflow ensures all team members stay aligned while maintaining code quality and productivity.
Local and Remote Repositories:
Each developer clones the project repository using
git clone repo-url
.They work on features in their local repositories and push changes to the remote repository hosted on GitHub.
Branches for Features:
Developer A creates a branch
feature/login
usinggit checkout -b feature/login
to work on the login feature.Developer B works on
feature/signup
.
Merging and Resolving Conflicts:
- Developer A merges the
feature/login
branch intomaster
usinggit merge feature/login
. If there are conflicts withfeature/signup
, Git highlights them for resolution.
- Developer A merges the
Git Commands with Scenarios
Command | Description | Use Case |
git add file_name | Stages changes to a file, preparing it for the next commit. | You modify app.py to include a new API endpoint. Use git add app.py to stage this change for your next commit. |
git status | Shows the current status of your working directory and staging area. | Before committing, run git status to ensure all intended files are staged. |
git log | Displays the commit history. | To review recent commits, use git log . For a concise view, git log --oneline lists commits with abbreviated IDs and messages. |
git reset --soft HEAD~1 | Unstages the last commit but keeps changes in the working directory. | You accidentally committed incomplete code. Use this to re-edit your files. |
Create a .gitignore file | Specifies files and directories to be ignored by Git. | Add *.log to .gitignore to exclude log files from being tracked. |
This table provides a quick reference to commonly used Git commands along with their descriptions and practical use cases.
1. Tracking Files:
Command:
git add file_name
Scenario: You modify
app.py
to include a new API endpoint. Usegit add app.py
to stage this change for your next commit.
2. Checking the Status:
Command:
git status
Scenario: Before committing, run
git status
to ensure all intended files are staged.
3. Viewing History:
Command:
git log
Scenario: To review recent commits, use
git log
. For a concise view,git log --oneline
lists commits with abbreviated IDs and messages.
4. Resetting Changes:
Command:
git reset --soft HEAD~1
Scenario: You accidentally committed incomplete code. Use
git reset --soft HEAD~1
to unstage the last commit and re-edit your files.
5. Ignoring Files:
Command: Create a
.gitignore
fileScenario: Add
*.log
to.gitignore
to exclude log files from being tracked.
Advanced Git Features
1. Git Revert:
This command undoes changes by creating a new commit.
Command:
git revert commit_id
Scenario: A recent commit introduced a bug. Use
git revert
to roll back the changes while preserving the commit history.
2. Git Cherry-Pick:
Select specific commits to apply to another branch.
Command:
git cherry-pick commit_id
Scenario: A hotfix made on the
hotfix
branch needs to be applied tomaster
. Usegit cherry-pick
to apply the fix without merging the entire branch.
3. Git Stash:
Temporarily save changes you’re not ready to commit.
Command:
git stash
Scenario: You’re working on a feature but need to switch branches. Stash your changes with
git stash
and retrieve them later usinggit stash apply
.
Git Alternatives
While Git is widely used, alternatives include:
GitLab: A Git-based platform that not only provides version control but also integrates CI/CD pipelines, enabling teams to automate build, test, and deploy workflows. This can be ideal for projects requiring a streamlined DevOps process.
Pros: Robust CI/CD integration, self-hosting options, and enhanced security controls for enterprise use. Cons: Higher resource usage when self-hosted, and some features are locked behind premium tiers.
SVN (Apache Subversion): A centralized version control system that stores data in a central repository, making it easy to manage access controls and monitor changes in real-time.
Pros: Simpler for small teams, effective for managing large binary files. Cons: Lacks the distributed nature of Git, making it less resilient to server outages.
Bitbucket: Supports both Git and Mercurial repositories and integrates seamlessly with other Atlassian products like Jira.
Pros: Ideal for teams already using Atlassian tools, strong code review capabilities. Cons: Limited free tier for larger teams compared to platforms like GitHub.
Understanding the strengths and trade-offs of these alternatives can help teams choose the right tool for their specific workflows and collaboration needs.
GitLab: A Git-based platform with CI/CD features.
SVN: A centralized version control system.
Bitbucket: Supports Git and Mercurial repositories.
GIT LOG: is a command in the Git version control system that shows a history of commits made in a Git repository. It displays a list of past changes, including information like the commit message, author, date, and a unique identifier for each commit.
COMMANDS:
git log | used to see the history of the git |
git log --oneline | used to see only commit ID's and messages |
git log --pretty=oneline | Used to get only commit ID's and messages |
git log -1 | used to see the latest commit |
git log -3 | used to see latest 3 commits |
git log --follow --all filename | used to see the no of commits for a single file |
git log --graph --oneline --all | see all the history in graph |
GIT AMEND: it is a Git command used to make changes to the most recent Git commit. It allows you to edit the commit message, add additional changes, or both.
COMMANDS:
git commit --amend -m "message" | used to change the commit message for a latest commit |
git commit --amend --author "username " | used to change the author of latest commit |
git commit --amend --no-edit | used to commit the changes with previous commit |
GIT RESET: is a command in Git that allows you to move the HEAD and the current branch pointer to a specific commit, effectively "rewinding" or "resetting" your project's state. It's commonly used to undo changes or to unstage commits.
COMMANDS:
git reset --hard HEAD~1 | used to delete the latest commit along with the changes |
git reset --hard HEAD~3 | used to delete the latest 3 commits along with the changes |
git reset --soft HEAD~1 | used to delete only commits but not actions/changes |
git reset --soft HEAD~3 | used to delete only latest commits but not actions/changes |
GIT REVERT: is a command that allows you to undo or reverse the changes made in a previous commit. It creates a new commit that undoes the changes introduced by the specified commit, effectively taking your code back to a previous state without deleting commit history
git revert commit_id | used to delete a particular commit action and add a new commit for the change |
GIT IGNORE:
It will be useful when you don't want to track some specific files then we use a file called .gitignore
create some text files and create a directory with "jpg" files.
GIT BRANCHES:
A branch represents an independent line of development.
The git branch command lets you create, list, rename, and delete branches.
The default branch name in Git is master.
allows you to work on different features or changes to your code independently, without affecting the main or other branches.
It's a way to organize and manage your code changes, making it easier to collaborate and maintain your project.
COMMANDS:
git branch | used to see the list of branches |
git branch branch-name | to create a branch |
git checkout branch-name | to switch one branch to another |
git checkout -b branch-name | used to create and switch a branch at a time |
git branch -m old-branch new-branch | used to rename a branch |
git branch -d branch-name | to delete a branch |
git branch branch-name deleted-branch-id | Used to get deleted branch id |
git branch -D branch-name | to delete a branch forcefully |
The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.
Now all the things you have done is on your local system.
GIT MERGE:
Git merge is a command used in the Git version control system to combine changes from one branch.
To merge: git merge branch_name
GIT CHERRY-PICK:
Git cherry-pick is a command in Git that allows you to take a specific commit from one branch and apply it to another branch. It's like picking a cherry (commit) from one branch and adding it to another branch, allowing you to selectively copy individual commits without merging the entire branch.
Command: git cherry-pick commit_id
GIT MERGE CONFLICTS:
GIT makes merging super easy!
CONFLICTS generally araise when two people two people have changed the same lines in a file (or) if one developer deleted a file while another developer is working on the same file!
In this situation git cannot determine what is correct!
Lets understand in a simple way!
cat>file1 : hai all
add & commit
git checkout -b branch1
cat>file1 : 1234
add & commit
git checkout master
cat>>file1 : abcd
add & commit
git merge branch1 : remove it
git diff file1
vim file1
git add
git commit -m "final commits"
NOTE: Don't give file name on commit
Identify Merge Conflicts:
- see the file in master branch then you will see both the data in a single file including branch names that are dividing with conflict messages
Resolve Conflicts:
open file in VIM EDITOR and delete all the conflict dividers and save it!
- add git to that file and commit it with the command (git commit -m "merged and resolved the conflict issue in abc.txt")
GIT REBASE: Git rebase is a Git command used to incorporate changes from one branch into another. It allows you to re-organize and streamline the commit history by moving or combining commits from one branch onto another.
Command: git rebase-branch
GIT STASH: Using the git stash command, developers can temporarily save changes made in the working directory. It allows them to quickly switch contexts when they are not quite ready to commit changes. And it allows them to more easily switch between branches.
Generally, the stash's meaning is "store something safely in a hidden place."
COMMANDS:
git stash | to delete the changes temporarly |
git stash save "message" | to save the stash along with the message |
git stash apply | to get back the data again |
git stash list | to get the list of stashes |
git stash clear | to clear all stashes |
git stash pop | to delete the first stash |
git stash drop | used to delete the latest stash |
git stash drop stash@{2} | used to delete a praticular stash |
GIT TAGS: it tags are markers or labels that you can place on specific commits (versions) in a Git repository. These tags provide a way to give meaningful names to important points in the project's history, such as software releases or significant milestones.
COMMANDS:
git tag
To see the list of tags
git tag tagname
To create normal tag
git tag -a -m "message" tagname
To create annotated tag
git tag -d tagname
To delete a tag
git show tagname
To see the details of the tag
GIT-HUB
GitHub is a web-based platform used for version control.
It simplifies the process of working with other people and makes it easy to collaborate on projects.
Team members can work on files and easily merge their changes in with the master branch of the project.
COMMANDS:
git remote add origin repo-url | link local-repo to central-repo |
git remote -v | used to get the linked repo in github |
git push -u origin branch-name | push the code from local to central |
git push -u origin branch-1 branch-2 | used to push the code to multiple branches |
git push -u origin --all | used to push the code to all branches at a time |
git clone repo-url | used to get the code from central to local |
git pull origin branch | used to get the chanes from central to local |
git fetch branch-name | used to fetch the data from central to local |
git fetch --all | used to fetch the changes from all branches in github |
git merge origin/branch | used to merge the changes from cental to local |
git push -u origin --delete branch-name | used to delete the github branch from local |
git remote rm origin | used to unlink the github-repo |
git remote rename old -link new-link | used to change the repo |
Conclusion
Git is a versatile and powerful tool for version control. Understanding its workflow and commands with real-world scenarios can greatly enhance your development process. Whether you’re working solo or collaborating in a team, Git provides the flexibility and reliability needed for modern software projects.
Explore these commands and features to master Git and streamline your development workflow!