Skip to content

Instantly share code, notes, and snippets.

@mRoca
Last active November 6, 2020 17:10
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mRoca/ecbe2b82d94c7ef440f8 to your computer and use it in GitHub Desktop.
Save mRoca/ecbe2b82d94c7ef440f8 to your computer and use it in GitHub Desktop.
Git flow

Naming convention

Item Name
Release branch release/v2.0.1
Feature branch feature/my_feature_name
Feature branch feature/2234_my_feature_name
Hotfix branch hotfix/my_hotfix_name
Commit Add my news
Tag v2.0.1
Hotfix Tag v2.0.1.1
Work in progress PR [WIP] #1234 My pull request
Ready for review PR [RFR] #1234 My pull request

Commit message

Every good commit should be able to complete the following sentence : When applied, this commit will: {{ YOUR COMMIT MESSAGE}}

For example:
– When applied this commit will Update README file
– When applied this commit will Add validation for GET /user/:id API call
– When applied this commit will Revert commit 12345

Basics

# Clone the repo from the remote
git clone <remote_repo>

# Refresh all branches from remote
git fetch --all --verbose --prune
git fetch -vap

# List branches
git branch -av

# List commits for the current branch (see below "git lg" for a better command line)
git log

# List commits for all branches (see below "git lgall" for a better command line)
git log --all --graph

# Switch to a branch
git checkout my_branch

# Get current branch changes from remote
git pull --rebase origin my_branch

# Choose the right branch
# Look the issue target version, or ask to your lead dev

# Create a new branch from the current branch
git checkout -b my_new_branch

# Create a new branch from another branch
git checkout -b my_new_branch the_old_branch

# Add added or updated files to the next comit
git add .
git add my/file

# Track and commit all updated files
git commit -a

# Commit after changes
# If the commit refers to an existing issue, put the issue #ID at the beggining of the message :
git commit -am "#1234 Add the git flow documentation"

# Commit new changes in the last commit, or change the last commit message
git commit --amend -a
git commit --amend -am "The new commit message"

# Send your branch to the remote
git push origin my_branch

# Squash X commits into a single one
git rebase -i HEAD~X
# You editor will open itself, then replace the words "pick" by "squash" on each line to squash, except the first one, save and quit.

# Delete a local branch
git branch -d my_branch

# Delete a remote branch
git push origin :my_branch

# ...
git help

gitconfig

You can add some git aliases and configure some options by editing your .gitconfig file :

[user]
    name = yourName
    email = yourName@gmail.com
[core]
    excludesfile = /home/yourName/.gitignore
[push]
    default = simple
[alias]
    lg = log --abbrev-commit --decorate --date=relative --color --graph --pretty=tformat:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ar)%Creset'
    lgall = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
    c = commit
    co = checkout
    st = status
    f = fetch
    br = branch
    a = add
    r = rebase
    rere = rebase -i HEAD~2
    rerere = rebase -i HEAD~3
[rerere]
    enabled = true

With this .gitconfig file, you can add a .gitignore file in your home and add all your IDE hidden directories (.idea, .sublime, ...).

Somes aliases are added with this file :

git co master # git checkout master
git rere # git rebase -i HEAD~2
git rerere # git rebase -i HEAD~3
git lg # git log
git lgall # git log --all
git a, git st, git br, ...

Workflow - Howto

I want to switch to a release branch

git checkout release/v2.0.1

I want to switch to a tag

git checkout v2.0.1

I want to work on an issue / feature

git checkout -b feature/2234_my_feature_name release/v2.0.1

I want to get all new commit from a release branch in my feature branch

git checkout feature/2234_my_feature_name
git rebase release/v2.0.1
git push -f feature/2234_my_feature_name
# Or git rebase release/v2.0.1 feature/2234_my_feature_name

I want to merge my two last feature commits in a single commit to clean the history and make future merges easier

git rebase -i HEAD~2
git push -f

I want to get only one commit from another branch in the current branch

git cherry-pick 28f0f05

I want to merge my feature with code review and PR

git checkout feature/2234_my_feature_name
git rebase release/v2.0.1
git push -f origin feature/2234_my_feature_name

Merge with Gitlab

Go to Gitlab, then create a Merge Request from your feature branch, to the target release branch. Put a label at the beginning of the name : [WIP] or [RFR]. Wait for the +1 comment from another team member, then use the Accept merge request button, checking Delete the merged branch checkbox

Merge with Github

Go to Github, then create a Pull Request from your feature branch in your forked project, to the target release branch in the parent project. Put a label at the beginning of the name : [WIP] or [RFR]. Wait for the +1 comment from another team member, then use the Merge PR button, then delete the merged branch

I want to merge my feature in a release branch

Caution : this method is without code review (and PR / MR), normally not used.

git checkout release/v2.0.1 # Switch to the release branch
git pull --rebase orgin release/v2.0.1 # Get all new commits on the release branch, or git pull --ff-only orgin release/v2.0.1
git merge feature/2234_my_feature_name # Merge the feature
git push origin release/v2.0.1 # Send the completed feature branch
git branch -d feature/2234_my_feature_name # Delete the local feature branch
git push origin :feature/2234_my_feature_name # Delete the remote feature branch

I want to create a new release branch

git checkout release/v2.0.1
git checkout -b release/v2.0.2
# Or git checkout -b release/v2.0.2 release/v2.0.1

I want to merge a release branch in master

git checkout master # Switch to master
git merge release/v2.0.1 # Merge the release branch into master
git push origin master # Send master
git tag -a v2.0.1 -m 'version 2.0.1' # Create a new tag
git push origin v2.0.1 # Send the new tag

I want to create a new tag

git checkout master
git tag -a v2.0.1 -m 'version 2.0.1'
git push origin v2.0.1

I want to delete an old release branch (a new one has been merged in master)

git branch -d release/v2.0.1
git push origin :release/v2.0.1

I want to do a hotfix

git checkout -b hotfix/fix_some_things release/v2.0.1
# Work & commit ...
git checkout release/v2.0.1
git merge hotfix/fix_some_things
git checkout master
git merge release/v2.0.1
git tag -a v2.0.1.1 -m 'version 2.0.1.1 - hotfix'
git branch -d hotfix/fix_some_things
# Merge the hotfix in working branches
git co release/v2.0.2
git merge release/v2.0.1

Refs

http://csarrazi-slides.github.io/git-for-teams

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment