项目作者: ondrejsika

项目描述 :
Skoleni Gitu
高级语言: Shell
项目地址: git://github.com/ondrejsika/git-training.git
创建时间: 2019-06-28T04:53:55Z
项目社区:https://github.com/ondrejsika/git-training

开源协议:

下载


Ondrej Sika (sika.io) | ondrej@sika.io | course ->

Git Training

Install Git

Download installer from https://git-scm.com or use package manager.

Mac

  1. brew install git

Linux

  1. apt install git

Windows

https://git-scm.com/downloads/win

  1. winget install -e --id Git.Git
  1. choco install git

Editor

I prefer VS Code, you can download it here: https://code.visualstudio.com/download

Or download it using package manager:

Mac

  1. brew cask install visual-studio-code

Windows

  1. choco install vscode

Install slu

SikaLabs Utils

Mac

  1. brew install sikalabs/tap/slu

Linux

  1. curl -fsSL https://raw.githubusercontent.com/sikalabs/slu/master/install.sh | sudo sh

Windows

  1. scoop install https://raw.githubusercontent.com/sikalabs/scoop-bucket/master/slu.json

Course

Real version control by Elon Musk

About Me - Ondrej Sika

Freelance DevOps Engineer, Consultant & Lecturer

  • Complete DevOps Pipeline
  • Open Source / Linux Stack
  • Cloud & On-Premise
  • Technologies: Git, Gitlab, Gitlab CI, Docker, Kubernetes, Terraform, Prometheus, ELK / EFK, Rancher, Proxmox, DigitalOcean, AWS

Star, Create Issues, Fork, and Contribute

Feel free to star this repository or fork it.

If you found bug, create issue or pull request.

Also feel free to propose improvements by creating issues.

Chat

For sharing links & “secrets”.

Slack & Newsletter

Join my Slack for future discussion & help.

Subscribe to my Newsletter for future DevOps topics.

Overview of Git Storage

detlas

snapshots

Basic Configuration

I prefer global configuration (using --global) stored in your home directory applied to all repositories.

You can configure just one repo, you can call git config from you repository with flag --local.

  1. git config --global user.name "Ondrej Sika"
  2. git config --global user.email "ondrej@ondrejsika.com"

Rebase workflow (if you want to use rebase workflow)

WARNING: Apply only if you want to use rebase workflow!

  1. git config --global pull.ff only
  2. git config --global merge.ff only
  3. git config --global pull.rebase true

Init Default Branch

  1. git config --global init.defaultBranch main
  1. git config --global init.defaultBranch master

GPG sign tags by default

  1. git config --global tag.gpgSign true

or for example disable only for local repo

  1. git config --local tag.gpgSign false

Git Editor

Git use by default Vim or editor from EDITOR environment variable. If you want to use different editor, you can configure it.

  1. git config --global core.editor emacs

You can use GUI editors like VS Code too:

  1. git config --global core.editor "code --wait"

See Associating text editors with Git on Github Help to use your editor on your platform.

Sources:

Git PS1

If you want to see your branch in terminal prompt you have to use git-prompt.sh.

It works on Unix (ZSH & Bash). If you use Windows, it works by default in Git Bash and there is no way how add it into CMD or PowerShell.

Install On Unix:

  1. wget https://github.com/git/git/raw/master/contrib/completion/git-prompt.sh
  2. mv git-prompt.sh ~/.git-prompt.sh
  3. echo ". ~/.git-prompt.sh " >> ~/.bashrc

You have to add __git_ps1 to your PS1 variable.

Bash Example:

  1. export PS1='\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1)\$ '

Save it to .bashrc:

  1. echo "export PS1='\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1)\$ '" >> ~/.bashrc

Aliases

You can create own git aliases:

  1. git config --global alias.<alias> <command>

Examle:

  1. git config --global alias.co checkout
  2. git config --global alias.br branch
  3. git config --global alias.ci commit
  4. git config --global alias.st status

