Here are some common Git command examples along with explanations:
Basic Commands
-
Initialize a Repository:
git init
Initializes a new Git repository in the current directory.
-
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.
-
Check Repository Status:
git status
Displays the state of the working directory and the staging area.
-
Stage Changes:
git add filename git add .
Adds changes in the specified file (or all changes with
.
) to the staging area. -
Commit Changes:
git commit -m "Your commit message"
Records changes in the repository with a descriptive message.
-
Show Commit History:
git log
Lists the commit history for the repository.
Branching and Merging
-
Create a New Branch:
git branch new-branch
Creates a new branch called
new-branch
. -
Switch to a Branch:
git checkout branch-name
Switches to the specified branch.
-
Merge Branches:
git merge branch-name
Merges the specified branch into the current branch.
Remote Repositories
-
Add a Remote Repository:
git remote add origin https://github.com/user/repo.git
Adds a remote repository with the name
origin
. -
Push Changes to Remote:
git push origin branch-name
Pushes local changes to the specified branch of the remote repository.
-
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
-
Initialize a Repository:
git init
-
Create and Switch to a New Branch:
git branch new-feature git checkout new-feature
-
Stage and Commit Changes:
git add . git commit -m "Add new feature"
-
Push to Remote:
git push origin new-feature
-
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!
标签:git,remote,repository,Git,command,examples,branch,new,changes From: https://www.cnblogs.com/alex-bn-lee/p/18672787