git版本控制&&github的使用

简介:

不知道什么是git?额,你火星了,抓紧时间百度去吧。。。。

废话不多说了,直接开始正题

一:先说说如何在本地建立和使用git

      0.首先下载安装git:sudo apt-get install git-core git-doc git-gui 

      1.建一个工作目录: mkdir test

      2.转到该目录: cd test

      3.输入命令:git init 初始化工作就算做完了

      4.然后你就可以在目录下面进行工作了,比如写代码

      5. 代码写的差不多了,就添加进版本库吧:git add . (这是将所有文件添加进去)

      6. 建立一个版本,提交一下:git commit -m "first commit"

这样基本工作就算做完了,你以后就可以修改删除代码,提交更改,除此之外,还可以做点其他的事:

      7.查看日志:git log

      8.查看详细日志:git log --stat --summary

      9.显示更新细节:git show 版本名(就是那一串很长的字母)

      10.恢复版本:git revert --no-commit head

      11.恢复文件: git checkout head *.cpp

      12.重命名版本:git tag v1.0

这只是在本地实现的,以后一个软件或许要多人开发,为了方便控制版本,交流代码,或者展示开发进度,很有必要在网上弄一个版本库,每个人完成后就提交到版本库中,国外有一个非常好的网站叫github,网址github.com,这个网站比较有趣,免费的空间里面代码是完全公开的,想弄个私人的或企业的,嘿嘿,交钱,物有所值。

     简单说说怎么用吧:

1.注册,用户名,邮箱,密码就OK

2.接下来配置ssh,下面是官网上的说明,直接考过来

Set Up SSH Keys

We use SSH keys to establish a secure connection between your computer and GitHub. Setting them up is fairly easy, but does involve a number of steps.

To make sure you generate a brand new key, you need to check if one already exists. First, you need to open an app called Terminal.

Open the terminal

Need a quick lesson about Terminal?

  1. Check for SSH keys. Have an existing key pair? You can skip to Step 4.

    First, we need to check for existing ssh keys on your computer:

    $ cd ~/.sshChecks to see if there is a directory named ".ssh" in your user directory

    If it says “No such file or directory“ skip to step 3. Otherwise continue to step 2.

  2. Backup and remove existing SSH keys.

    Since there is already an SSH directory you’ll want to back the old one up and remove it:

    $ lsLists all the subdirectories in the current directoryconfig	id_rsa	id_rsa.pub	known_hosts$ mkdir key_backupmakes a subdirectory called "key_backup" in the current directory$ cp id_rsa* key_backupCopies the id_rsa and id_rsa.pub files into key_backup$ rm id_rsa*Deletes the id_rsa and id_rsa.pub files
  3. Generate a new SSH key.

    To generate a new SSH key, enter the code below. We want the default settings so when asked to enter a file in which to save the key, just press enter.

    $ ssh-keygen -t rsa -C "your_email@youremail.com"Creates a new ssh key using the provided emailGenerating public/private rsa key pair.Enter file in which to save the key (/Users/your_user_directory/.ssh/id_rsa):<press enter>

    Now you need to enter a passphrase.

    Why do passphrases matter?

    Enter passphrase (empty for no passphrase):<enter a passphrase>Enter same passphrase again:<enter passphrase again>

    Which should give you something like this:

    Your identification has been saved in /Users/your_user_directory/.ssh/id_rsa.Your public key has been saved in /Users/your_user_directory/.ssh/id_rsa.pub.The key fingerprint is:01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db user_name@username.comThe key's randomart image is:+--[ RSA 2048]----+|     .+   +      ||       = o O .   ||        = * *    ||       o = +     ||      o S .      ||     o o =       ||      o . E      ||                 ||                 |+-----------------+
  4. Add your SSH key to GitHub.

    On the GitHub site Click “Account Settings” > Click “SSH Public Keys” > Click “Add another public key”

    Account Settings

    Open the id_rsa.pub file with a text editor (Notepad, TextEdit, or gedit will do just fine). This is your public SSH key. You may need to turn on “view hidden files” to find it because the .sshdirectory is hidden. It’s important you copy your SSH key exactly as it is written without adding any newlines or whitespace. Now paste it into the “Key” field.

    Can’t view hidden files? Other ways to copy:

    Now paste it into the “Key” field.

    Paste your SSH Key

    Hit “Add Key.”

  5. Test everything out.

    To make sure everything is working you’ll now SSH to GitHub. Don’t change the “git@github.com” part. That’s supposed to be there.

    $ ssh -T git@github.comAttempts to ssh to github

    Which should give you this:

    The authenticity of host 'github.com (207.97.227.239)' can't be established.RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.Are you sure you want to continue connecting (yes/no)?

    Don’t worry, this is supposed to happen. Type “yes”.

    Hi username! You've successfully authenticated, but GitHub does not provide shell access.

    Having problems?