Usage of aliases is git co for git checkout, git ci for git commit, …

Those aliases work on every platform (event Windows).

My Git Aliases

From ondrejsika/dotfiles

  1. alias st='git status'
  2. alias sta='git status --untracked-files=all'
  3. alias di='git diff'
  4. alias dis='git diff --staged'
  5. alias dit='git diff | tig'
  6. alias dist='git diff --staged | tig'
  7. alias ci='git commit'
  8. alias co='git checkout'
  9. alias br='git branch'
  10. alias ad='git add'
  11. alias fa='git fetch --all --prune'
  12. alias ga='gitk --all'
  13. alias glo='git log --oneline '

I also use alias completion using complete alias, eg: complete -F _complete_alias st for st alias.

slu git

  1. slu git url open

With alias alias guo="slu git url open"

  1. guo
  1. slu git url get
  1. slu git use-ssh

VS Code and Git

Demo Gitlab

Gitlab Upgrade Path

https://gitlab-com.gitlab.io/support/toolbox/upgrade-path/

New Repository

Create on Github (eg.: example-repository) and clone it.

  1. git clone git@github.com:ondrejsika/example-repository.git
  2. cd example-repository

or create it locally

  1. mkdir example-repository
  2. cd example-repository
  3. git init

Gitignore

File .gitignore defines files ignored by Git. Those files doesn’t exist for Git. This is a simple example for Next.js project.

  1. # Mac
  2. .DS_Store
  3. # Editor
  4. .vscode
  5. .idea
  6. *.swp
  7. *.swo
  8. *~
  9. # Generic
  10. *.log
  11. *.backup
  12. *.local.sh
  13. *.local.yml
  14. *.local.yaml
  15. *.local.json
  16. *.local.txt
  17. # NodeJS
  18. node_modules
  19. # NodeJS
  20. .next
  21. out

This example gitingore has been generated by slu file-templates gitignore --node --nextjs

Github Gitignore Templates

http://github.com/github/gitignore

EditorConfig

File .editorconfig define editors behavior, like spaces vs tabs or tab size, for example.

  1. root = true
  2. [*]
  3. indent_style = space
  4. indent_size = 2
  5. charset = utf-8
  6. trim_trailing_whitespace = true
  7. insert_final_newline = true
  8. end_of_line = lf
  9. max_line_length = off

slu file-templates: gitignore & editorconfig

Gitignore

  1. slu file-templates gitignore
  1. slu ft gi
  1. slu file-templates gitignore --terraform --node
  1. slu ft gi --terraform --node

Editorconfig

  1. slu file-templates editorconfig
  1. slu ft ec
  1. slu file-templates editorconfig --go --python
  1. slu ft ec --go --python

Basic Commands

git status

Show status of repository. See which files are edited or want to be committed.

  1. git status

Show all untracked files in status

  1. git status --untracked-files=all

git add

Add file to next commit

  1. git add <path>

Examples

  1. git add index.html
  2. git add .

Partial git add

You can use -p to switch into interactive mode and select part of changed file, which you want to commit.

  1. git add -p <path>

git diff for new changes

You can see changes before git add or git commit.

See new changes in files managed by Git (not in new files):

  1. git diff

If you want to see staged changes (added, prepared for commit), you have to use:

  1. git diff --staged

Unstage

Remove changes from next commit

  1. # Unstage all changes
  2. git reset
  3. # Unstage file
  4. git reset -- <path>

git commit

Save prepared changes to repository

Create commit from all staged changes

  1. git commit

Create commit form all changes (not new files)

  1. git commit -a

Commit one file (not new files)

  1. git commit <file>

Specify message in parameter insted of open vim

  1. git commit -m "<message>"

Combination of -a -m params

  1. git commit -am "<message>"

Update latest commit

  1. git commit --amend

Conventional Commits

https://www.conventionalcommits.org

  1. <type>[(<scope>)]: <message>

Types:

  • feat
  • fix
  • chore

