Learning Git

Git is a free & open source,distributed version control systemThe basic Git workflow goes something like this:

1) You modify files in your working directory.

2) You stage the files, adding snapshots of them to your staging area.

3) You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.



Set Up SSH Keys

$ cd ~/.ssh

$ ls
known_hosts

$ ssh-keygen -t rsa -C "bambreeze@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/bambreeze/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/bambreeze/.ssh/id_rsa.
Your public key has been saved in /home/bambreeze/.ssh/id_rsa.pub.
The key fingerprint is:
...
The key's randomart image is:
...

$ ls
id_rsa  id_rsa.pub  known_hosts

$ vim id_rsa.pub

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

$ ssh -T git@github.com
Hi bambreeze! You've successfully authenticated, but GitHub does not provide shell access.

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.Set yourusername andemailGit tracks who makes each commit by checking the user’s name and email. 

In addition, we use this info to associate your commits with yourGitHub 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 user name.

$ git config --global user.name "bambreeze"
$ git config --global user.email bambreeze@gmail.com
$ git config --global github.user bambreeze

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.”

$ git config --global github.token <your_token>

Create A Repository @ GitHub

ldd3$ git init
Initialized empty Git repository in /home/bambreeze/workspace/git-space/ldd3/.git/

<Add some files…>

ldd3$ git add .

ldd3$ git commit -m "initial commit for Linux Device Driver (3rd) examples"
Created initial commit ec142ce: initial commit for Linux Device Driver (3rd) examples
 99 files changed, 13387 insertions(+), 0 deletions(-)
...

ldd3$ git status
# On branch master
nothing to commit (working directory clean)

ldd3$ touch README

ldd3$ vim README

ldd3$ git add README

ldd3$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   README
#

ldd3$ git commit -m "adding README file for LDD3-Examples project"
Created commit 40db767: adding README file for LDD3-Examples project
 1 files changed, 2 insertions(+), 0 deletions(-)
 create mode 100644 README

ldd3$ git remote add origin git@github.com:bambreeze/LDD3-Examples.git

ldd3$ git push origin master

Counting objects: 123, done.
Compressing objects: 100% (121/121), done.
Writing objects: 100% (123/123), 100.69 KiB, done.
Total 123 (delta 46), reused 0 (delta 0)
To git@github.com:bambreeze/LDD3-Examples.git
 * [new branch]      master -> master

Linux Kernel Source of Linus's Tree

$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git linux

$ cd linux/

$ git pull

Creating and Commiting

$ cd (project-directory)
$ git init
$ (add some files)
$ git add .
$ git commit -m 'Initial commit'

Cloning and Creating a Patch

$ git clone git://github.com/git/hello-world.git
$ cd hello-world
$ (edit files)
$ git add (files)
$ git commit -m 'Explain what I changed'
$ git format-patch origin/master

Basic commands

(1) Show the working tree status
$ git status

(2) Commit codes & Skipping the Staging Area
$ git commit -am 'your commit message'

(2) Show commit logs
$ git log

(4) Fetch from and merge with another repository or a local branch
$ git pull

(5) Update remote refs along with associated objects
$ git push origin master

Viewing Changes

(1) Changed but not yet staged
$ git diff
$ git diff <file>

(2) Staged that will go into your next commit
$ git diff --cached
$ git diff --cached <file>

(3) Compare two different version
$ git diff <first_commit_id> <second_commit_id>
$ git diff <first_commit_id>:<file> <second_commit_id>:<file>
$ git diff c3f0 fdcb

Check History Version

(1) List the history version
$ git log --pretty=oneline pci.txt
$ git log pci.txt

(2) Export the old version
$ git show a37bee7956ab34785815aafbe9f920c2cfd02b97:./pci.txt > pci_old.txt

(3) 查看某个历史版本

首先,使用 git log 查询历史版本的 SHA HASH
然后,使用 git checkout <sha hash>,就可以回到过去的某个历史版本。
最后,使用 git checkout <branch>,可以回到最新的状态。

$ git log
$ git checkout d1d93bd
$ git status
# Not currently on any branch.
nothing to commit (working directory clean)
$ git checkout home
$ git status
# On branch home
nothing to commit (working directory clean)

(4) checkout某个文件的历史版本
首先,使用 git log <file> 查询该文件的历史版本的 SHA HASH
然后,使用 git checkout <sha hash> -- <file>,就可以回到过去的某个历史版本。
最后,使用 git reset HEAD <file> 以及 git checkout -- <file>,可以回到最新的状态。
$ git log test-nouse.c
$ git checkout 86bec8c3643a57 -- test-nouse.c 
$ git status

# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: test-nouse.c
#
$ git reset HEAD test-nouse.c 
$ git checkout -- test-nouse.c 
$ git status

# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#

nothing to commit (working directory clean)

Git 撤销 commit

$ git  reset --hard <commit_id>

根据–soft –mixed –hard,会对working tree和index和HEAD进行重置:
git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息
git reset –soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可
git reset –hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容

Setup Git Server

1. 服务器上创建一个git用户,用来运行git服务。同时,禁用git用户shell登录。

$ sudo adduser git

$ sudo  vim /etc/passwd

...
git:x:1000:1000:,,,:/home/git:/usr/bin/git-shell

客户端可以运行ssh-keygen命令来生成 SSH 公钥。

然后,将所有用户的公钥的内容导入到服务器的 /home/git/.ssh/authorized_keys 文件里。

2. 服务器上初始化Git仓库

$ cd /home/pi/workspace/repos
$ sudo git init --bare sample.git
$ sudo chown -R git:git sample.git

3. 客户端 clone 仓库,并提交代码到服务器

$ git clone git@192.168.2.110:/home/pi/workspace/repos/sample.git
$ cd sample/
$ vim README
$ git add .
$ git commit -am 'Initial commit'
$ git push origin master

Reference

1. http://git-scm.com/
2. http://progit.org/book/
3. https://github.com/

4. Git 学习笔记

5. Git撤销commit

6. Ubuntu搭建Git服务器

7. 搭建Git服务器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值