Tag Archives: git

Script: delete git branch and switch back to master

I call this script git-done

current_branch=$(git rev-parse --abbrev-ref HEAD)

if [ $current_branch == "master" ]
then
  echo "You're already on master"
  exit 0
else
  echo "switching to master"
  git checkout master
  echo "deleting $current_branch..."
  git-delete-branch $current_branch
fi
Tagged

Git stash and cleanup

Random git stash -related commands and processes to follow.

To stash your changes

git stash

To list your stashes

git stash list

To apply and pop off top-stash from list. Note, that it will only pop off the top if applying the stash does not result in merge conflicts. Otherwise, it’s just an git stash apply

git stash pop

To drop a stash (either because you only apply or you unsuccessfully pop your stash

git stash drop

If you have applied a stash with merge conflicts and want to get rid of it

# to resolve the merge conflicts, moving all files to staged state
git add . 

# to unstage all and move it back into untracked state
git restore --staged . 

# to remove all untracked state
git checkout . 

# to clean up any new (f)iles and (d)irectories
git clean -fd

Tagged

Developing with git in a team environment

I’m posting a doc that my coworker (Neha K.) gave me permission to repost. It’s the set of instructions my company follows to manage our source control and development environment.

The basic idea is that we always branch to start development on a new feature or work on a bug.

Git Development Process

This is the process we follow for development with Git. Its based on Vincent Dreissen’s “A successful Git branching model”, please read that for a more detailed understanding of the process but keep in mind it has been modified a little to suit our needs.

git

Main branches

2 main long running active branches are:

  • develop – branch against which all the development is done.

  • master – branch used for all the releases

Naming convention

  • Feature branch: feature-{issueId}

  • Hotfix branch: hotfix-{issueId}

  • Release tags: v{version-number}

Feature development

Its recommended to create a feature branch off of develop for any feature development. If the feature is small enough, it can be done directly on develop – thats at the discretion of the developer. But if the feature is for a future release and not the very next one, then a feature branch should be used. Here are steps for developing on a separate feature branch.

Create the feature branch

Checkout develop branch

git checkout develop

Get the latest from develop

git pull --rebase

Create the feature branch off of develop and switch to the new branch

git checkout -b feature-10 develop

Push the feature branch to central repo

git push -u origin feature-10

Commit/push to the feature branch as frequently as you like.

Merge develop into the feature branch

If its a branch that spans over multiple days, its advisable to merge develop into the feature branch periodically so that you can make sure work pushed on develop doesnt affect your feature and its also better than doing one big merge into develop on feature completion.

Checkout develop branch

git checkout develop

Get the latest from develop by rebasing (to ensure no no commits created from merge)

git pull --rebase

Checkout feature branch

git checkout feature-10

Rebase from develop. This is preferable to merge (git merge –no-ff develop) because it doesn’t create additional commits.

git rebase develop

Push the merged code

git push origin feature-10

End the feature branch

Once the feature development is complete, merge it back to develop and delete it. Getting the latest from the feature branch is relevant if multiple developers worked on the feature branch.

Checkout feature branch

git checkout feature-10

Get the latest from feature-10

git pull

Checkout develop branch

git checkout develop

Get the latest from develop

git pull --rebase

Merge feature branch in

git merge --no-ff feature-10

Push the merged code

git push origin develop

Delete local feature branch

git branch -D feature-10

Delete remote feature branch

git push origin --delete feature-10

Releases

Releases are done on master branch. When we are ready to release a new version, make sure that all the features going into that release are merged into develop and then merge develop into master.

Merge develop into the master branch

Checkout develop branch

git checkout develop

Get the latest from develop

git pull

Checkout master branch

git checkout master

Get the latest from master

git pull

Merge develop in

git merge --no-ff develop

Push the merged code

git push origin master

Testing is done against master build and all bugfixes are done on master. When the release is ready to go to production, master is tagged and the bugfixes are merged back to develop. Bugfixes can also be continuously merged back into develop on an as-needed basis, rather than waiting for all of them to be done. If its a more involved bugfix, one might want to create a separate branch for it if he/she does not want rest of the release testing to get affected. Instructions below do not show a different bugfix branch.

Tag the release

Checkout the master branch

git checkout master

Get the latest from master

git pull

Tag master with the release version

git tag -a v1.0

Push the tag

git push origin tag v1.0

Hotfixes

Hotfixes are done on a separate hotfix branch, created off of master branch.

Create the hotfix branch

Checkout master branch

git checkout master

Get the latest from master

git pull

Create the hotfix branch off of master and switch to the new branch

git checkout -b hotfix-12 master

Push the hotfix branch to central repo

git push -u origin hotfix-12

End the hotfix branch

Once the hotfix(es) are done, merge it back to master and develop and delete it. Instead of merging hotfix into develop, master can be merged into develop too after hotfix is merged into master. If latter is being done, replace hotfix branch name with master in the instructions for merging hotfix in develop. Getting the latest from the hotfix branch is relevant if multiple developers worked on the hotfix branch.

Checkout hotfix branch

git checkout hotfix-12

Get the latest from hotfix-12

git pull

Checkout master branch

git checkout master

Get the latest from master

git pull

Merge hotfix branch in

git merge --no-ff hotfix-12

Push the merged code

git push origin master

Checkout develop branch

git checkout develop

Get the latest from develop

git pull

Merge hotfix branch in

git merge --no-ff hotfix-12

Push the merged code

git push origin develop

Delete local hotfix branch

git branch -d hotfix-12

Delete remote hotfix branch

git push origin --delete hotfix-12

Notes

  • Clean up old branches  –  To clean up old branches locally, that have been deleted from the remote repo, run this command “git remote prune origin”

  • Merge conflicts  –  If a merge results in conflicts, you can resolve them manually or run “git mergetool”.  This command will help resolve conflicts using a visual diff tool that is configured for git.

Tagged ,