Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.
/ Git-Document Public archive

This is a repository for my summary about Git

Notifications You must be signed in to change notification settings

ngyngcphu/Git-Document

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 

Repository files navigation

Git Document

This is my summary document about Git

Table of contents

I. Git Basic

II. Git Branch

III. Git Stash

IV. GitHub Setting

V. Git Collaborating

VI. Git Flow and GitHub Actions

VII. References


I. Git Basic

  • Initializing a Repository in an Existing Directory
git init
  • Recording Changes to the Repository
git add <file_name> / git add .
git commit -m "<message>"
  • Viewing the Commit History
git log
git reset <commit_id>
git restore .
  • Checking the Status of Your Files
git status
  • .gitignore
# a comment - This line is ignored
# do not track files ending .a
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the TODO file in the current directory, not subdir/TODO
/TODO
#  ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/ directory
doc/**/*.txt

II. Git Branch

  • Creating a New Branch
git branch <branch_name>
  • Switching Branches
git checkout <branch_name>
  • Switching a new branch
git checkout -b <branch_name>
  • Git Merging
git merge
  • Fast Forward Merge
  • Three-Way Merge
  • Conflict

Read at here

III. Git Stash

  • Stashing Your Work
git stash
  • Apply the recent stash and then immediately drop it from your stack.
git stash pop
  • List Stashes
git stash list
  • Remove a stash
git drop <stash_code>
  • Apply your work in stash
git stash apply <stash_code>

1. Create personnel access token

  1. From user menu, click 'Settings'
  2. From sidebar menu, click 'Developer settings'
  3. From sidebar menu, click 'Personal access tokens'
  4. Click 'Generate new token' button
  5. Create new-token as follow
  6. Copy generated token to use later

2. Create a new repository

echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://[TOKEN]@github.com/[USER]/[REPO]
git push -u origin main

V. Git Collaborating

1. On the same branch

  • Create branch
git switch -C feature/introduction
# or
git checkout -b feature/introduction
# or
git branch -M feature/introduction
  • Push branch to origin
git push --set-upstream origin feature/introduction

a. Create your profile file

# Mac/Linux
touch introduction.txt
# Window
type nul > your_file.txt

b. Development time (update introduction.txt)

Name: <your_name>
Hobby: <your_hobby>

c. Commit code

git add .
git commit -m "add profile"
git push origin feature/introduction

d. Commit directly on branch from GitHub console without conflict

After editing from GitHub console, then

git pull

e. Commit directly on branch from GitHub console causing conflict

After editing from GitHub console, then

# Edit introduction.txt
vi introduction.txt #Mac/Linux
git add . commit
git commit -m 'update same file'

git pull
git rebase origin/feature/introduction

# Check git log
git log --all --decorate --oneline --graph

git push origin feature/introduction

f. Create a pull-request to main branch

g. Rebase to main branch

git rebase -i origin/main

git push origin feature/introduction -f

h. Merge pull-request

2. On the different branch

Task: Add row "University" and "Year" for the file: introduction.text but on different git branch and merge them to main

Example on branch feature/introduction_1

Name:
Hobby:
University: HCMUT #new line

on branch feature/introduction_2

Name:
Hobby:
Year: 2nd #new line

a. Create development branch

git switch main # switch to main branch
git pull # get latest code
git checkout -b feature/introduction_1 # create new branch

# edit introduction.txt file
vi introduction.txt #Mac/Linux

git add .
git commit -m "add university"
git push --set-upstream origin feature/introduction_1
git switch main  # switch to main branch
git pull # get latest code

git checkout -b feature/introduction_2 # create another new branch

# edit introduction.txt file
vi introduction.txt #Mac/Linux

git add .
git commit -m "add Year"
git push --set-upstream origin feature/introduction_2

b. Combine source

  • Merge
git checkout feature/introduction_1 # switch branch

git checkout -b feature/merge_introduction # create a merge branch
git merge feature/introduction_2

# resolve conflict if any
echo 'please resolve conflict if any'

git add .
git commit -m 'combined commits'
git push --set-upstream origin feature/merge_introduction
  • Rebase
git checkout feature/introduction_2 # switch branch

git rebase feature/introduction_1

# resolve conflict if any
echo 'please resolve conflict if any'

git add .
git rebase --continue
git push -f --set-upstream origin feature/introduction_2

c. Create a pull-request to main branch

d. Merge

VI. Git Flow and GitHub Action

workflow Branches

  • main
  • develop
  • dev-release
  • stg-release
  • prd-release
  • feature/**

VII. References

About

This is a repository for my summary about Git

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published