我常用的 30 多个 Git 命令
你好世界👋
本文将列出我经常使用的所有 Git 命令。这并非完整列表,仅包含我常用的命令。其目的是方便您快速查找所需操作。
- 克隆仓库
- 将仓库克隆到指定目录
- 将当前目录初始化为 Git 仓库
- 添加文件
- 添加当前目录中的所有文件
- 添加所有文件,包括 .gitignore 文件中的文件。
- 创建一个新的提交
- 将更改添加到现有提交
- 显示状态
- 以简短格式显示状态
- 以简短格式显示状态并显示分支
- 显示提交日志
- 显示未暂存文件的差异
- 显示暂存文件的差异
- 显示所有带有 URL 的远程服务器
- 添加新遥控器
- 更改现有远程 URL
- 切换到另一个分支
- 创建一个新的分支并切换到该分支。
- 删除当前目录中所有未暂存的更改
- 删除文件的所有未暂存更改
- 将本地更改推送到远程仓库
- 推送并将远程设置为上游
- 强制将本地更改推送到远程仓库
- 删除远程仓库中的分支
- 删除本地分支
- 强制本地删除分支
- 删除所有未跟踪的文件和目录
- 将该分支合并到当前分支
- 从远程获取更改并合并
- 删除所有尚未提交的已跟踪文件的更改。
git 克隆
# Create 'blogs' folder and clone the 'pbteja1998/blogs' repo into it
git clone https://github.com/pbteja1998/blogs.git
# Create `my-blogs` folder and clone the `pbteja1998/blogs` repo into it
git clone https://github.com/pbteja1998/blogs.git my-blogs
git init
# Initializes the current directory as a git repo
git init
git 添加
# Adds the file contents to the index
# Ready to be committed next time you run `git commit`
# By default, Ignores the files present in `.gitignore`
# Add a single file
git add README.md
# Add all the files in the current directory
git add .
# Also adds the files present in `.gitignore`
git add -f .
git提交
# Commits/Records the changes to the local repo
git commit -m "some message"
# Does not create a new commit
# Adds the changes to the most recent commit
git commit --amend
git 状态
# Shows the status of the working tree
git status
# Shows the output in short format
git status -s
# Shows the branch even in short format
git status -sb
git 日志
# Shows the commit logs
git log
git diff
# Shows the changes between unstaged files and the commits
git diff
# Shows the changes between staged(ready-to-be-committed) files and the commits
git diff --staged
git远程
# Shows all the remotes configured and their remote URL
git remote -v
# Adds a remote
# git remote add <remote-name> <remote-url>
git remote add upstream https://github.com/something/blogs.git
# Changes the URL of the remote
git remote set-url upstream https://github.com/some-thing/blogs.git
git checkout
# Switch to branch
# git checkout <branch>
git checkout master
# Creates a new branch and switch to that
git checkout -b new-feature
# Removes all the unstaged changes in the current directory
git checkout .
# Removes all the unstaged changes for a file
git checkout -- README.md
git push
# Pushes the local changes to the remote to keep it up-to-date
git push origin master
# Force push the local changes to the remote
# Usually git will not allow you to push to the remote if the remote has some commits that are not present in local repo
# This will override that check and lets you force push to the remote
# This may cause the remote to lose some commits. So use it carefully.
git push -f origin master
# Push and set the remote as upstream
# same as `git push --set-upstream origin feature-branch`
git push -u origin feature-branch
# Deletes the branch in the remote
# same as `git push --delete origin new-feature`
git push -d origin new-feature
git 分支
# Deletes the branch locally
# same as `git branch --delete feature-branch`
git branch -d feature-branch
# Force delete a branch even if it's not merged
# same as `git branch --delete --force feature-branch`
git branch -D feature-branch
git clean
# Removes all the files and directories that are not yet tracked by git
git clean -fd
git合并
# Merges the <branch> to the current branch
# git merge <branch>
git merge feature-branch
git pull
# Fetches the changes from the remote and merge it into local repo
git pull origin master
git 重置
# Removes all the changes to the tracked files that have not yet been committed
git reset --hard
接下来是什么?
下一篇文章很可能是我对 Kent C. Dodds 的 EpicReact.Dev 的评测系列文章的一部分。请查看系列页面了解更多信息。
下次再见👋
如果你喜欢这篇文章,请查看
如果您有任何意见,请在下方留言,或者您也可以在 Twitter 上 @ 我(@pbteja1998),或者随时关注我。
文章来源:https://dev.to/pbteja1998/30-git-commands-that-i-frequently-use-14cj