Then: Set Up Your Info

Now that you have Git set up and your SSH keys entered into GitHub, it’s time to configure your personal info.

  1. Set your username and email.

    Git tracks who makes each commit by checking the user’s name and email. In addition, we use this info to associate your commits with your GitHub account. To set these, enter the code below, replacing the name and email with your own. The name should be your actual name, not your GitHub username.

    $ git config --global user.name "Firstname Lastname"Sets the name of the user for all git instances on the system$ git config --global user.email "your_email@youremail.com"Sets the email of the user for all git instances on the system

    More about user info

  2. Set your GitHub token.

    Some tools connect to GitHub without SSH. To use these tools properly you need to find and configure your API Token.

    On the GitHub site Click “Account Settings” > Click “Account Admin.”

    Copy your API token

    At the command line run the following code, using your GitHub username and token in place of the ones shown.

    $ git config --global github.user usernameSets the GitHub username for all git instances on the system$ git config --global github.token 0123456789yourf0123456789tokenSets the GitHub token for all git instances on the system

    *Note* If you ever change your GitHub password, a new token will be created and will need to be updated.

Lastly: Celebrate

Congratulations, you now have Git and GitHub all set up! What do you want to do next?

接下来,建立一个repository

Create A Repo

If you’ve found yourself on this page, we’re assuming you’re brand new to Git and GitHub. This guide will walk you through the basics and explain a little bit about how everything works along the way.

First: Create A Repo

Every time you make a commit with Git, it is stored in a repository (a.k.a. “repo”). To put your project up on GitHub, you’ll need to have a GitHub repository for it to live in.

More about repositories

  1. Create a new repo

    Click New Repository.

    Click “New Repository

    Fill out the information on this page. When you’re done, click “Create Repository.”

    Fill in the info

    Congratulations! You have successfully created your first repo!

Next: Create a README for your repo.

While a README isn’t a required part of a GitHub repo, it is a good idea to have one. READMEs are a great place to describe your project or add some documentation such as how to install or use your project.

More about READMEs

  1. Create the README file

    In the prompt, type the following code:

    $ mkdir ~/Hello-WorldCreates a directory for your project called "Hello-World" in your user directory$ cd ~/Hello-WorldChanges the current working directory to your newly created directory$ git initSets up the necessary Git filesInitialized empty Git repository in /Users/your_user_directory/Hello-World/.git/$ touch READMECreates a file called “README” in your Hello-World directory

    Open the new README file found in your Hello-World directory in a text editor and add the text “Hello World!” When you are finished, save and close the file.

  2. Commit your README

    Now that you have your README set up, it’s time to commit it. A commit is essentially a snapshot of all the files in your project at a particular point in time. In the prompt, type the following code:

    More about commits

    $ git add READMEStages your README file, adding it to the list of files to be committed$ git commit -m 'first commit'Commits your files, adding the message "first commit"

    The code above executes actions locally, meaning you still haven’t done anything on GitHub yet. To connect your local repository to your GitHub account, you will need to set a remote for your repo and push your commits to it:

    More about remotes

    $ git remote add origin git@github.com:username/Hello-World.gitSets the origin for the Hello-World repo$ git push origin masterSends your commit to GitHub

    Now if you look at your repository on GitHub, you will see your README has been added to it.

    Your README has been created

Lastly: Celebrate

Congratulations! You have now created a repository on GitHub, created a README, committed it, and pushed it to GitHub. What do you want to do next? 

完成上面的设置,个人使用基本没什么问题了,以后完成后记得随时上传就是了 git push origin master

这只是个人使用,如果多个人的话,可以去官网看看帮助,我这里有个文档,也挺好的,共享一下: http://files.cnblogs.com/ma6174/git-tutor.pdf

在网上发现了一个很好的图片,把所有的命令都集中起来了,就和vi的一样 ,果断共享

 


