Skip to content

Instantly share code, notes, and snippets.

@chris2cant
Last active November 6, 2019 13:46
Show Gist options
  • Save chris2cant/da54e70f72c4036132ba73ca85e4cbf5 to your computer and use it in GitHub Desktop.
Save chris2cant/da54e70f72c4036132ba73ca85e4cbf5 to your computer and use it in GitHub Desktop.

Git

I created this guide to help everyone to execute git command step by step. This guide follows the oneflow method. Everything in this guide merge my experience, articles and feedbacks.

Table of Contents

Configure your user

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

Display your config

git config --list
# Alias for a pretty git log
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# Execute the alias of the pretty git log
git lg

Generate an SSH keys

Generate an SSH key with passphrase. Set a Passphrase is strongly recommended !

# -t : Type [dsa | ecdsa | ed25519 | rsa]
# -b : Bits
# -C : Comment (at the end of id_rsa.pub)
# PLEASE ! Replace "johndoe@example.com" with your email address
ssh-keygen -t rsa -b 4096 -C "johndoe@example.com"

A list of git tools :

SemVer is the versioning method I follow for my projects.

X (major release) : MAJOR version when you make incompatible API changes.
Y (feature)       : MINOR version when you add functionality in a backwards-compatible manner.
Z (hotfix)        : PATCH version when you make backwards-compatible bug fixes.

On a git project

X : Major release
Y : A new feature
Z : A hotfix

Go to semver.org for more informations

This is an example from a bike to an electric motorbike.

# MAJOR - ("incompatible API changes")
[1.0.0]  - A Bike

# MINOR - "Backwards-compatible"
[1.1.0]  - Add front light

# MINOR - "Backwards-compatible"
[1.2.0]  - Add back light

# PATCH - "Backwards-compatible bug fixes"
[1.2.1]  - Fix the back light

... 21 features later ...

# MAJOR - New law for electric bike. ("incompatible API changes")
[2.0.0]  - Add electric motor : Electric Bike

... 43 features later ...

# PATCH - "Backwards-compatible bug fixes"
[2.43.23] - Fix the battery issue 

# MAJOR - New law for electric bike with acceleration handle  ("incompatible API changes")
[3.0.0]  - Remove pedals - Add acceleration handle - Electric motorbike

Init

# Init a git repository
git init
# Create a feature branch
# -b : New branch
git checkout -b feature/my-feature
# Publishing this local branch on the remote server
# After having set upstream you can use only "git push" instead "git push origin feature/my-feature"
# -u : Set upstream [-u | --set-upstream]
git push -u origin feature/my-feature
# Start to pull master to be up to date
git checkout master
git pull
# Go to the feature branch
git chekout feature/my-feature
# Pull to be up to date (To be sure)
git pull
# Rebase with master and resolve conflicts
git rebase master
# During conflic add your files with `git add yourfile`
# Use git rebase --continue after file conflict
# Push force (-f) because you rewrite your feature origin 
git push -f origin feature/my-feature

# Create a merge request or merge in command line
Case 1 : Rebase
# Switch to master
git checkout master
# Rebase the feature in master
git rebase feature/my-feature
git push
# Remove the branch locally
git branch -d feature/my-feature
# Remove the branch remotely
git push origin :feature/my-feature
# Create a release branch
git checkout -b release/1.0.0
# Publishing this local branch on the remote server
git push -u origin release/1.0.0
git checkout release/1.0.0
# New version for a major feature X(+1).Y.Z (Example : 1.0.0 -> 2.0.0)
# New version for a minor feature X.Y(+1).Z (Example : 1.0.0 -> 1.1.0)
git tag 1.0.0
git checkout master
git merge release/1.0.0
# Important : --tags push the tag
git push --tags origin master
# Remove the branch locally
git branch -d release/1.0.0
# Remove the branch remotely
git push origin :release/1.0.0
git checkout master
# Create a branch hotfix from the tag 1.0.1
git checkout -b hotfix/1.0.1 1.0.1
# Publishing this local branch on the remote server
git push -u origin hotfix/1.0.1

Hotfix version should increase the z part (Version x.y.z)

git checkout hotfix/1.0.1
# New version for a hotfix X.Y.Z(+1) (Example : 1.0.0 -> 1.0.1)
git tag 1.0.1
git checkout master
# Merge the hotfix in master
git merge hotfix/1.0.1
# Important : --tags push the tag
git push --tags origin master
# Remove the branch locally
git branch -d hotfix/1.0.1
# Remove the branch remotely
git push origin :hotfix/1.0.1

Squash commit is a good practice to keep a clean git log

# Display logs
git lg

In this example we have two commits for the same feature. If we want to keep only one commit message we can squash it in one !

* 4c23828 - (HEAD -> master, origin/master) [mySecondFeature] - Add the second method (3 minutes ago) <Chris>
* efd6cee - [mySecondFeature] - Add the first method (4 minutes ago) <Chris>
* 6987662 - [myFirstFeature] - Add the first feature(12 minutes ago) <Chris>
* 092b49f - Initial commit (22 hours ago) <Chris>

Go to the commit just before "mySecondFeature"

# Rebase 
# -i : Interactive
# Commit hash
git rebase -i 6987662

Your terminal will show you vim with this content

pick efd6cee [mySecondFeature] - Add the first method 
pick 4c23828 [mySecondFeature] - Add the second method

Change pick by squash for the last commit you wand to squash. When you are done, save it !

pick efd6cee [mySecondFeature] - Add the first method 
squash 4c23828 [mySecondFeature] - Add the second method

Your terminal will show you vim with this new content.

# This is a combination of 2 commits.
# This is the 1st commit message:

[mySecondFeature] - Add the first method 

# This is the commit message #1:

[mySecondFeature] - Add the second method

Now you can comment with # the last commit and rename the commit message you want to keep. When you are done, save it !

# This is a combination of 2 commits.
# This is the 1st commit message:

[mySecondFeature] - Add the second feature

# This is the commit message #1:

#[mySecondFeature] - Add the second method

Push your changes

# Push with force because you made a rebase
git push -f origin master

Verification

# Display logs
git lg

Now you have one commit per feature. It's cleaned !

* 4c23828 - (HEAD -> master, origin/master) [mySecondFeature] - Add the second feature (3 minutes ago) <Chris>
* 6987662 - [myFirstFeature] - Add the first feature(12 minutes ago) <Chris>
* 092b49f - Initial commit (22 hours ago) <Chris>

That's it !

# Pull your branch without commit your changes
# 1. Stash your code
# 2. Pull your code
# 3. Stash pop your code
git pull --autostash

πŸ“Œ Pinned articles

Sources

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