Git_版本控制的项目流程管理

学习《Bioinformatics Data Skill》 Charpter 5-Using Git to Make Life Easier: Git for Scientists"

[TOC]

Bioinformatics data skill book charpter 5

working with git is fundamentally about creating and manipulating the commits(snapshots):

  • creating commits
  • looking at past commits
  • sharing commits
  • comparing different commits

we can create a git repository through command git init

git add

then we can track files we want through command git add README data/README: TWO roles of git add:

  • Alerting Git to start tracking untracked files (this also stages the current version of the file to be included in the next commit)
  • Staging changes made to an already tracked file (staged changes will be included in the next commit)

we can check the status of the git repository git status

A staged file is not only tracked, but its latest changes are staged to be included in the next commit

git commit

git commit -m "initial import" This command commits your staged changes to your repository with the commit message “initial import.” Commit messages are notes to your collaborators (and yourself in the future) about what a particular commit includes

git diff: Seeing File Differences

shows the differences between the files in your working directory and what's been staged.

  • added lines will be green and deleted lines will be red.
  • plus+ indicate a line addition, negative indicate deletion.

after we stage a filegit add README, we can commit.If we wanted to compare what’s been staged to our last commit. git diff --staged

git log: Seeing the commit history

After we commit what we did to the README file, we can use the git log to visualize our chain of commits.git commit -a -m "added infomation about project to README" and then git log, we will see the commit changes to our repository.

image

git mv, git rm : Moving and Removing Files

To move or remove tracked files in Git, we need to use Git’s version of mv and rm: git mv and git rm. git mv, git rm

image

.gitignore tell git what to ignore.

because git status keeps listing files which are not tracked, we use .gitignore file to list the untracked files/directories.echo >.gitignore and git add .gitstatus.

What we should tell .gitignore to ignore? these can be solved by using a global .gitignore file to universally ignoregit config --global core.excludesfile ~/.gitignore_global

  • large files: fastq
  • Intermediate fiels
  • Text editor temporary files
  • Temporary code files

git reset: undoing a stage

If you accidentally stage a messy file for a commit with git add, you can unstage it with git reset

echo "TODO: ask sequencing center about adapters" >> README.md
git add README.md
git status
git reset HEAD README.md
git status

git restore: discard changes and restore


Collaborating with Git by using Github.

Collaborate with git, which at its core is just about sharing commits between you and your collaborators' repositories. The basis of sharing commits in Git is the idea of a remote repository(eg. Github). we can retrieve commits from a remote repository (git pull) and send commits to a remote repository (git push).

image

After we create a reporitory, we can use SSH keys to authenticate youself. the SSH keys can avoid having to enter password each time.
Type cat ~/.ssh/id_rsa.pub to view your SSH keys and copy it to the Github account settings.

ssh -T git@github.com

git remote: Connecting with git remotes

git remote add origin git@github.com:wang-tianpeng/git_test.git

this command enables us to specify the address of git reporitory and the remote name for it origin.you can add other remote repositories with different names by git remote add. We can type git remote -v to view the existing remote name. If we need to delete an unused remote repository, we can usegit remote rm

git push Pushing commits to a remote repository.

Let’s push our initial commits from zmays-snps into our remote repository on Git‐Hub. The subcommand we use here is git push <remote-name> <branch>. Thus, to push our zmays-snps repository's commit. we can type:

git push origin master

origin means your name of Github remote repository. master means your curent default branch name master.

image

git pull

Collaboration on Git is a back-and-forth exchange, where one person
pushes their latest commits to the remote repository, other collaborators pull changes into their local repositories, make their own changes and commits, and then push these commits to the central repository for others to see and work with.

### we can first mimic(mimicking) a collaborator's version of the project
git clone git@github.com:wang-tianpeng/git_test.git zmays-snps-barbara

First, we can make a commit to our local repository.

echo "Samples expected from sequencing core 2013-01-10" >> README.md

git commit -a -m "added information about samples"
git push origin master

### Now in zmays-snps-barbara directory
git pull origin master

So finally, we can verify that collaborator's repository contains the most recent commit using git log or even fancy command <font color=red>git log --pretty=oneline --abbrev-commit</font>. The tips for git collaborating: Pull often.

Merge Conflicts

