Basic Git commands

Here is a list of some basic Git commands to get you going with Git.

For more detail, check out the Atlassian Git Tutorials for a visual introduction to Git commands and workflows, including examples.

 

Git taskNotesGit commands

Create a new local repository

 
git init

Check out a repository

Create a working copy of a local repository:
git clone /path/to/repository
For a remote server, use:
git clone username@host:/path/to/repository

Add files

Add one or more files to staging (index):
git add <filename>

git add *

Commit

Commit changes to head (but not yet to the remote repository):
git commit -m "Commit message"
Commit any files you've added with git add, and also commit any files you've changed since then:
git commit -a

Push

Send changes to the master branch of your remote repository:
git push origin master
StatusList the files you've changed and those you still need to add or commit:
git status

Connect to a remote repository

If you haven't connected your local repository to a remote server, add the server to be able to push to it:

git remote add origin <server>
List all currently configured remote repositories:git remote -v

Branches

Create a new branch and switch to it:
git checkout -b <branchname>
Switch from one branch to another:
git checkout <branchname>
List all the branches in your repo, and also tell you what branch you're currently in:
git branch
Delete the feature branch:
git branch -d <branchname>
Push the branch to your remote repository, so others can use it:
git push origin <branchname>
Push all branches to your remote repository:
git push --all origin
Delete a branch on your remote repository:
git push origin :<branchname>

Update from the remote repository

 

Fetch and merge changes on the remote server to your working directory:
git pull
To merge a different branch into your active branch:
git merge <branchname>

View all the merge conflicts:

View the conflicts against the base file:

Preview changes, before merging:

git diff

git diff --base <filename>

git diff <sourcebranch> <targetbranch>
After you have manually resolved any conflicts, you mark the changed file:
git add <filename>

Tags

You can use tagging to mark a significant changeset, such as a release:
git tag 1.0.0 <commitID>
CommitId is the leading characters of the changeset ID, up to 10, but must be unique. Get the ID using:
git log
Push all tags to remote repository:
git push --tags origin

Undo local changes

If you mess up, you can replace the changes in your working tree with the last content in head:

Changes already added to the index, as well as new files, will be kept.

git checkout -- <filename>
Instead, to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it, do this:
git fetch origin

git reset --hard origin/master

Search

Search the working directory for foo():git grep "foo()"
### Git Fetch 与 Clone 的区别及使用场景 #### 差异概述 `git fetch` 和 `git clone` 是两个用于获取远程仓库数据的命令,但它们的功能和适用场景存在显著差异。 - **功能定义** `git fetch` 主要用于从远程仓库拉取最新的更改并存储在本地的远程分支中[^3]。而 `git clone` 则是从远程仓库复制整个项目及其历史记录到本地,并初始化一个新的工作目录[^1]。 - **初始状态影响** 当执行 `git clone` 时,它不仅会下载所有的文件和版本历史,还会创建一个全新的本地副本以及默认的工作区[^2]。相比之下,`git fetch` 不会影响当前的工作区或提交历史;它只是更新本地对于远程分支的状态认知。 #### 使用方法比较 ##### git fetch ```bash $ git fetch origin ``` 上述命令将从名为 `origin` 的远程服务器上抓取最新数据至本地跟踪分支下,不会自动合并这些变化到现有分支里。 ##### git clone ```bash $ git clone https://github.com/example/repo.git mylocalrepo ``` 此命令把指定URL地址指向的远端储存库完全拷贝下来形成新的mylocalrepo文件夹作为项目的根路径位置。 #### 场景应用对比 | 特性/用途 | git fetch | git clone | |-------------------|---------------------------------------|------------------------------------| | 数据获取范围 | 更新已有仓库中的远程分支信息 | 复制完整的远程仓库 | | 是否改变工作树 | 否 | 是 | | 需求前提条件 | 必须已经有一个已存在的本地Git仓库 | 可以在一个空白目录或者任何地方运行| 因此,在初次接触某个特定代码基底的时候应该选用`git clone`, 而当你已经在维护着一份该软件源码并且想要保持其处于最新状态时候,则更适合采用`git fetch`. ### 结论 综上所述, 如果你是第一次访问某份资料那么就应当利用`git clone`; 若你已经有了该项目的一个实例并且希望安全地接收来自上游的变化而不立即融入自己的改动之中的话,那就需要用到`git fetch`了. ```python # Example of using Python to simulate a basic CLI interaction with these commands. def execute_git_command(command_type, remote="origin", local_dir=None): if command_type == 'fetch': return f"$ git fetch {remote}" elif command_type == 'clone' and local_dir is not None: return f"$ git clone https://{remote}.com/{project_name} {local_dir}" print(execute_git_command('fetch')) print(execute_git_command('clone', local_dir='new_project')) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值