学习《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.
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
.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
).
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.
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:
- Use
git status
to find the conflicting files. - Open and edit those files manually to a version that fixes the confict.
- Use
git add
to tell Git that you've solved the conflict in a particular file. - 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
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
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.
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
《Bioinformatics Data Skill》