Git

Origins and How to use it

Quentin LAFFONT

Full Stack JS Developer

 

https://qlaffont.com

           

            @qlaffont

Origins

Created by Linux Torvalds in 2005

 

For his design criterion, he specified that patching should take no more than three seconds, and added three more points:

  • Take Concurrent Versions System (CVS) as an example of what not to do; if in doubt, make the exact opposite decision.
  • Support a distributed, BitKeeper-like workflow.
  • Include very strong safeguards against corruption, either accidental or malicious.

Wikipedia

What is Git ?

Tools to manage version of codes

 

Lexical to know:

  • Tree -> Your repository
  • Branch -> One version of your code
  • Commit -> State of your code

 

For one tree, each branch will have many commits.

Git Illustration

Convention

In git, we have mostly two branches :

 

  • Master - Code used in production environment

 

  • Develop - Code used in pre-production / development environment

 

You can create others branches if you need

Origin Concept

When you work on git, you can add a remote server to push your codes. Many times is Github or Gitlab.

 

Your code hosted in your computer = Local

Your code hosted in Github/Gitlab Server = Origin

Warning

GIT

Gitlab

Github

BitBucket

etc.

Git is a software.

Github, Gitlab, etc are Git Server providers.

Warning 2

Git don't required a server.

 

You can use git only for you without Github, etc.

Commands - Branch

Select a branch

 

You can select a branch by using this command

 

> git checkout my-branch

Commands - Branch

Create a branch

 

You can create a branch by using this command

 

> git checkout -b my-branch

Commands - Branch

Delete a branch

 

You can delete a branch by using this command

 

> git checkout -d my-branch

Commands - Branch (Origin)

Fetch all remote branches

 

> git fetch --all

Commands - Commits

1. Add files

 

You can add file to a commit by using this command

 

> git add --a (Add all files)

> git add myfile.txt (Add only one file)

Commands - Commits

2. Write commit message

 

Now you can add commit message :

 

> git commit -m "this is my commit message"

> git commit (Will open vim and you will add commit message)

 

 

Recommendation: Use Semantic Commit

Commands - Commits (Origin)

1. Fetch modification in remote

 

If you local branch is different with your origin branch, you need to sync your local codes :

 

> git pull (Will fetch commits in origin and added it to your local)

Commands - Commits (Origin)

(2.) Resolve conflicts

 

If your local modifications are in conflict with remote commits, you will need to resolve conflicts.

 

Tutorial by Github : https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/resolving-a-merge-conflict-using-the-command-line

Commands - Commits (Origin)

3. Push your local commits to server

 

Now that you have created yout commit and you have sync commits with remote, you can push your commits :

> git push origin/my-remote-branch

Commands - Status

To check all modifications before commit :


> git status

Commands - History

If you want to see every commit done in your current branch, you can use:


> git log

Git Commands

Advanced part

Commands - Reset

If you have created some commit in local and you want to remove previous commit :

> git reset --hard COMMITHASH 

 

 

COMMITHASH : Hash given when you see git history (git log)

Commands - Reset (origin)

If you have done a revert in local branch, you need to force push to be sure that your local and your remote are sync :

> git push -f origin/my-branch
 

Commands - Revert commit

If you have created one commit and you want to revert it :

> git revert COMMITHASH 

 

COMMITHASH : Hash given when you see git history (git log)

 

Information :
When you do a revert, the initial commit will stay in history and a new commit will be added to revert your changes.

 

Commands - Rebase commits

If you have push some commits in master, you have push some other commits in develop, you want to sync develop with master.

> git checkout master

   git pull

   git checkout develop

   git rebase master

Commands - Squash commits

If you have created multiple commit, and you want to merge them to one commit you can squash it :

1 - type git rebase -i COMMIT HASH OR git rebase -i HEAD~NB

 (NB is equal of commits number, ex: 3 last commit -> HEAD~3)

2 - You see all your commits in VIM

3- Edit

  • Keep pick at the beginning of the first commit you want to keep

  • Replace pick by f (or type :%s/pick/f and type enter) at the beginning of the commits you want to squash

  • type Echap (to exit insert mode) then :wq to exit VIM

Commands - Diffs

If you want to apply your commit later, you can create a patch file.

 

> git diff from-commit to-commit > output-file

 

later

 

> git apply output-file

Commands - Clean delete branches

Add git gone command :

 

git config --global alias.gone "! git fetch -p && git for-each-ref --format '%(refname:short) %(upstream:track)' | awk '\$2 == \"[gone]\" {print \$1}' | xargs -r git branch -D"

 

Type in your repo :

- git gone

Bonus: Git Flow

Git flow is a tools for git to add some conventions in naming branch and others things.

 

More informations: https://github.com/nvie/gitflow

Git - Origins and How to use it

By Quentin LAFFONT

Git - Origins and How to use it

Git - Origins and How to use it

  • 848