👩‍💻 适合初学者的 Git 和 Github

2025-05-28

👩‍💻 适合初学者的 Git 和 Github

Git 是什么

Git 是一个分布式版本控制系统,用于在软件开发过程中跟踪源代码的变化。它旨在协调程序员之间的工作,但它可以用来跟踪任何文件集的变化。

这基本上意味着:

  • 它是一个记录我们的文件随时间变化的系统。
  • 您可以随时调用并返回该文件的特定版本。
  • 其他开发人员可以协作并在自己的计算机上拥有文件的本地版本。

为什么要使用 git ?

  • 您可以将项目的修订版本存储在一个目录中。
  • 随时轻松返回您的修订版本。
  • 开发新功能而不破坏原始代码库。
  • 与其他开发人员合作,不受地理限制。

Github 是什么?

Github 是一个在线服务,你可以托管你的项目、分享你的代码,并帮助其他开发者下载和开发你的项目。之后,他们可以上传他们的代码编辑并与主分支/代码库(master 分支)合并。

如何安装 Git

  • 前往Git 网站
  • 如果你使用的是 Windows,我推荐你使用Cmder。它是 Windows 的命令行界面。下载安装 Git 时附带的完整版本。(我用过,绝对值得一试)。

如何设置您的用户名和电子邮件

打开你的 cdmer(我将在本文写作过程中使用它)

   git config --global user.name jane tracy 
Enter fullscreen mode Exit fullscreen mode

用于设置电子邮件

   git config --global user.email janetracy00@gmail.com 
Enter fullscreen mode Exit fullscreen mode

查看您注册的用户详细信息

   git config user.name
   git config user.email 
Enter fullscreen mode Exit fullscreen mode

基本命令控制

  • 创建文件夹:mkdir test
  • 要创建文件:touch index.html style.css app.js
  • 删除文件:rm index.html
  • 查看文件夹内部:ls (Ls)/ dir
  • 要向上移动文件夹:cd ..
  • 删除文件夹:rmdir test
什么是 Git 存储库 (.git)

这是项目内部的一个文件夹,Git 会跟踪文件中的所有更改,并构建一个随时间推移的历史记录。在项目的 Git 仓库文件夹中,您将看到一个 .git 文件夹。
注意:Git 仓库应位于项目文件夹的根目录,以便跟踪整个项目的更改。

   git init
Enter fullscreen mode Exit fullscreen mode

git 中各个阶段的工作

项目图像

1)git状态

运行 git status 将显示哪些文件当前位于暂存区。

   git status
Enter fullscreen mode Exit fullscreen mode

如果列出的文件名为红色,则表示它们不在暂存区。如果文件名为绿色,则表示它们已在暂存区,可以提交。

2)git添加

运行 git add 会将文件移动到暂存区。这有助于你在提交之前检查更改。
要添加单个文件

   git add index.html 
Enter fullscreen mode Exit fullscreen mode

添加多个文件

   git add . 
Enter fullscreen mode Exit fullscreen mode

3)git rm

这有助于取消暂存区中的任何文件。

    git rm --cached index.html 
Enter fullscreen mode Exit fullscreen mode

4)git提交

运行此命令会将您的文件提交到暂存区。您还可以在提交中添加描述性消息,以便将来查看项目文件夹时使用。

    git commit -m "added index and styles file"
Enter fullscreen mode Exit fullscreen mode

5)git log/git log--oneline

git log 可帮助您查看提交历史记录。每个提交都有一个唯一的 ID、作者详细信息、日期、时间和提交消息。

    git log/ git log --oneline
Enter fullscreen mode Exit fullscreen mode

git log --oneline 显示较短的版本(一行日志)。它包含 id 和提交消息。

    ## get commits by a specific author
    git log --author jane

    ## get commits by message
    ## get commit that contains 'index'
    git log --all --grep index

    ## get commit in the last 2 weeks
    git log --since=2.weeks
Enter fullscreen mode Exit fullscreen mode

撤销操作

git 撤销镜像

6)git checkout提交

这可以帮助您返回到上一个提交。但是所做的任何更改都不会被保存,提交历史记录也不会被破坏。这是一个只读阶段,因此它比还原或重置更安全。如果您想创建一个新分支来保留您创建的提交,可以使用 switch 命令中的 -c 选项。

    git switch -c <new-branch-name>
    ## undo this by:
    git switch -
Enter fullscreen mode Exit fullscreen mode

7)git 恢复

它反转提交引入的更改并使用反向代码创建一个新的提交。这比使用 git reset 更安全,而且它不会永久删除提交。

8)git 重置

这不会删除提交,但提交将无法通过引用直接访问。它会更改提交历史记录。可以使用git reflog查找和恢复这些提交。

   git checkout 91471e4
   git revert 91471e4
   git reset 91471e4

   ## Any pending work that was hanging out in the 
   Staging Index and Working Directory will be lost.
   git reset 91471e4 --hard

   ##The Staging Index is reset to the state of the specified 
     commit.Any changes that have been undone from the Staging 
     Index are moved to the Working Directory. 
     git reset 91471e4 --mixed

  ##The ref pointers are updated and the reset stops there. The 
    Staging Index and the Working Directory are left untouched.
    git reset 91471e4 --soft

Enter fullscreen mode Exit fullscreen mode

有关git reset及其工作原理的更多详细信息。

9)git分支

