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

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

1
2
3
4
5
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.

1
ssh -T git@github.com

git remote: Connecting with git remotes

1
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:

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

1
2
### 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.

1
2
3
4
5
6
7
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 git log --pretty=oneline --abbrev-commit. 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 git log --pretty=oneline --abbrev-commit

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

1
2
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.

1
2
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## 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 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.

1
2
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.

1
2
3
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

1
2
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》

文章目录
  1. 1. Bioinformatics data skill book charpter 5
    1. 1.0.1. git add
    2. 1.0.2. git commit
    3. 1.0.3. git diff: Seeing File Differences
    4. 1.0.4. git log: Seeing the commit history
    5. 1.0.5. git mv, git rm : Moving and Removing Files
    6. 1.0.6. .gitignore tell git what to ignore.
    7. 1.0.7. git reset: undoing a stage
    8. 1.0.8. git restore: discard changes and restore
  2. 1.1. Collaborating with Git by using Github.
    1. 1.1.1. git remote: Connecting with git remotes
    2. 1.1.2. git push Pushing commits to a remote repository.
    3. 1.1.3. git pull
    4. 1.1.4. Merge Conflicts
    5. 1.1.5. git shortlog
  3. 1.2. Working with Past Commits
    1. 1.2.1. git checkout: Getting files from the past
    2. 1.2.2. git stash: Stashing Your Changes
    3. 1.2.3. git diff: Comparing the Commits and Files.
    4. 1.2.4. git commit –amend: Undoing and Editing commits.撤回commit
  4. 1.3. Working with Branches
    1. 1.3.1. git branch, git checkout: Creating and Woring with Branches.
    2. 1.3.2. git merge: Merging Branches
    3. 1.3.3. Branches and Remotes
|