Table of Contents
Git Version Control for Team Development
When working with a team, keep your local repository synchronized with the remote repository before editing, use a branch for your own changes, and push your work back to the server when it is ready for review or integration.
Pull before editing
Before starting work, update your local copy from the remote repository:
git pull
This helps avoid editing on top of old code. If your team uses a specific main branch, switch to that branch first and then pull:
git checkout master
git pull origin master
Some repositories now use main instead of master; check the branch name used by your project.
View your current branch
To see all local branches and identify the branch you are currently on, run:
git branch
The current branch is marked with *.
Create and switch to a new branch
Create a branch for your task:
git branch branchname
git checkout branchname
The same operation can also be done in one command:
git checkout -b branchname
Use a clear branch name, such as fix-login-error or feature-user-profile, so other team members can understand the purpose of the work.
Commit your changes
After editing files, check what changed:
git status
git diff
Add the files you want to commit:
git add filename
Then create a commit:
git commit -m "Describe the change"
A good commit message should briefly explain what changed and why.
Merge your branch
When the branch is ready to be merged back into the main development branch, switch to the target branch first:
git checkout master
git pull origin master
Then merge your work branch:
git merge branchname
If there are conflicts, Git will show the files that need to be fixed. Open those files, resolve the conflict markers, test the result, then commit the merge.
Delete the finished branch
After the branch has been merged, delete the local branch:
git branch -d branchname
If the branch has not been merged, Git will warn you instead of deleting it.
Push your code to the server
To push a branch to the remote repository:
git push origin branchname
If you merged into master, push the updated master branch:
git push origin master
Again, if your repository uses main, replace master with main.
Basic team workflow
A simple team workflow is:
git checkout master
git pull origin master
git checkout -b branchname
# edit files
git status
git add filename
git commit -m "Describe the change"
git checkout master
git pull origin master
git merge branchname
git push origin master
git branch -d branchname
For larger teams, it is usually better to push your branch and open a pull request instead of merging directly into the main branch. This allows code review, automated tests, and discussion before the change is accepted.
