Git

From Colettapedia
Jump to navigation Jump to search

References

Key Concepts

  • default branch name is main, other branches can be named whatever you want
  • Repos: origin & upstream
  • Together repos and branch names are concatenated with a '/' to identify the head of that branch
  • Vocab: Pull Request
  • HEAD = name for the currently checked out commit with branch
    • detached HEAD = being on an unnamed branch
  • feature branch
  • dirty checkin = doesn't fix one thing and one thing only

Github

Step 1: Fork Someone Else's Repo (THEIRS) into your Github account (YOURS), and clone locally (LOCAL)

  1. Fork THEIRS on Github. Now YOURS fork exists on your Github account.
  2. Clone YOURS on your LOCAL machine - "When a repository is cloned LOCALly, it has a default remote called origin that points to YOURS on GitHub, not the original repository it was forked from (THEIRS)"
  3. Configure remotes - To keep track of THEIRS, you need to add another remote named upstream: git remote add upstream https://github.com/octocat/Spoon-Knife.git

Step 2: Absorb Latest Changes from THEIRS Repo in LOCAL, then push to YOURS

  1. git fetch upstream - THEIRS repo now appears in git branch -r as upstream/master
  2. git checkout master - or whatever branch you want to absorb the upstream changes
  3. Try git rebase upstream/master if your branch can be "fast-forwarded"
  4. If that doesn't work, you might have to merge it using git merge
  5. git push - pushes changes from LOCAL to YOURS on Github

Step 3: Fixing a Bug in THEIRS via Github Pull Request

  1. Report the bug on Github issues. Github will assign the bug report an issue#. You'll want to reference that number in your commit messages, because when you're browsing commits on Github.com, #69 will get converted to a hyperlink to the github bug report for Issue 69 for example.
  2. Start in LOCAL master branch. Make sure:
    1. You've git pulled all the latest from YOURS
    2. You've absorbed changes from THEIRS into LOCAL
  3. git checkout -b issue69 - Create the issue fix branch on LOCAL
  4. Fix the issue
  5. git commit -m "Fixes Issue #69" - Mention the issue number
  6. git push --set-upstream origin issue69 - Pushes LOCAL branch issue69 to github, Now, YOURS repo has a branch issue69
  7. On github, do a pull request from YOURS/issue69 branch to THEIRS/master
  8. The administrator of THEIRS will review the pull request and merge if it's ok.
  9. Delete YOURS/issue69 on github
  10. Locally, git checkout master
  11. git branch -d issue69 - delete the branch LOCAL/issue69
  12. Repeat Step 2 as above

Troubleshooting push origin master

  1. Edit $REPO_HOME/.git/config and make sure [remote "origin"] url = ssh://git@github.com/colettace/omero-pychrm.git or whatever
  2. cd .ssh; ssh-keygen -t rsa -C "your_email@example.com"
  3. Paste the public key into github
  4. ssh -T git@github.com to see if it works
  5. git push

Tips

  • In practice, using the github workflow of THEIRS YOURS and LOCAL means you'll never merge a LOCAL branch to LOCAL/master. LOCAL/master is merged/rebased from THEIRS/master. YOURS/master is the last to be to fully updated because you push from LOCAL/master to YOURS/master

Github apps

Merge feature branch from other user to main repo

  1. From your project repository, check out a new branch and test the changes.
  2. Merge the changes and update on GitHub.
    • git checkout master
    • git merge --no-ff colettace-autoreconfed
      • --no-ff = no fast forward. Useful for merging feature branches. Merge the changes thing as a single unit.
    • Can also squash your own commits on your feature branch before merging using interactive rebase git rebase -i HEAD~N, where N is number of commits you want to squash.
      • git rebase --abort - if you messed up
    • git push origin master

Daily usage

  • git fetch vs git pull
  • git submodule update

Initialize

  • Introduce yourself to git with your name and public email before doing any operation
git config --global user.name "Christopher Edmund Coletta"
git config --global user.email "christopher.coletta@nih.gov"
git config --global core.filemode true
  • Password credentials/caching
    • Linux: git config --global credential.helper 'cache --timeout=3600' - cache your password for an hour

Importing a New Project

  1. git init - create an empty git repository in the working directory
  2. git add . - Add files to index, i.e., take snapshot of contents of all files in current directory
  3. git commit, then type in message, or use -m "log message" to do it on the command line.
  4. git commit -a -m "Add any files with tracked changes to the index then commit" <- Message says it all
    • N.B., does not include untracked files, must use git add to put them into the index

