Working with git

From Einstein Toolkit Documentation
Revision as of 06:22, 25 November 2011 by 137.43.60.79 (talk) (Update from a repository, if there are no local uncommitted changes:)
Jump to: navigation, search

Interacting with a remote repository

This page describes solution to typical needs with git. If you are new to git, please read any of the fine tutorials to learn about the concepts that git offers, and how one would typically work with git.

Later, if you get stuck, or if you maneuvered yourself into a corner, you can come back here to find solutions to particular problems. That is, this page is not organised by "how do I do thing that are natural for git", but rather "how do I map things I need to do onto actions that are natural for git".

This page is split into three parts:

  • Interacting with a remote repository (pull, push, merge)
  • Local commits
  • Working (adding/removing files, undoing changes)


Check out a repository:

 git clone <remote-repo-url> <local-repo-name>

For example

 git clone git://carpetcode.org/McLachlan McLachlan

Find out what changes were made to a remote repository since I last pulled:

 git fetch
 git log HEAD..origin

Update from a repository, if there are no local uncommitted changes:

 git pull --rebase

Hint: If you find you always want to rebase when you pull, then make it the default behavior by setting the autosetuprebase option

 git config --global branch.autosetuprebase always

Now whenever you run

 git pull

it will do a rebase too.

Update from a repository, if there are local changes:

Save all local changes:

 git stash

Then update as described above:

 git pull --rebase

Re-apply all local changes:

 git stash pop

If there are no conflicts, you are done. If there are conflicts, handle them, then:

 git stash drop

since "stash pop" doesn't pop the stash if there are conflicts. "git stash list" shows all stashes. View the content of a stash with "git stash show <stash>".

Push to a repository:

Ensure all remote changes have been pulled (see above), then:

 git push

Create a branch containing the current snapshop of the local repository, including all uncommitted changes, and switch to this branch

Create a new branch pointing to the current commit:

 git branch <newbranchname> 

Check out the new branch:

 git checkout <newbranchname>

List all branches

 git branch -a

??? How to find out whether there are accidental local branches without remote counterpart?

Find out on which branch I am:

 git branch

The branch marked with a star "*" is the current branch.

Switch to a branch, if there are no uncommitted changes

 git checkout <branch-name>

??? Does this require a better syntax so that branch names and directory names are not confused?

Committing

Commit a change:

Commit certain files:

 git commit <list-of-files-to-commit>

Commit all modified files which are known to git (i.e. have been added with "git add"):

 git commit -a

Modify a commit that hasn't propagated yet, keeping the working tree the same:

 git commit --amend <files>

This is the same as "git commit", except it rewrites the last commit instead of creating a new one. <files> lists any files with new changes that you want to include. You can also say "-a" to mean all registered files.

Undo a commit that hasn't propagated yet, keeping the working tree the same:

 git reset HEAD~1

This deletes the last commit but leaves the changes from the current commit in the working tree. HEAD~1 means one commit before the latest.

Revert a commit (i.e. create another commit), dropping all changes that it made:

 git revert <commit>

If your working tree is not clean, do a "git stash" before and a "git stash pop" after.

Revert a commit (i.e. create another commit) that may have propagated already, keeping all the changes as local modifications:

 ???

Making local changes

Look at local modifications:

List modified files:

 git status

Get a diff between the local repo and the working tree ("which changes did I make since the last commit?"):

 git diff HEAD [<filename>]

Get a diff between the local repo and the staging area ("which changes did I mark for committing?"):

 git diff --staged [<filename>]

Get a diff between the staging area and the working tree ("which of my changes would not be committed?"):

 git diff [<filename>]

Get a diff between the remote repository and working tree ("what would a 'git pull' do?"):

 git diff origin [<filename>]

Revert an uncommitted change to a file:

 git checkout -- <file>

Add a file to the repository:

First create file locally, then:

 git add <list-of-files-to-add>

Note: This does not mark the file for adding! Instead, this adds file at the time of "git add"; later modifications are ignored. Then commit. ICH: what do you mean? This registers the file with git ("adds it") and adds its content to the staging area. If you always commit with "git commit <file>..." then you don't have to worry about "add"ing again.

Remove a file from the repository:

Option 1:

Remove the files from the working tree as usual, then commit the changes to these files (i.e. their deletion) directly to the repository:

 rm <files>
 git commit <files>

You can use "-a" instead of <files> in the "git commit" command to commit all changes (including deletions) to registered files.

Option 2:

Remove the files from the working tree and mark them as removed in the staging area, then commit the staging area:

 git rm <files>
 git commit

Undo adding a file to the repository:

Delete file locally again, then

 git reset HEAD <file>

ICH: Do you mean "undo adding a file to the staging area/registering a file"? You use "repository" to mean the committed data everywhere else on this page.

Undo removing a file from the repository, recreating the file manually:

Recreate file locally, then:

 git reset HEAD <file>

Undo removing a file from the repository, resurrecting the file in the repository:

 ??? (probably a combination of "checkout" and "reset")

Useful hints

Aliases

Aliases are a handy way to simplify the commands needed for your own commonly used tasks. For example, you could add a "what's upstream" command to easily find out which commits will be pulled

 git config --global alias.wu '!sh -c "git fetch && git log origin..HEAD"'

From now on, just run

 git wu

and it will list the commits which would be merged in by a pull. Similarly, it can be useful to have a "what's downstream" command to know which commits would be pushed:

 git config --global alias.wd "log origin..HEAD"

Another useful example is if you often find yourself wanting to do the stash-pull-pop sequence, you could create an alias for that:

 git config --global alias.sp '!sh -c "git stash && git pull --rebase && git stash pop"'