[1093] Git command examples
Ref: Git-Commands
Here are some common Git command examples along with explanations:
Basic Commands
-
Initialize a Repository:
git initInitializes a new Git repository in the current directory.
-
Clone a Repository:
git clone https://github.com/user/repo.gitCreates a copy of an existing repository from a remote URL to your local machine.
-
Check Repository Status:
git statusDisplays 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 logLists the commit history for the repository.
Branching and Merging
-
Create a New Branch:
git branch new-branchCreates a new branch called
new-branch. -
Switch to a Branch:
git checkout branch-nameSwitches to the specified branch.
-
Merge Branches:
git merge branch-nameMerges the specified branch into the current branch.
Remote Repositories
-
Add a Remote Repository:
git remote add origin https://github.com/user/repo.gitAdds a remote repository with the name
origin. -
Push Changes to Remote:
git push origin branch-namePushes local changes to the specified branch of the remote repository.
-
Pull Changes from Remote:
git pull origin branch-nameFetches 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! 😊
浙公网安备 33010602011771号