Handling development workflows effectively with Git is crucial for smooth collaboration and efficient code management. Let's explore some common scenarios and best practices to navigate them seamlessly. Observing git commands in action can give us a clear view about these commands.

 

Naming Your Branches

Git branches are essential for organizing development tasks and understanding their purpose. Typically, repositories contain three main branches:

  • develop: This branch integrates new updates. Create a new branch from develop when starting a new development task.
  • staging: Receives updates from develop and is often associated with continuous deployment (CD) actions for testing changes on a staging server.
  • master: This branch receives updates from staging and represents the live server.

When you want to make a new development, you always need to create a new branch from the develop branch. A good practice is to create your branch in a group that describes your development better. Some common groups are feature (creating new feature), update (updating existing feature), fix (fixing a bug), hotfix (fixing a bug that requires immediate action) etc.

Let’s say you are adding a feature for customer support’s email. So, you can do following:

git checkout develop  #goes to develop branch
git pull origin develop  #gets latest updates for develop
git checkout -b feature/customer_support_email   #creates a new branch

 

Collaborating on the Same Branch

Collaborative tasks often require multiple team members to work on the same branch simultaneously. Here's how to set up collaborative development:

One team member creates the branch and pushes it to the remote repository.

git checkout -b feature/our_branch
git push --set-upstream origin feature/our_branch

Other team members need to fetch the branch for collaboration. Fetch the branch from the remote repository.

git fetch origin feature/our_branch

This will download the branch as origin/feature/our_branch. You can check that you have that branch with the following command.

git branch -a

If you don’t know the branch name you can first fetch all the remote branches like following:

git fetch

Then you can see the all branch list like before. Or you can run this command:

git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)' --sort=committerdate

This will show all the branch lists with the name of the owner.

After you investigate your partner’s changes you need to get the new changes in your branch.

git pull origin feature/our_branch --rebase

 

Handling Uncommitted Changes

In situations where you need to switch branches without committing changes, Git's stash feature comes in handy.

First you need to save your changes to stash.

git stash # Saves uncommitted changes and removes the changes
git stash save “your stash message” # save with a message

You can monitor changes that you saved.

git stash list    # Shows list of stash
git stash list -p   # Shows what changes the stash hold

You can retrive the changes.

git stash pop                     # gets the latest stash and removes from stash list
git stash pop stash@{1}   # gets a specific stash and removes from stash list

git stash apply stash@{1}  # applies saved changes for specific stash.
git stash drop stash@{1}    # removes specific stash