博主ma6174对本博客文章(除转载的)享有版权,未经许可不得用于商业用途。转载请注明出处http://www.cnblogs.com/ma6174/

对文章有啥看法或建议,可以评论或发电子邮件到ma6174@163.com


本文转自ma6174博客园博客,原文链接:http://www.cnblogs.com/ma6174/archive/2012/02/18/2357140.html如需转载请自行联系原作者
相关文章
|
28天前
|
开发工具 git
使用Git根据日期进行代码版本切换的方法
通过以上步骤,可以有效地根据日期进行Git代码版本的切换。这种方法在需要回溯历史版本进行bug修复或功能复查时特别有用。Git的灵活性和强大功能使其成为现代软件开发不可或缺的工具之一。
246 103
|
2月前
|
开发工具 git
Git版本控制工具合并分支merge命令操作流程
通过以上步聚焦于技术性和操作层面指南(guidance), 可以有效管理项目版本控制(version control), 并促进团队协作(collaboration).
357 15
|
4月前
|
开发工具 git
使用Git下载指定版本或指定commit
使用Git下载指定版本或指定commit
|
7月前
|
人工智能 API 开发工具
GitHub官方开源MCP服务!GitHub MCP Server:无缝集成GitHub API,实现Git流程完全自动化
GitHub MCP Server是基于Model Context Protocol的服务器工具,提供与GitHub API的无缝集成,支持自动化处理问题、Pull Request和仓库管理等功能。
1432 2
GitHub官方开源MCP服务!GitHub MCP Server:无缝集成GitHub API,实现Git流程完全自动化
|
10月前
|
开发工具 git
如何操作github,gitee,gitcode三个git平台建立镜像仓库机制,这样便于维护项目只需要维护一个平台仓库地址的即可-优雅草央千澈
如何操作github,gitee,gitcode三个git平台建立镜像仓库机制,这样便于维护项目只需要维护一个平台仓库地址的即可-优雅草央千澈
607 69
如何操作github,gitee,gitcode三个git平台建立镜像仓库机制,这样便于维护项目只需要维护一个平台仓库地址的即可-优雅草央千澈
|
7月前
|
Linux 开发工具 git
版本控制工具:Git的安装和基本命令使用指南。
结束这段探险,掌握了Git你就等于掌握了一个宝藏,随时可以瞥见你的编程历程,轻松面对日后的挑战。Git,无疑是编程者的强大武器,开始你的Git探险之旅吧!
274 28
|
5月前
|
网络协议 开发工具 git
解决 git 报错 “fatal: unable to access ‘https://github.com/.../.git‘: Recv failure Connection was rese
在使用 Git/Git小乌龟 进行代码管理的过程中,经常会遇到各种各样的问题,其中之一就是在执行 git clone 或 git pull 等操作时出现 “fatal: unable to access ‘https://github.com/…/.git’: Recv failure Connection was reset” 的报错。这个问题通常是由网络连接问题或代理设置不正确导致的。在我的个人使用经验中,我亲自尝试了四种方法,它们都能够有效地解决这个报错。个人比较推荐方法二。
2630 0
|
7月前
|
文字识别 网络协议 开发工具
GitHub封锁?推荐5个国产的Git仓库替代平台
近日,GitHub对中国区IP的部分限制引发了广泛关注。未登录用户被拒,已登录用户功能受限,南北网络环境差异更显“内卷”。为应对这一挑战,本文推荐了多个国产Git平台:Gitee(码云)、GitCode(CSDN旗下)、CODING(腾讯系)、CodeUP(阿里云支持)及微信代码管理工具。这些平台功能全面、稳定性强,是开发者迁移项目的理想选择。通过同步代码、配置CI/CD流水线等简单步骤,可确保项目平稳过渡。此次事件提醒我们,掌握核心技能与支持国产平台同样重要!
4765 11
|
7月前
|
安全 Linux 网络安全
在Linux(CentOS和AWS)上安装更新的git2的方法并配置github-ssh
经过以上这些步骤,你现在就能在GitHub上顺利往返,如同海洋中的航海者自由驰骋。欢迎你加入码农的世界,享受这编程的乐趣吧!
294 10
|
8月前
|
安全 开发工具 Android开发
【Android Git】Git版本回退方式
在实际操作中,选择合适的版本回退方式,可以有效地管理代码版本,提高开发效率和代码质量。
463 26