Making Changes

  • index - the staging area for a group of changes to be committed at once
  • --cached - a modifier you use with other git commands to indicate you want to work with what's in the index, like diff, rm, etc
  • git add file1 file2 file3
    • git add is way different than svn - add is add the changes to the index, THEN commit
    • git add -A stages All
    • git add . stages new and modified, without deleted
    • git add -u stages modified and deleted, without new
  • git diff compare changed files against the index
  • git diff HEAD <file> - compare the head with what's in the index
  • git diff HEAD^ HEAD - get diff of previous commit
  • git diff --cached - see what is about to be committed, includes stuff you just added to index
  • git status - What's changed relative to head & what's in the index
  • git log --stat - recommended. Can also use git log -p to show complete diffs at each commit
    • alias gl='git log --graph --abbrev-commit --pretty=oneline --decorate' - command line branching graph
    • git config --global alias.adog "log --all --decorate --oneline --graph"
  • git rm --cached the.file - remove a file from the index (tell git to no longer track the file, but keep the local copy)

Managing Branches

  • git branch - list all branches
    • will always show a "master" branch created for me
    • asterisk shows which branch you're currently on
  • git branch -r - show all remote tracking branches, including ones from your origin repo (on Github) and your upstream repo (the one you forked from on Github)
  • git branch -a - Shows local AND remote branches
  • git branch newbranchname - create a new branch
  • git checkout experimental - switch branches
  • git checkout -b new_branch - create branch and checkout
  • git checkout master; git merge newbranchname - merge branches
  • git diff to show conflicts, which should be resolved by editing files
  • gitk - show graphical representation of resulting history
  • git branch -d newbranchname - delete branch
    • checks to make sure changes in branch made it to master
  • git branch -D crazy-idea - delete the shit out of a branch
    • forget any changes made in branch
  • Create a branch from unstaged/uncommited changes on master
git checkout -b new_branch
git commit -a -m"edited"
git checkout master
git status
  • git branch branchname <sha1-of-commit> - branch from a previous commit
  • git push --set-upstream origin <newbranchname> - push new branch to github
  • git push origin --delete <branchname> - delete github branch

Resolving Conflicts

  • Problem I had:
Failed to merge in the changes.
Patch failed at 0001 checkpoint 1. Starting to unify FeatureSets
The copy of the patch that failed is found in:
   /Users/chris/src/wnd-charm/.git/rebase-apply/patch

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

Using git for collaboration

  • git clone /path/to/orig_git_repo - clone from repo on your filesystem
  • git clone https://github.com/wnd-charm/wnd-charm.git - clone from a Github Repo
  • git fetch - fetch new commits from remote servers, which go to remote tracking branches -- a staging area for these commits for diff'ing, merging etc.
  • Can also specify repo to fetch from: git remote add bob /path/to/bobs/repo, then git fetch bob.
  • git log -p master..bob/master available after you set up remote
  • git merge bob/master is the same as git pull . remotes/bob/master
    • WARNING! git merge merges COMMITS not code, so the incoming code
  • git pull - pull changes from upstream repo. Have your local changes committed before you pull.
  • git fetch - safe even if i have local changes
  • git log -p HEAD..FETCH_HEAD - HEAD and FETCH_HEAD are special reserved tags ".." is called range notation.
    • gitk HEAD..FETCH_HEAD two-dot form
    • gitk HEAD...FETCH_HEAD three-dot form

Exploring History

  • git log
  • git show <some token> = get details
  • git tag <you tag string> <token> ... examples:
    • git tag v2.5 1b2e1d63ff
    • git diff v2.5 HEAD
    • git branch stable v2.5

Throwing Away Changes/Cleaning Stuff Up

  • git reset --hard - gets you back to the head of the local repo
  • git reset --hard commit_sha - Undo any commits to local repo, including merge
  • git checkout -- the_file - throw away changes relative to the index
  • git checkout HEAD the_file - throw away changes relative to the committed versions
  • git reset --hard HEAD^ - reset your current branch and working directory to its state at HEAD^
  • git revert - undo changes i pushed
  • git show --pretty="format:" --name-only bd61ad98 - list all the files for a commit in git

Restoring files deleted from previous commit

  • git rev-list -n 1 HEAD -- <file_path>
    • this gets the SHA1 tag for the commit that removed the file
  • git checkout <deleting_commit>^ -- <file_path>
    • Then checkout the version at the commit before.
    • the carot^ means the commit before the given tag

Cleaning up files not under version control

  • git clean -f - remove all files not under version control
  • git clean -f -d - remove all files and directories not under version control
  • git clean -n - Dry-run, show what would be deleted
  • git clean -f -x - remove ignored as well as non-ignored files

Undo a commit

  • git reset HEAD^ - the "mixed reset" ... moves HEAD back one, but doesn't throw away the changes. Good if you checked in code that was actually meant for another branch

Undo last push to github

  • git push -f origin HEAD^:master - undo last push
  • git push -f origin HEAD^^:master - undo last 2 pushes, works n times

Amend your last commit

  • Add changes to the index and type git commit --amend
    • Great if you forgot to add untracked files, or you want to change the commit message

