[1093] Git command examples
Ref: Git-Commands
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! 😊
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2021-01-15 【523】python存储变量方法
2021-01-15 【522】深度学习超参数调节
2019-01-15 【348】通过 Numpy 创建各式各样的矩阵
2019-01-15 【347】将jupyter notebook嵌入博客园
2018-01-15 【290】Python 常用说明
2018-01-15 【290】Python 模块(自定义包)
2018-01-15 【290】Python 函数