My aditional types:

  • init
  • VERSION
  • content
  • ci
  • docs
  • refactor
  • cleanup

My format with prefix:

  1. [[prefix]] <type>[(<scope>)]: <message>

Prefixes:

  • [auto]

git log

Show history of commits

  1. git log
  2. git log --oneline
  3. git log --oneline --graph --all

Browsing history

Tig

Simple terminal history browser for Git

Install

Mac

  1. brew install tig

Linux

  1. apt install tig
Usage
  1. # only actual branch
  2. tig
  3. # all branches
  4. tig --all

Gitk

Graphic commit log. Distributed with Git.

  1. # only actual branch
  2. gitk
  3. # all branch
  4. gitk --all

Remote Repository (Github, Gitlab)

If you have clonned repository, git clone has added configuration of repository.

Check it by:

  1. git remote -v

and you will see:

  1. ondrej@sika-macbookpro:~/example-repository (master)$ git remote -v
  2. origin git@github.com:ondrejsika/example-repository.git (fetch)
  3. origin git@github.com:ondrejsika/example-repository.git (push)

If you’ve created repository by git init you see nothing.

Add Remote Repository

To add remote repository, you have to use:

  1. git remote add <name> <url>

For example:

  1. git remote add origin git@github.com:ondrejsika/example-repository.git

Now you can push & share your code with collaborators. Check git remote -v.

Rename & Remove Remote Repository

If you want to rename remote repository, use:

  1. git remote rename <name> <new name>

If you want delete remote, use:

  1. git remote remove <name>

git push

Push your commits to remote repository (Github).

  1. # Push new branch to repository
  2. git push <remote> <branch> -u
  3. # Push commit
  4. git push

git pull

Pull new commits from remote repository (Github).

  1. git pull

Working with Branches

Stash

Git stash is used for temporarily postpone your changes to make your working directory clean.

That’s required by some Git commands like git rebase, … or sometimes for git checkout, git cherry-pick, …

If you want to stash changes, use:

  1. git stash

And check status using git status.

If you want to see, which files are stashed, use:

  1. git stash show

If you want to see patch, add -p:

  1. git stash show -p

If you want to apply stashed changes and remove stash, use:

  1. git stash pop

And check git diff and git stash show.

If you have multiple stashes you work only with the latest.

List all stashes:

  1. git stash list

If you want to specify other stash you can use stash@{0}. For example:

  1. git stash show stash@{1}
  2. git stash show -p stash@{1}

More about stash in offical documentation - https://git-scm.com/docs/git-stash

List Branches

  1. # Show local branches
  2. git branch
  3. # Show all branches (with the branches of remote repository - on Github)
  4. git branch --all

Switch vs Checkout

Switch is new command which implements part of checkout functionality.

See: https://stackoverflow.com/a/70454786/5281724

Create a Branch

Create branch (and dont switch to it)

  1. git branch <new_branch> [<branch_from>]

Switch branch

  1. git checkout <branch>
  1. git switch <branch>

Create branch and switch to it

  1. git checkout -b <new_branch> [<branch_from>]
  1. git switch -c <new_branch> [<branch_from>]

Switch Branch

  1. git checkout <branch>
  1. git switch <branch>

Push & Pull Branch

  1. # Push commits to remote repository (Github)
  2. git push <remote> <branch> -u
  3. # Pull new commits to my branch
  4. git pull

Merging Branches

You can merge branches locally or on Github / Gitlab using Pull / Merge Requests.

Merge hell

Rebase

See my czech article about rebase: https://ondrej-sika.cz/git/rebase/

Git Reset

Reset HEAD (current brach) to specific state.

Set HEAD to specific state, but don’t change files in working directory.

  1. git reset <commit>

If you want also reset files, use --hard:

  1. git reset --hard <commit>

Remove Last Commit

For example, you want to remove last commit but want to keep changes:

  1. git reset HEAD~1

See git status and git diff, files from last commit are now in changed.

If you want remove last commit with its changes, use:

  1. git reset --hard HEAD~1

