- vừa được xem lúc

GIT FLOW OVERVIEW AND COMMON GIT COMMANDS

0 0 2

Người đăng: Truong Phung

Theo Viblo Asia

Common Git Commands

Here are some common Git commands used in daily version control tasks:

1. Basic Git Commands

  • git init: Initializes a new Git repository in the current directory.

    git init
    
  • git clone: Clones an existing repository from a remote server.

    git clone https://github.com/user/repo.git
    
  • git status Shows the current status of your working directory and staging area.

    git status
    
  • git add: Stages changes (files or directories) for the next commit.

    git add file.txt
    git add . # Stage all changes
    
  • git commit: Records the staged changes in the repository with a message.

    git commit -m "Commit message"
    
  • git push: Pushes the local commits to the remote repository.

    git push origin branch-name
    
  • git pull: Fetches and integrates changes from the remote repository to the local branch.

    git pull origin branch-name
    

2. Branching and Merging

  • git branch: Lists, creates, or deletes branches.

    git branch # List all branches
    git branch new-branch # Create a new branch
    git branch -d old-branch # Delete a branch
    
  • git checkout: Switches to a different branch or commit.

    git checkout branch-name
    git checkout -b new-branch # Create and switch to a new branch
    
  • git merge: Merges a branch into the current branch.

    git checkout main
    git merge feature-branch
    

3. Remote Repositories

  • git remote: Manages the remote repository connections.

    git remote -v # List remote connections
    git remote add origin https://github.com/user/repo.git # Add a new remote
    
  • git fetch: Downloads changes from the remote repository but does not integrate them.

    git fetch origin
    

4. Viewing History

  • git log: Displays the commit history.

    git log # Show commit history
    git log --oneline # Condensed history view
    git log --graph --oneline --decorate # Graphical commit history
    
  • git diff: Shows changes between commits, branches, or the working directory.

    git diff # Changes not staged
    git diff --staged # Changes staged for commit
    git diff branch1 branch2 # Compare two branches
    

5. Undoing Changes

  • git reset: Resets the staging area or moves the branch pointer.

    git reset file.txt # Unstage a file
    git reset --hard HEAD^ # Revert to the previous commit
    
  • git revert: Reverts a specific commit by creating a new commit that undoes it.

    git revert commit-sha
    

6. Stashing Changes

  • git stash: Temporarily saves changes that are not ready to be committed.

    git stash # Stash current changes
    git stash pop # Apply the stashed changes and remove them from the stash
    git stash list # List all stashed changes
    

7. Tagging

  • git tag: Creates tags to mark specific commits, typically used for releases.

    git tag v1.0 # Create a lightweight tag
    git tag -a v1.0 -m "Version 1.0" # Annotated tag
    git push origin v1.0 # Push tag to remote
    

8. Collaboration

  • git rebase: Re-applies commits on top of another base tip, useful for linearizing commit history.

    git checkout feature-branch
    git rebase main
    
  • git cherry-pick: Applies a specific commit from one branch to another.

    git cherry-pick commit-sha
    

These commands are foundational for working with Git in both solo and collaborative projects.


Git Flow Overview

Git Flow is a branching strategy for managing feature development, releases, and hotfixes in a structured and scalable way. It helps teams manage parallel development, collaborate efficiently, and handle releases and bug fixes smoothly. Here's a brief overview of the common Git Flow:

1. Main Branches

  • main (or master): The production-ready branch that contains the stable, deployed code. All releases are tagged here.
  • develop: The main development branch where the latest code for the next release is integrated. Features and fixes are merged into develop.

2. Supporting Branches

Git Flow uses additional branches for feature development, bug fixes, and releases. Each branch has a specific role:

  • Feature Branches (feature/):

    • Used to develop new features or changes.
    • Created from develop and merged back into develop once complete.
    • Example: feature/new-login
  • Release Branches (release/):

    • Used to prepare a new release.
    • Created from develop when it’s feature-complete, allowing for final testing, bug fixes, and versioning before merging into main.
    • Example: release/v1.0
  • Hotfix Branches (hotfix/):

    • Used to fix critical bugs in production.
    • Created from main and merged back into both main and develop to ensure the fix is applied to both branches.
    • Example: hotfix/fix-payment-bug

3. Git Flow Workflow

  • Feature Development: Developers create a feature branch (feature/) from develop to work on new features. After completion, the feature is merged back into develop.
  • Releasing: Once develop is stable and ready for release, a release/ branch is created for final testing and adjustments. Once ready, it's merged into main (for production) and develop (to keep it updated).
  • Hotfixes: If a critical bug is found in production, a hotfix/ branch is created from main, fixed, and then merged into both main and develop to keep both updated.

4. Version Tags

After merging a release/ or hotfix/ into main, you tag the commit with a version number (e.g., v1.0) to mark the release.

Summary of Key Git Flow Commands

  • Start a feature:
    git checkout -b feature/xyz develop
    
  • Finish a feature:

    git checkout develop && git merge feature/xyz
    
  • Start a release:

    git checkout -b release/v1.0 develop
    
  • Finish a release:

    git checkout main && git merge release/v1.0
    
  • Start a hotfix:

    git checkout -b hotfix/fix-bug main
    
  • Finish a hotfix:

    git checkout main && git merge hotfix/fix-bug
    

Git Flow is ideal for teams that require a structured process for managing releases, features, and hotfixes, particularly in projects with regular production releases.

Bình luận

Bài viết tương tự

- vừa được xem lúc

Đặt tên commit message sao cho "tình nghĩa anh em chắc chắn bền lâu"????

. Lời mở đầu. .

1 1 737

- vừa được xem lúc

Tập hợp những câu lệnh GIT hữu dụng

Dưới đây là một vài ví dụ về các câu lệnh Git mà tôi thường dùng. git config --global user.name "John Doe". git config --global user.

0 0 68

- vừa được xem lúc

Cấu hình CI/CD với Github (phần 2): Trigger một work flow

Events trigger. Bạn có thể cấu hình cho workflows chạy khi có một sự kiện nào đó xảy ra trên GitHub, theo một lịch có sẵn hoặc cũng có thể là một sự kiện nào đó xảy ra ngoài GitHub.

0 0 80

- vừa được xem lúc

Cấu hình CI/CD với Github (phần 1): Một ít lý thuyết

CI/CD là gì. Về mặt khái niệm là vậy nhưng về mặt triển khai thì CI/CD là quá trình tự động thực hiện các quá trình build, test, release, deploy khi có các trigger như commit/merge code lên một branch định sẵn hoặc có thể là tự động chạy theo một lịch cố định.

0 0 128

- vừa được xem lúc

Giới thiệu về Git LFS

. Git LFS là gì . Git LFS làm điều này bằng cách thay thế các tệp lớn trong repo của bạn bằng một con trỏ nhỏ.

0 0 37

- vừa được xem lúc

Git workflow được Google và Facebook sử dụng có gì hay ho

Với developer thì Git hẳn là công cụ rất quen thuộc và không thể thiếu rồi. Thế nhưng có mấy ai thực sự hiểu được Git.

0 0 85