DEV Community

DevCorner
DevCorner

Posted on

Using Git Version Control in a Company: Step-by-Step Guide

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"
Enter fullscreen mode Exit fullscreen mode

To check the configuration:

git config --list
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If using SSH:

git clone git@github.com:company/project.git
Enter fullscreen mode Exit fullscreen mode

3. Understanding the Main Branches

Most companies follow a branching strategy like Git Flow or Trunk-based development. Common branches include:

  • main or master: 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Or, if working from develop:

git checkout develop
git pull origin develop
git checkout -b feature/new-feature
Enter fullscreen mode Exit fullscreen mode

5. Making Changes and Committing

After making code changes, check the status:

git status
Enter fullscreen mode Exit fullscreen mode

Add changes to staging:

git add .
Enter fullscreen mode Exit fullscreen mode

Commit with a meaningful message:

git commit -m "Implemented new feature XYZ"
Enter fullscreen mode Exit fullscreen mode

6. Pushing Changes to Remote

git push origin feature/new-feature
Enter fullscreen mode Exit fullscreen mode

If pushing for the first time:

git push --set-upstream origin feature/new-feature
Enter fullscreen mode Exit fullscreen mode

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-featuredevelop.
  • 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
Enter fullscreen mode Exit fullscreen mode

Or, if multiple commits:

git commit -m "Fixed review comments"
git push origin feature/new-feature
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To delete the merged branch:

git branch -d feature/new-feature
git push origin --delete feature/new-feature
Enter fullscreen mode Exit fullscreen mode

10. Handling Merge Conflicts

If Git reports conflicts while merging:

git status
Enter fullscreen mode Exit fullscreen mode

Manually resolve conflicts in the affected files, then:

git add resolved-file.js
git commit -m "Resolved merge conflicts"
git push origin develop
Enter fullscreen mode Exit fullscreen mode

11. Working with Bug Fixes

For fixing bugs:

git checkout -b bugfix/fix-issue-123 develop
Enter fullscreen mode Exit fullscreen mode

Follow the commit, push, and PR process similar to feature branches.

For urgent production fixes:

git checkout -b hotfix/urgent-fix main
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Or rebase:

git rebase develop
Enter fullscreen mode Exit fullscreen mode

13. Stashing Unfinished Work

If you need to switch branches but have uncommitted changes:

git stash
git checkout develop
Enter fullscreen mode Exit fullscreen mode

To restore changes:

git stash pop
Enter fullscreen mode Exit fullscreen mode

14. Reverting Changes

Undo local changes before commit:

git checkout -- filename.js
Enter fullscreen mode Exit fullscreen mode

Undo last commit but keep changes:

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

Revert a pushed commit:

git revert <commit-hash>
git push origin develop
Enter fullscreen mode Exit fullscreen mode

15. Deleting a Branch

Locally:

git branch -d feature/old-feature
Enter fullscreen mode Exit fullscreen mode

Remotely:

git push origin --delete feature/old-feature
Enter fullscreen mode Exit fullscreen mode

16. Resetting a Branch to a Previous Commit

If needed:

git reset --hard <commit-hash>
git push --force
Enter fullscreen mode Exit fullscreen mode

17. Checking Git Log & History

git log --oneline --graph --decorate --all
Enter fullscreen mode Exit fullscreen mode

View a file’s change history:

git blame filename.js
Enter fullscreen mode Exit fullscreen mode

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)