And see (git status, git diff), no changes.

Interactive Rebase

Create some demo commits:

  1. touch A
  2. git add A
  3. git commit -m A
  4. touch B
  5. git add B
  6. git commit -m B
  7. touch C
  8. git add C
  9. git commit -m C
  10. touch D
  11. git add D
  12. git commit -m D
  13. touch E
  14. git add E
  15. git commit -m E
  16. touch F
  17. git add F
  18. git commit -m F
  19. touch G
  20. git add G
  21. git commit -m G
  22. touch H
  23. git add H
  24. git commit -m H

You rewrite history, join commits, update messages, reorder commits, …

  1. git rebase -i <ref>

Example:

  1. git rebase -i HEAD~6

Cherry Pick

Copy commit (ref) to actual HEAD.

  1. git cherry-pick <ref>

Example:

  1. git cherry-pick v1.0.x
  2. git cherry-pick 47bdfb7

git reflog

Reflog shows a history of refference. By default shows a HEAD. You can undo any git operations even reset.

  1. git reflog
  1. git reflog <branch>

git tag

Create tag:

  1. git tag <tag> [<ref>]

Example:

  1. git tag v1.0.0
  2. git tag v1.0.0 HEAD~1
  3. git tag v1.0.0 master
  4. git tag v1.0.0 075615a

List tags:

  1. git tag

Push tag:

  1. git push <remote> <tag>

Example:

  1. git push origin v1.0.0

Push all tags:

  1. git push <remote> --tags

Example:

  1. git push origin --tags

Delete tag (not recommended):

  1. git tag -d <tag>

Example:

  1. git tag -d v1.0.1

Delete tag from server:

  1. git push <remote> :<tag>

Example:

  1. git push origin :v1.0.2

git blame

See authors of actual code

  1. git blame <file>

See authors of code in specific revision

  1. git blame <rev> <file>

See only lines from 1 to 10

  1. git blame -L 1,10 <file>

Submodules

Clone repository with submodules:

  1. git clone --recursive <repo_url>

If you have cloned repository without --recursive you have to:

  1. git submodule update --init
  2. # for nested submodules
  3. git submodule update --init --recursive

Add submodule to repository:

  1. git submodule init
  2. git submodule add <submodule_repo_url> [<path>]

Add submodule and track specific branch:

  1. git submodule add -b <branch> <submodule_repo_url> [<path>]

Update tracked branch:

  1. git submodule set-branch --branch <branch> <path>

Update remote repository:

  1. git submodule set-url <path> <newurl>

Update submodule from remote repository

  1. git submodule update --remote

Pull changes & pull submodules

  1. git pull --recurse-submodules

Execute command for each submodule:

  1. git submodule foreach 'git reset --hard'
  2. # including nested submodules
  3. git submodule foreach --recursive 'git reset --hard'

Thank you! & Questions?

That’s it. Do you have any questions? Let’s go for a beer!

Ondrej Sika

Do you like the course? Write me recommendation on Twitter (with handle @ondrejsika) and LinkedIn (add me /in/ondrejsika and I’ll send you request for recommendation). Thanks.

Wanna to go for a beer or do some work together? Just book me :)

Extra

git bisect

Git bisect helps you find commit which introduce a bug

Start bisect session

  1. git bisect start

Select goog commit

  1. git bisect goog <ref>

Set bad commit

  1. git bisect bad <ref>

Now check if actual working tree contains bug.

If yes

  1. git bisect bad

In not

  1. git bisect good

Repeat it until bisect print out the commit which introduce a bug.

To close bisect session, use:

  1. git bisect reset

GPG Commit Signing

  1. git config --global commit.gpgsign true
  1. git config --global user.signingkey B000780A20CF1013F7A59081775D8A020903EF6B

Auto setup remote on push (from Git version 2.37.0)

  1. git config --global --add --bool push.autoSetupRemote true

Source: https://twitter.com/ji/status/1546948817462800384