Merge conflicts occur when Git can’t automatically merge your repository with the commits from the latest pull. The strategy to solve them is always the same:

  1. Use git status to find the conflicting files.
  2. Open and edit those files manually to a version that fixes the confict.
  3. Use git add to tell Git that you've solved the conflict in a particular file.
  4. Once all conflicts are resolved, use git status to check that all changes are staged. Then, commit the resolved versions of the conflicting file(s).

git shortlog


Working with Past Commits

git checkout: Getting files from the past

If you accidentially overwrite the current version of README.md, we can see this change with git status, We can restore this file by checking out the version in our last commit(HEAD points) with command git checkout -- <file> or git restore <file>

we can restore the README to any version of history with command git checkout HEAD~<NUM> README
or with the IDs of history version <font color=red>git log --pretty=oneline --abbrev-commit</font>

git stash: Stashing Your Changes

git stash is handy when we want to save our messy, partial progress before operations that are best performed with a clean working directory.

Stashing our working changes sets our directory to the same state it was in at the last
commit git stash and git stash pop

git diff: Comparing the Commits and Files.

We can use git diff to compare our current working tree to other commits and differences between Two arbitary commits. if we wanted to compare what we have now (at HEAD) to commit dafce75

git diff dafce75
git diff HEAD~1
image

git commit --amend: Undoing and Editing commits.撤回commit

If we were to make a mistake in a commit message, thene we could amend our commit with.git commit --amend. Amending commits isn’t limited to just changing the commit message though. You can make changes to your file, stage them, and then amend these staged changes with git commit --amend.

git commit -a -m "added adpters file to readme"
git commit --amend

Working with Branches

Git’s branches are virtual, meaning that branching doesn’t require actually copying files in your repository. You can create, merge, and share branches effortlessly with Git. Git branches can be helpful in three ways:

  • Branches allow you to experiment in your project without the risk of adversely affecting the main branch, master. We can create new branch and implement the changes while the master branch unaffected.
  • We can develop new features or bug fixes without affecting master branch. We can then merge the completed branches into the master branch.
  • branches simplify working collaboratively on repositories. Collaborator can work on their own separate branches.

git branch, git checkout: Creating and Woring with Branches.

we can create a New git branch with command git branch, and switch to the branch with command git checkout. We can see all the branches and these last commits with command git log --abbrev-commit --pretty=oneline --graph --branches -n2

## Creating New branch
git branch readme-changes
git branch

## switch to new branch
git branch readme-changes
git branch

### edit anything on the README.md and commit
~~~~~~~
git commit -a -m "reformatted readme, added sample info"

### switch back to the master branch.
git checkout master
### check the README.md which swaps out to the version on the master.


### suppose we do sth on the master branch
git branch
echo ">adapter-1\\nGATGATCATTCAGCGACTACGATCG" >> adapters.fa
git add adapter.fa
git commit -a -m "added adapters file"

### after we switch to the branch, we will only see the branch-commited files.

git merge: Merging Branches

First, use git checkout to switch to the branch we want to merge the other branch into. Then, use git merge <otherbranch> to merge the other branch into the current branch. if we want to merge the readme-changes branch to the master branch, we can switch to the master first, and merge with git merge readme-changes. we will find that the README.md file on the master branch was replaced by the README.md file from the branch.

git merge readme-changes
git log --abbrev-commit --pretty=oneline --graph --branches -n 3
image

Branches and Remotes

If we want to see how Git's branches and remote repositories are related, we can share our local branches with collaborators. we can git branch --all to show the hidden remote branches.

image

git push origin new-methods
git fetch origin
git branch --all

git pull is a command combined with git fetch and git merge. So, if we want to see all the branches from the collaborators, we can use command

git fetch origin
git branch --all

git fetch doesn’t change any of your local branches; rather, it just synchronizes your
remote branches with the newest commits from the remote repositories. If we want to delete the local branch with command git branch -d new-methods

参考资料:https://oschina.gitee.io/learn-git-branching/

《Bioinformatics Data Skill》

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 193,968评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,682评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,254评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,074评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,964评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,055评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,484评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,170评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,433评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,512评论 2 308
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,296评论 1 325
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,184评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,545评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,150评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,437评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,630评论 2 335

推荐阅读更多精彩内容