GIT Merge - Best Practices
Almost every developer's work ends in GIT, which is a powerful version control system in the present world. With a development team sharing one Git repository to consolidate their project work, team members often feel the need to merge their work. Hence, it is necessary to understand and learn the best practices of GIT merge functionality.
Git merge allows developers to combine two different code branches. Lets look into the steps to perform git merge in an optimum way.
1. Go to the default branch in your local git repo and pull the latest changes. In most cases it is either the master or develop branch.
2. Before working on a feature or defect, create a feature branch and navigate(checkout) to the branch. We can achieve this using a single command.
$git checkout -b <branch_name>
2. Once the feature development is complete in the new local branch, add the files and commit the changes.
$git add <file_name>
$git commit -m "comments"
It is important to commit or stash your changes before checking out another branch or the changes will be lost.
3. Checkout the master/develop branch, pull the latest changes pushed by other team members.
$git checkout <master_branch>
4. If there are no changes to the master/develop branch, checkout local feature branch and push it to remote.
In case the master has changes, they need to be merged with the feature branch. Checkout the local feature branch and merge the master. This has to be done from the feature branch. So, make sure to checkout the local feature branch and run the below merge command.
$git checkout <feature_branch>
$git merge develop
There are times when files couldn't be merged automatically. This occurs when there were changes made to the same files. Git informs us to merge them manually and flags the necessary files. Build the workspace post manual resolution, and commit the changes.
6. Push the local branch to remote.
$git push --set-upstream origin <feature_branch>
Note the '--set-upstream' is used to create the branch in remote and push the changes along. If the feature branch already exists in the remote, just the 'git push' command will do.
Click here for a list of frequently used Git commands.