bookmark_borderGit 101

Introduction

Git significantly improves developers’ productivity.

Contents

  1. What is Git?
  2. Git Setup – Local Machine
  3. Git Basic Command
  4. Git – Tagging
  5. Create, delete, and list git branch
  6. Git branch with colors in Bash
  7. Git branch with colors in Zsh

What is Git?

Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git Setup – Local Machine

  1. Installation
  2. Setup for local

Installation

Open Terminal and Enter git

$ git
$ git --version
git version 2.24.3 (Apple Git-128)

or Download https://git-scm.com/download/mac

GitHub or Gitlab username.

$ git config --global user.name “github or gitlab username”

Enter GitHub or Gitlab email.

$ git config --global user.email “valid email”

Line Coloring.

$ git config --global color.ui auto

Git Basic Command

Setup & Init

Initialize an existing directory as a Git repository

$ git init
Initialized empty Git repository in ~/Desktop/git-directory/.git/

retrieve an entire repository from a hosted location via URL

$ git clone [url]

Inspect git

show the commit history for the currently active branch & show any object in Git.

$ git log
$ git show
Merge: 5dfd7fc d53a204
Author: jayjo
Date:   Sun Nov 8 11:40:20 2020 +0900

    Merge branch 'master' of 

Add all file contents to the index and Record changes to the repository

$ git add . && git commit -m "description"

Remove a git commit that has not been pushed

$ git reset HEAD~1

Git – Tagging

Listing Your Tags

$ git tag
v1.0.0

Creating Tags

# create tag in local
$ git tag -a v1.0.1 -m "version updated to 1.0.1"
$ git tag
v1.0.0
v1.0.1

#push tag to the server
$ git push origin v1.0.1

#push all tags to the server
$ git push origin --tags

Delete v1 .0.1 tag

# delete tag v1.0.1 in local
$ git tag -d v1.0.1

# delete tag v1.0.1 in the server
$ git push origin :refs/tags/v1.0.1

Checking out Tags

$ git checkout v1.0.1

https://git-scm.com/book/en/v2/Git-Basics-Tagging

Create, delete, and list git branch

Create a branch

(master) $ git branch branch_one
(master) $ git checkout branch_one
Switched to branch 'branch_one'
(branch_one) $
  • the branch name is branch_one

Edit code and push the remote branch

(branch_one) $ git add . && git commit -m "branch test"
[b_one 1b3fb2e] branch test
 1 file changed, 3 insertions(+), 1 deletion(-)
(branch_one) $ git push origin b_one

Now you can pull requests to master.

Delete the branch

(master) $ git branch -d branch_one
error: The branch 'branch_one' is not fully merged.
If you are sure you want to delete it, run 'git branch -D branch_one'.
  • If you want to delete the branch, the branch should be merged.
(master) $ git branch -D branch_one
Deleted branch branch_one (was 66a559f).
(master) $ git push origin --delete branch_one
To https://github.com/xxxxx/xxxx-xxxx-xxxx.git
 - [deleted]         branch_one

List of branches

(branch_one) $ git branch -a
  =
* branch_one
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/branch_one
  remotes/origin/master

Git branch with colors in Bash

$ vi ~/.bash_profile
# Git branch in prompt.
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

Copy and paste the code in .bash_profile

(master) $ 

Git branch with colors in Zsh

$ vi ~/.zshrc
# Load version control information
autoload -Uz vcs_info
precmd() { vcs_info }

# Format the vcs_info_msg_0_ variable
zstyle ':vcs_info:git:*' formats 'on branch %b'

# Set up the prompt (with git branch name)
setopt PROMPT_SUBST
PROMPT='%n in ${PWD/#$HOME/~} ${vcs_info_msg_0_} > '
  • Add Git branch

https://github.com/ohmyzsh/ohmyzsh

And using Oh My Zsh is an open source, community-driven framework for managing your zsh configuration.

Installation

$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Add plugins and use plugins

$ vi ~/.zshrc
plugins=(
  git
  bundler
  dotenv
  osx
  rake
  rbenv
  ruby
)
ZSH_THEME="agnoster" # (this is one of the fancy ones)
# see https://github.com/ohmyzsh/ohmyzsh/wiki/Themes#agnoster
ANOTE.DEV