Git
Jump to navigation
Jump to search
Troubleshooting
Contents
- 1 References
- 2 Key Concepts
- 3 Github
- 3.1 Step 1: Fork Someone Else's Repo (THEIRS) into your Github account (YOURS), and clone locally (LOCAL)
- 3.2 Step 2: Absorb Latest Changes from THEIRS Repo in LOCAL, then push to YOURS
- 3.3 Step 3: Fixing a Bug in THEIRS via Github Pull Request
- 3.4 Troubleshooting push origin master
- 3.5 Tips
- 3.6 Github apps
- 3.7 Merge feature branch from other user to main repo
- 4 Daily usage
- 5 Initialize
- 6 Importing a New Project
- 7 Making Changes
- 8 Managing Branches
- 9 Using git for collaboration
- 10 Exploring History
- 11 Throwing Away Changes/Cleaning Stuff Up
- 12 Working with files in the repo/repo dir
- 13 Merge vs. Rebase
- 14 git stash
- 15 gitignore
- 16 git submodule
- 17 Github presentation
- 18 Github Project quickstart
References
- cheat sheet
- On Undoing, Fixing, or Removing Commits in Git
- Nice tutorial site with small lessons/examples
- 10 things I hate about Git
- Github/Campfire Emoji Cheatsheet
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)
- Fork THEIRS on Github. Now YOURS fork exists on your Github account.
- 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)" - 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
git fetch upstream
- THEIRS repo now appears ingit branch -r
asupstream/master
git checkout master
- or whatever branch you want to absorb the upstream changes- Try
git rebase upstream/master
if your branch can be "fast-forwarded" - If that doesn't work, you might have to merge it using
git merge
git push
- pushes changes from LOCAL to YOURS on Github
Step 3: Fixing a Bug in THEIRS via Github Pull Request
- 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.
- Start in LOCAL master branch. Make sure:
- You've
git pull
ed all the latest from YOURS - You've absorbed changes from THEIRS into LOCAL
- You've
git checkout -b issue69
- Create the issue fix branch on LOCAL- Fix the issue
git commit -m "Fixes Issue #69"
- Mention the issue numbergit push --set-upstream origin issue69
- Pushes LOCAL branch issue69 to github, Now, YOURS repo has a branch issue69- On github, do a pull request from YOURS/issue69 branch to THEIRS/master
- The administrator of THEIRS will review the pull request and merge if it's ok.
- Delete YOURS/issue69 on github
- Locally,
git checkout master
git branch -d issue69
- delete the branch LOCAL/issue69- Repeat Step 2 as above
Troubleshooting push origin master
- Edit $REPO_HOME/.git/config and make sure [remote "origin"] url = ssh://git@github.com/colettace/omero-pychrm.git or whatever
cd .ssh; ssh-keygen -t rsa -C "your_email@example.com"
- Paste the public key into github
ssh -T git@github.com
to see if it worksgit 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
- Gist - paste code and snippets
- Github pages - websites by pushing your repo
Merge feature branch from other user to main repo
- From your project repository, check out a new branch and test the changes.
git clone https://github.com/wnd-charm/wnd-charm.git wnd-charm_main-repo
git checkout -b colettace-autoreconfed master
git pull https://github.com/colettace/wnd-charm.git autoreconfed
- If any merge conflicts, resolve using
git mergetool
, thengit commit
to conclude merge. git pull -Xtheirs
orgit merge --strategy-option theirs
- 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
- Linux:
Importing a New Project
git init
- create an empty git repository in the working directorygit add .
- Add files to index, i.e., take snapshot of contents of all files in current directorygit commit
, then type in message, or use -m "log message" to do it on the command line.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, etcgit add file1 file2 file3
git add
is way different than svn - add is add the changes to the index, THEN commitgit add -A
stages Allgit add .
stages new and modified, without deletedgit add -u
stages modified and deleted, without new
git diff
compare changed files against the indexgit diff HEAD <file>
- compare the head with what's in the indexgit diff HEAD^ HEAD
- get diff of previous commitgit diff --cached
- see what is about to be committed, includes stuff you just added to indexgit status
- What's changed relative to head & what's in the indexgit log --stat
- recommended. Can also usegit log -p
to show complete diffs at each commitalias gl='git log --graph --abbrev-commit --pretty=oneline --decorate'
- command line branching graphgit 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 branchesgit branch newbranchname
- create a new branchgit checkout experimental
- switch branchesgit checkout -b new_branch
- create branch and checkoutgit checkout master; git merge newbranchname
- merge branchesgit diff
to show conflicts, which should be resolved by editing filesgitk
- show graphical representation of resulting historygit 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 commitgit push --set-upstream origin <newbranchname>
- push new branch to githubgit 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 filesystemgit clone https://github.com/wnd-charm/wnd-charm.git
- clone from a Github Repogit 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
, thengit fetch bob
. git log -p master..bob/master
available after you set up remotegit merge bob/master
is the same asgit pull . remotes/bob/master
- WARNING!
git merge
merges COMMITS not code, so the incoming code
- WARNING!
git pull
- pull changes from upstream repo. Have your local changes committed before you pull.git fetch
- safe even if i have local changesgit log -p HEAD..FETCH_HEAD
- HEAD and FETCH_HEAD are special reserved tags ".." is called range notation.gitk HEAD..FETCH_HEAD
two-dot formgitk 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 repogit reset --hard commit_sha
- Undo any commits to local repo, including mergegit checkout -- the_file
- throw away changes relative to the indexgit checkout HEAD the_file
- throw away changes relative to the committed versionsgit reset --hard HEAD^
- reset your current branch and working directory to its state at HEAD^git revert
- undo changes i pushedgit 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 controlgit clean -f -d
- remove all files and directories not under version controlgit clean -n
- Dry-run, show what would be deletedgit 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 pushgit 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 controlgit 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 stashgit 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 filesgit clean -fdX
- delete ignored files
git submodule
git submodule update --init
- what you have to do if you forget togit 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.
- 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
- On server generate ssh key
- ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- 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.
- ssh-add ~/.ssh/id_rsa.
- add sshkey to the agent
- Add key to github account
- Create repo in github