alex_bn_lee

导航

[1093] Git command examples

Ref: Git-Commands


Here are some common Git command examples along with explanations:

Basic Commands

  1. Initialize a Repository:

    git init
    

    Initializes a new Git repository in the current directory.

  2. Clone a Repository:

    git clone https://github.com/user/repo.git
    

    Creates a copy of an existing repository from a remote URL to your local machine.

  3. Check Repository Status:

    git status
    

    Displays the state of the working directory and the staging area.

  4. Stage Changes:

    git add filename
    git add .
    

    Adds changes in the specified file (or all changes with .) to the staging area.

  5. Commit Changes:

    git commit -m "Your commit message"
    

    Records changes in the repository with a descriptive message.

  6. Show Commit History:

    git log
    

    Lists the commit history for the repository.

Branching and Merging

  1. Create a New Branch:

    git branch new-branch
    

    Creates a new branch called new-branch.

  2. Switch to a Branch:

    git checkout branch-name
    

    Switches to the specified branch.

  3. Merge Branches:

    git merge branch-name
    

    Merges the specified branch into the current branch.

Remote Repositories

  1. Add a Remote Repository:

    git remote add origin https://github.com/user/repo.git
    

    Adds a remote repository with the name origin.

  2. Push Changes to Remote:

    git push origin branch-name
    

    Pushes local changes to the specified branch of the remote repository.

  3. Pull Changes from Remote:

    git pull origin branch-name
    

    Fetches and merges changes from the specified branch of the remote repository into the current branch.

Example Workflow

  1. Initialize a Repository:

    git init
    
  2. Create and Switch to a New Branch:

    git branch new-feature
    git checkout new-feature
    
  3. Stage and Commit Changes:

    git add .
    git commit -m "Add new feature"
    
  4. Push to Remote:

    git push origin new-feature
    
  5. Merge New Feature into Main Branch:

    git checkout main
    git merge new-feature
    git push origin main
    

Explanation

  • git init: Sets up a new Git repository.
  • git clone: Clones an existing repository.
  • git status: Shows the status of the working directory.
  • git add: Stages changes for the next commit.
  • git commit: Commits the staged changes.
  • git log: Displays the commit history.
  • git branch: Manages branches.
  • git checkout: Switches branches.
  • git merge: Merges branches.
  • git remote: Manages remote repositories.
  • git push: Pushes changes to a remote repository.
  • git pull: Fetches and merges changes from a remote repository.

These commands cover a wide range of common Git operations and should help you manage your repositories effectively. If you have any specific questions or need more details, feel free to ask! 😊

posted on 2025-01-15 12:44  McDelfino  阅读(24)  评论(0)    收藏  举报