Git is widely used in companies to manage source code efficiently. This guide explains how an employee typically uses Git in a company with various scenarios and Git commands.
1. Setting Up Git
Before working with Git, configure your global settings:
git config --global user.name "Your Name"
git config --global user.email "your.email@company.com"
To check the configuration:
git config --list
2. Cloning the Company Repository
Employees typically clone the project repository from a remote Git server (e.g., GitHub, GitLab, Bitbucket).
git clone https://github.com/company/project.git
cd project
If using SSH:
git clone git@github.com:company/project.git
3. Understanding the Main Branches
Most companies follow a branching strategy like Git Flow or Trunk-based development. Common branches include:
-
main
ormaster
: The stable production branch -
develop
: The integration branch for upcoming releases -
feature/*
: For new features -
bugfix/*
: For bug fixes -
hotfix/*
: For urgent production fixes
To view branches:
git branch -a
4. Creating a Feature Branch
Employees should not commit directly to main
or develop
. Instead, create a new feature branch:
git checkout -b feature/new-feature
Or, if working from develop
:
git checkout develop
git pull origin develop
git checkout -b feature/new-feature
5. Making Changes and Committing
After making code changes, check the status:
git status
Add changes to staging:
git add .
Commit with a meaningful message:
git commit -m "Implemented new feature XYZ"
6. Pushing Changes to Remote
git push origin feature/new-feature
If pushing for the first time:
git push --set-upstream origin feature/new-feature
7. Creating a Pull Request (PR)
Once the feature is completed:
- Go to the Git platform (GitHub/GitLab/Bitbucket).
- Open a Pull Request (PR) from
feature/new-feature
→develop
. - Add reviewers and wait for approval.
8. Handling Code Reviews and Changes
If requested changes are needed:
git checkout feature/new-feature
git pull origin feature/new-feature
git add .
git commit --amend -m "Updated feature as per review"
git push origin feature/new-feature --force
Or, if multiple commits:
git commit -m "Fixed review comments"
git push origin feature/new-feature
9. Merging to Develop
Once approved, merge the branch:
git checkout develop
git pull origin develop
git merge feature/new-feature
git push origin develop
To delete the merged branch:
git branch -d feature/new-feature
git push origin --delete feature/new-feature
10. Handling Merge Conflicts
If Git reports conflicts while merging:
git status
Manually resolve conflicts in the affected files, then:
git add resolved-file.js
git commit -m "Resolved merge conflicts"
git push origin develop
11. Working with Bug Fixes
For fixing bugs:
git checkout -b bugfix/fix-issue-123 develop
Follow the commit, push, and PR process similar to feature branches.
For urgent production fixes:
git checkout -b hotfix/urgent-fix main
12. Updating Your Local Branch with Remote Changes
Keeping branches updated to avoid conflicts:
git checkout develop
git pull origin develop
git checkout feature/new-feature
git merge develop
Or rebase:
git rebase develop
13. Stashing Unfinished Work
If you need to switch branches but have uncommitted changes:
git stash
git checkout develop
To restore changes:
git stash pop
14. Reverting Changes
Undo local changes before commit:
git checkout -- filename.js
Undo last commit but keep changes:
git reset --soft HEAD~1
Revert a pushed commit:
git revert <commit-hash>
git push origin develop
15. Deleting a Branch
Locally:
git branch -d feature/old-feature
Remotely:
git push origin --delete feature/old-feature
16. Resetting a Branch to a Previous Commit
If needed:
git reset --hard <commit-hash>
git push --force
17. Checking Git Log & History
git log --oneline --graph --decorate --all
View a file’s change history:
git blame filename.js
18. Best Practices for Git in Companies
✔ Keep commit messages meaningful and concise.
✔ Pull the latest changes before starting new work.
✔ Avoid force push (git push --force
) unless necessary.
✔ Use .gitignore
to exclude unnecessary files.
✔ Regularly clean up old branches.
This covers all important Git workflows in a corporate environment. Let me know if you need any modifications!
Top comments (0)