Working with files in the repo/repo dir

  • git ls-files --other
  • git grep "hello" - search for in all versions of all files under version control
    • git grep -i pychrm - case insensitive search (just like regular grep)
  • git ls-tree -r master --name-only - list all tracked files

Merge vs. Rebase

  • Rebase
    • Take all the changes that were committed on one branch and replay them on another one.
    • Can delete the .git/rebase-merge directory to eliminate a failed rebase operation
  • Merge
    • Generate a patch which is the diff of that branch and this and apply it here, instead of trying to retrofit my changes onto that branch
  • In sum: In sum: "Now, the snapshot pointed to by C3' is exactly the same as the one that was pointed to by C5 in the merge example. There is no difference in the end product of the integration, but rebasing makes for a cleaner history. If you examine the log of a rebased branch, it looks like a linear history: it appears that all the work happened in series, even when it originally happened in parallel."

git stash

  • git stash save
  • git stash pop
  • git stash list
  • git stash clear
  • git stash drop - drops the top stash
  • git stash show -p - show a diff between top of stack stash and it's parent (not with changes you have now)

gitignore

  • git clean -ndX - show ignored files
  • git clean -fdX - delete ignored files

git submodule

  • git submodule update --init - what you have to do if you forget to git clone --recursive
  • git submodule update


Github presentation

  • Github Guides
  • Clarification:
    • Git - Version control tool that GitHub is build on top of (Open source software)
    • GitHub - A private company that provides no-cost websites and software to help you interact with Git in a nice way.
    • GitHub.com - The website you log into to view the repositories online
    • GitHub Desktop An Application to help you synchronize local code with GitHub.com
  • You're gonna hear this word "workflow" a lot. manages workflow, collaborative and asynchronous workflow. How people collaborate on projects broken down in a very systematic way.
  • A platform for hosting and collaborating on projects with other people, and/or across multiple computers.
  • Work locally then "push your changes" to the web, sync from anywhere
  • Largest code host on the planet with 31 million repositories, rich ecosystem where everyone is working simultaneously on everyone else's project.
  • Issue tracking - Helps organize reporting of bugs/requesting of new features in the software. many of you have filed a help desk ticket, there's back and forth, "kind of like email, except they can be shared and discussed with the rest of the team." milestone, assignee, labels
  • code review - "Pull request" - The discussion is captured/recorded understand why and how a decision is made. Discussion can include screenshots, in the case of code copy/pasted output. Every change gets tracked, you know who did it, when it happened, you know exactly who said what in discussions about every line item of change proposed.
    • Can't help but think how much better off we'd be if the US Congress drafted bills this way.
    • See changes side by side
  • Helps organize people within organizations
  • read, read-write, admin
  • Social media-type features
    • @mention system, mention the whole team or just specific people, notification, references
    • activity shows up on your github page.
    • be friends with other developers, follow what they're doing, follow other projects, see what projects are trending.
  • Smart about text entry/formatting - using "Markdown" prettifies your text document, creates links easily
  • Make code citable using a Digital Object Identifier (DOI, used for academic reference and metrics system). If a script is part of your research, archive one of your github repositories and assign a DOI using data archiving tool Zenodo

Github Key concepts

  • Team
  • Projects
  • published repositories:
    • Public repository
    • Private repository - Requires Github subscription (costs $$)
  • You have it in two places: This is a key divergence . When we're talking about code, we not just talking about the text files that is the code. If it's a python script, you need the python interpreter install, If it's a C program you need to compile. That's the thing that happens locally.
    1. Remote repository - You can send people links, but it's not a development environment. You can't run your code on github.com
    • Local repository - if you were running code
  • Fork the repo then clone it locally
  • Sync changes from other repos: Push your new commits and pull commits from others.
  • Branch helps manage workflow where two or more people working on different parts of the project. features, enhancements
  • Master
  • Transparent history - other people can see what you're doing
  • Commits - Anytime you reach a goal or want to save your progress. any change, add delete modify a file. Each commit has a message. Change set - Like a stapler, put a bunch of changes together and commit them at once.
  • Easy roll back to a previous version
  • Markdown -
    • Fork and Pull model - you're working on somebody elses repository. Decision is made by maintainer whether to accept your changes or not.
    • Shared Repository - you're working on your own repo and you can merge anytime you want, but it's say to your fellow team members hey, I think I'm done with this project, can you take a look at it ("code review")
  • "Deploy" - software specific, put it out there in the real world and let it run watchfully
  • merge
  • owner, maintainer/collaborator, contributor, community member

Github Project quickstart

  1. On server generate ssh key
    1. ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    2. eval "$(ssh-agent -s)". SSH agent must be running in the background in the same terminal window that you'll be executing git commands that interact with github.
    3. ssh-add ~/.ssh/id_rsa.
  2. add sshkey to the agent
  3. Add key to github account
  • Create repo in github