This is my summary document about Git
VI. Git Flow and GitHub Actions
- 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
- 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
- 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>
- From user menu, click 'Settings'
- From sidebar menu, click 'Developer settings'
- From sidebar menu, click 'Personal access tokens'
- Click 'Generate new token' button
- Create new-token as follow
- Copy generated token to use later
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
- 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
# Mac/Linux
touch introduction.txt
# Window
type nul > your_file.txt
Name: <your_name>
Hobby: <your_hobby>
git add .
git commit -m "add profile"
git push origin feature/introduction
After editing from GitHub console, then
git pull
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
git rebase -i origin/main
git push origin feature/introduction -f
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
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
- 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
- main
- develop
- dev-release
- stg-release
- prd-release
- feature/**