uncategorized

git

  1. git docs
  2. git tutorials
  3. github help

basic

1. create a first repo

1
2
3
4
5
6
7
8
9
10
11
# create a dir
> mkdir dir && cd dir
# initialize a repo
> git init

# check repo status
> git status
# add new files and changes to stage area
> git add -A ./ # 当前目录下所有的文件与子目录,包含删除文件
# commit stage area data to local repo
> git commit -m "some change record"

2. branch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# check branch info
> git branch
# check branch detailed information
> git branch -vv

# create a repo branch
> git branch fixed
# switch to branch
> git checkout fixed

# one command to create and switch to a new branch
> git checkout -b fixed

# merge fixed branch to master branch,and delete fixed branch
> git checkout master
> git merge fixed
> git branch -d fixed

3. reset

HEAD表示当前版本(最新的提交),上一个版本就是HEAD^,上上一个版本就是HEAD^^,往上100个版本HEAD~100

1. 分支操作

1
2
3
4
# 文件会被还原
> git reset --hard HEAD^

> git reset --hard <commit id>

2. 文件操作

不修改文件内容,改变文件提交状态,可以取消git add操作

1
2
3
> git reset HEAD -- readme.txt<filename>

> git reset -- filename
  • You can unstage files by using the git reset <target>command
  • Files can be changed back to how they were at the last commit by using the command: git checkout -- <target>
  • git reset <target> 取消git add的操作效果,git checkout -- <target> 把文件恢复到上一次commit的状态

修改文件内容

1
> git checkout -- readme.txt<filename>

4. patch

1. create branch

1
2
3
4
5
6
# 创建新的branch
> git checkout -b fix-bug
# 修复,提交,测试后,check
> git log --pretty=oneline -3
# Extract all commits which are in the current branch but not in the master branch
> git format-patch maste --stdout > bug_fixed.patch

3. apply patch

1
2
3
4
5
6
7
8
9
10
11
12
# Apply patch, switch to master branch
> git checkout master
## look at what changes are in the patch
> git apply --stat bug_fixed.patch
## test the patch
> git apply --check bug_fixed.patch

# apply patch
## applies the patch but does not create a commit
> git apply bug_fixed.patch # 适合于引用外部开源软件(submodule),保留其开源版权
## create commits from patches generated by git-format-patch
> git am --signoff < bug_fixed.patch

5. submodule

1
2
3
4
5
6
7
8
9
10
11
# create a new submodule
> git submodule add https://github.com/chaconinc/DbConnector
## specify a path
> git submodule add https://github.com/chaconinc/DbConnector path

# update the submodules of a repo
> git submodule init
> git submodule update --init --recursive

# clone repo, and update submodule
git clone --recurse-submodules https://github.com/chaconinc/MainProject

6. remote

url format:

1
2
3
4
5
# public repo:
https://github.com/username/repository.git

# authorize, private repo
https://username:password@github.com/username/repository.git

1. clone

1
> git clone url

2. add/delete/rename remote

1
2
3
4
5
6
7
8
9
10
11
# verigy remote
> git remote -v

# add a new remote
> git remote add remote_name url

# Remove remote
> git remote rm remote_name

# rename remote
> git remote rename remote_name other

3. pull

1
2
3
4
5
6
7
8
#
> git pull origin master

##
# downloads the data to your local repository
> git fetch upstream
# 选择一个branch进行合并
> git merge upstream/master

git clonecommand automatically sets up your local master branch to track the remote master branch (or whatever the default branch is called) on the server you cloned from. Running git pull generally fetches data from the server you originally cloned from and automatically tries to merge it into the code you’re currently working on.

1
2
# it runs git rebase instead of git merge
> git pull --rebase origin(remote_name) master(remote_branch)

4. push

1
2
3
# Pushing  Remotely
# The -u tells Git to remember the parameters, so that next time we can simply run git push and Git will know what to do. Go ahead and push it!
> git push -u origin(remote_name) master(local_branch):remote_branch

5. Inspecting a Remote

1
2
# git remote show <remote>
> git remote show origin
Share