发布于 2026-01-06 0 阅读
0

面向初学者的 Git 命令!

面向初学者的 Git 命令!

配置 Git

# list all the configured values
git config --list 

# Set username & email for git globally
git config --global user.name "Progressive Programmer"
git config --global user.email "progressive@gmail.com"

# Set committer name & email for git
git config --global committer.name "Programmer"
git config --global committer.email "programmer@gmail.com"
Enter fullscreen mode Exit fullscreen mode

创建一个新的存储库

# Create a new folder and initialize git repo
git init name  

# Clone a remote repo in your local system
git clone url_of_repo 
Enter fullscreen mode Exit fullscreen mode

将文件/文件夹添加到暂存区

# Adds mentioned file/folder to the staging area
git add hello.py 

# Adds all the files and folders in the current directory 
git add . 

# Opens the file and let you choose 
the portion to be added for next commit
git add -p hello.py
Enter fullscreen mode Exit fullscreen mode

提交更改

# Commit a snapshot of staged changes
git commit  

# Commit a snapshot of changes in the working directory. 
git commit -a

# Shortcut for commit with a commit message 
git commit -m "commit message"
Enter fullscreen mode Exit fullscreen mode

Git 分支

# Create a new branch
git branch crazy_experiment

# List all branches 
git branch
# List both remote and local branches   
git branch -a
# List only branches that match the pattern mentioned 
git branch --list  'pattern here'
Enter fullscreen mode Exit fullscreen mode

切换 | 删除 | 重命名分支


# Move to different branches
git checkout branch_name
git switch  branch_name

# Shortcut to create a new branch and switch to the branch  
git switch -c  new_branch_name
git checkout -b  new_branch_name 

# Delete the given branch 
git branch -d  trash
# Force delete the given branch 
git branch -D  trash

# Rename the current branch 
git branch -m  new_name
Enter fullscreen mode Exit fullscreen mode

合并分支


# Merge given branch name with the working branch
git merge  branch_name
# Continue merger after conflict resolution
git merge  --continue 
Enter fullscreen mode Exit fullscreen mode

检查状态

# Shows the working tree status
git status  

# Shows status of working tree in a short format 
git status --short

# Shows status of branch in a short format 
git status --branch
Enter fullscreen mode Exit fullscreen mode

远程存储库


# Lists remote repo name and url(fetch/push) 
git remote -v

# Add a remote repository with local repository 
git remote add origin url_remote_repo

# Remove the remote repo with given name
git remote remove repo_name

# Rename the remote repo with given name
git remote rename old_name new_name

# Updates remote repo with local repo 
git push 
Enter fullscreen mode Exit fullscreen mode

希望这能帮到你!!🚀🚀

你最喜欢的git命令是什么?

别忘了在评论区留下您的想法和反馈。也欢迎您提出任何补充建议。

文章来源:https://dev.to/progressiveprogrammer/git-commands-for-beginners-pdi