git 分支
如果您想要尝试某个新功能,而不是直接提交到主分支,您可以创建一个新分支,复制主分支中的代码状态,然后添加新的提交,最后将其合并到主分支。如果您对新功能不满意,可以直接删除该分支并返回到初始代码库。

    ##To create a branch
    git branch feature-1

    ##To switch to a branch
    git checkout feature-1
    ## To create and switch to a branch
    git checkout -b feature-a
    ## To push a branch up on github
    git push origin feature-a

    ## To check the branches
    git branch -a

    ## To delete a branch
    git branch -d feature-1
    ## To forcefully delete a branch even if it's unmerged
    git branch -D feature-1

    ## To rename a current branch
    ## Rename feature-1 to feature-a
    git branch -m feature-a
Enter fullscreen mode Exit fullscreen mode

10)git合并

它将多个提交序列组合成一个示例,您可以使用它将 feature-1 组合到主分支。

   git merge feature-1
Enter fullscreen mode Exit fullscreen mode

发生冲突可能是因为当您在另一个分支上工作时,有人修改了主分支上的代码。合并操作必须等到您解决冲突后才能进行,您可以手动操作。了解更多关于git merge 的信息

如何在 Github 中创建仓库

11)git推送

1)方法一

假设你有一个正在编写的项目,并且想要将其发布到 GitHub 上以供协作或托管。你可以进入 GitHub 创建一个新的仓库,复制仓库的 URL 并运行:

   ## To push it into the master branch
   git push <url link> master
Enter fullscreen mode Exit fullscreen mode

当你想要编辑代码并再次推送到 GitHub 时,你可以先为远程仓库创建一个别名:

   git remote add origin <url>
   git remote add origin https://github.com/muchirijane/git-learning.git

   ## To push it into the master branch
   git push -U origin master
Enter fullscreen mode Exit fullscreen mode

12)git克隆

2)方法二

在 Github 中创建一个仓库并将其克隆到您的计算机

   git clone <url>
   ## you have a url repo
   git clone https://github.com/muchirijane/git-learning.git

   ## To push it up in Github to the master branch
   git push origin master

   ## To see urls of the remote repositories
   git remote -v
Enter fullscreen mode Exit fullscreen mode

在 Github 上协作

13)git pull

1)步骤1

这将从远程存储库(github repo)获取并下载代码,并更新本地存储库以匹配它。

   git pull origin master
Enter fullscreen mode Exit fullscreen mode
2)步骤2

创建新分支,添加代码并提交

   git checkout -b feature-2
Enter fullscreen mode Exit fullscreen mode
3)步骤3

将您的分支推送到 GitHub 并创建拉取请求

   git push origin feature-2
Enter fullscreen mode Exit fullscreen mode

结论

如果您想在技术领域更上一层楼,学习 Git 非常重要。我还没有讲解过 Git 或 Github 的所有命令,但这些基础知识可以帮助您入门。您可以查看 YouTube 上的教程了解更多信息。未来我会制作第二部分,介绍更高级的命令。
如果您想了解如何创建 Git 别名,请访问此网站

🔥 奖励部分

Github 中的 Forking

你可以用这种方法来做你的第一个开源项目。我们以贡献给firstcontributions
为例

1)第一步:Fork GitHub 仓库

点击页面顶部的 fork 按钮即可完成。这将在你的 Github 帐户中创建仓库的副本。
从 GitHub 上 fork 镜像

2)第二步:克隆项目

从 GitHub 上 fork 镜像
点击克隆按钮,然后点击复制到剪贴板图标。
这将在您的计算机上创建项目文件的副本。
附言:您需要创建一个“开源”文件夹,用于复制项目文件。
打开您的终端,或者像我一样,使用 cdmer 并运行

   git clone <url>
   ## Let's get the url for contributions repo
   git clone https://github.com/muchirijane/first-contributions.git
Enter fullscreen mode Exit fullscreen mode
3)运行 git status

在开始编码之前,运行 git status 以确保项目文件中的所有内容都使用“origin/master 分支”进行更新

   git status
Enter fullscreen mode Exit fullscreen mode
4)创建新分支

在这个面向初学者的开源项目中,你的任务是添加你的名字。
分支名称将包含你的名字

   git checkout -b <add-your-name-in-the-branch>
   ## My name to the branch
   git checkout -b add-jane-tracy
Enter fullscreen mode Exit fullscreen mode
5)做出你的贡献

对于这种情况,您需要将您的姓名添加到contribution.md文件中,
然后运行git add,git commit并推送您的分支

   git add .
   ## commit the changes
   git commit -m "added jane muthoni to the contributors list"
   ## Let's push our branch
   git push origin <branch-name-you-created>
   git push origin add-jane-muthoni
Enter fullscreen mode Exit fullscreen mode
6)比较并拉取请求

不!你还没完成,还有一步。
要将你的代码贡献到 origin 仓库,
请点击“比较并拉取请求”
GitHub 上的开源贡献

7)创建新的拉取请求

GitHub 上的开源贡献
如果您愿意,可以发表评论,然后单击“创建拉取请求”按钮。

👩‍💻💃 祝贺您首次做出开源贡献

如果您已完成上述步骤,您的分支将由 Github 存储库的原始所有者合并到主分支。
GitHub 上的开源贡献

我为你感到骄傲。这只是第一步。请使用“首次贡献”提交你的第一个拉取请求,并查看此列表以了解更多项目。也
欢迎你关注我的 Github

如果这篇文章对你有帮助,你也可以支持我。🙂

给我买杯咖啡

文章来源:https://dev.to/tracycss/git-and-github-for-beginners-po3
PREV
通过 Github 存储库学习代码👩‍💻
NEXT
可打印的 PDF - Git 备忘单