Difference between revisions of "TubeTK/Development/GITCheatSheet"

From KitwarePublic
Jump to navigationJump to search
Line 9: Line 9:


= Workspace tips =
= Workspace tips =
* Show branch in prompt: add the following to your .bashrc
Show the current branch in your prompt: add the following to your .bashrc
 
  PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;33m\]`git branch 2>/dev/null|cut -f2 -d\* -s|sed -e"s/ //g"`\[\033[00m\]\$ '
  PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;33m\]`git branch 2>/dev/null|cut -f2 -d\* -s|sed -e"s/ //g"`\[\033[00m\]\$ '
* gitk is a great tool for visualizing the git history and seeing where your master or branch is wrt origin:master
 
gitk is a great tool for visualizing the git history and seeing where your master or branch is with respect to origin:master
 
  gitk
  gitk
* git gui is a great tool for performing git commands with a graphical user interface
 
git gui is a great tool for performing git commands via a graphical user interface
 
  git gui
  git gui


=== Work with branches ===
= Work with branches =


To list just the branches in your local repository:
To list the branches in your local repository:


  git branch
  git branch
Line 26: Line 31:
  git branch -r
  git branch -r


To check out a new local branch based off of a published branch, for example the 2.4 branch:
To create a local branch:
 
git branch branch_name
 
To check out a new local branch based off of a published branch:
 
git checkout -b branch_name upstream/branch_name
 
To checkout a new local branch based off another local branch:
 
git checkout -b new_branch_name existing_branch_name


  git checkout -b vBeta upstream/vBeta
After you branch, you can switch freely between master and your branch:
 
  git checkout master
git checkout branch_name


To work on the Beta branch instead of master:
To work on the Beta branch instead of master:


  git branch --track vBeta origin/vBeta
  git branch --track vBeta upstream/vBeta
  git checkout vBeta
  git checkout vBeta
After you branch, you can switch freely between master and branch:
 
Remove a local branch:
 
  git checkout master
  git checkout master
  git checkout vBeta
  git branch -d <branch>
You can create your own branch based off another branch:
 
git branch '''<topic>''' master
Delete a remote branch
* Don't do this unless you're incredibly confident in what you're doing
 
git push origin :branch_name
 
 
 
 


=== If you prefer to use different directories for different branches ===
=== If you prefer to use different directories for different branches ===
Line 102: Line 129:
=== Stash local changes temporarily ===
=== Stash local changes temporarily ===
* git stash
* git stash
=== Create a local branch ===
* git branch test
=== Swtich to a local branch ===
* git checkout test


=== Get stashed local changes ===
=== Get stashed local changes ===
Line 127: Line 148:
* git push origin master
* git push origin master


=== Remove a local branch ===
 
* git checkout master
* git branch -d <branch>


=== Track a remote branch ===
=== Track a remote branch ===
Line 140: Line 159:
** sends changes to origin/somebranch
** sends changes to origin/somebranch


=== Delete a remote branch ===
 
* Don't do this unless you're incredibly confident in what you're doing
* git push origin :somebranch


=== Project History ===
=== Project History ===

Revision as of 23:38, 5 January 2011

Policy

Just because a thing can be done with git doesn't mean it should be done.

  • Please don't revise history after it's been made publicly available (i.e. don't revise history after a "git push")

These instructions

Whenever these instructions refer to origin or upstream, they assume you've setup your development environment according to the instructions on Git Usage and Procedures

Workspace tips

Show the current branch in your prompt: add the following to your .bashrc

PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;33m\]`git branch 2>/dev/null|cut -f2 -d\* -s|sed -e"s/ //g"`\[\033[00m\]\$ '

gitk is a great tool for visualizing the git history and seeing where your master or branch is with respect to origin:master

gitk

git gui is a great tool for performing git commands via a graphical user interface

git gui

Work with branches

To list the branches in your local repository:

git branch

To list all the branches in the remote repositories:

git branch -r

To create a local branch:

git branch branch_name

To check out a new local branch based off of a published branch:

git checkout -b branch_name upstream/branch_name

To checkout a new local branch based off another local branch:

git checkout -b new_branch_name existing_branch_name

After you branch, you can switch freely between master and your branch:

git checkout master
git checkout branch_name

To work on the Beta branch instead of master:

git branch --track vBeta upstream/vBeta
git checkout vBeta

Remove a local branch:

git checkout master
git branch -d <branch>

Delete a remote branch

  • Don't do this unless you're incredibly confident in what you're doing
git push origin :branch_name



If you prefer to use different directories for different branches

The following sequence creates "tubetk-vBeta" alongside "tubetk", then uses 'git relink' to save disk space, then switches to the vBeta in the tubetk directory:

cp -r tubetk/ tubetk-vBeta
git relink tubetk-vBeta/ tubetk/
cd tubetk-vBeta
git branch vBeta origin/vBeta 
git checkout vBeta

Check with tubetk developers before pushing changes to vBeta. If your bugfix is approved for vBeta, then it will also be fixed on master when changes in the branch are merged up.

Get changes from others

From the central repository

To get changes that have been committed to the location you originally cloned from:

git stash            # Push your uncommitted local changes to a stack.  Be careful, they are not saved anywhere else.
git pull upstream
git stash pop        # To re-apply the changes on the stack to the local repository

From other repositories

Pull changes made by another developer in his/her public repository, but not yet committed to the central repository:

git pull git://<some-other-repo>.git master

'git remote' can be used to manage short names for repositories that you frequently pull from.

From patches

Apply a patch from another developer, preserving the other developer's identity as the patch author:

git am --signoff patch.mbox

Prepare commits to share with fellow developers

With git, it's possible to record every edit and false start as a separate commit. This is very convenient as a way to create checkpoints during development, but often you don't want to share these false starts with others.

Git provides two main ways to do this, both of which can be done freely before you share the change:

  • 'git commit --amend' lets you make additional changes a part of the last thing you committed, optionally modifying the commit message as well. Use this if you realized right away that you left something out of the commit, or if you typo'd the commit.
  • 'git rebase --interactive origin' lets you go back through each change made since 'origin', possibly editing it or combining ('squashing') it with another change. In the most extreme case, you can 'squash' it into a single commit, if there's no value to other developers in seeing the individual steps.
    • For example, to squash the most recent four commits into one commit:
git rebase --interactive HEAD~4
change all lines except the first one to “squash” instead of “pick”, and save (note list of commits is backwards, from oldest at top to newest at bottom)
edit the commit message, and save
    • At any time, to abort the rebase:
git rebase --abort

Send patches through e-mail or the web

When you think your changes are ready to be used by others, you can share it in the form of a patch. Make a series of patches for each commit in your local branch but not in 'origin':

git format-patch -M origin

This creates a number of files with names like

0001-my-well-intentioned-change.patch

These patch files are suitable for putting on a webserver or for sending as e-mail with your favorite mail client or git-send-email (some configuration required).

To submit a patch, email it to one of the TubeTK Developers.



Stash local changes temporarily

  • git stash

Get stashed local changes

  • git stash pop

Update a branch to the remote master's head

  • git stash
  • git rebase origin master
  • git stash pop

Stash only those changes that have not been staged

  • git stash save --keep-index

Add to local commit

  • git add <filename>
  • git add -A

Push local commit

  • git push origin master


Track a remote branch

  • "git fetch" (get up to date) or "git pull"
  • git branch --track somebranch origin/somebranch
    • --track is not needed unless you've set branch.autosetupmerge to false in your config
  • git checkout somebranch
  • git commit
  • git push
    • sends changes to origin/somebranch


Project History

  • git log ( To view the history of your changes )
  • git log -p ( To see complete diffs at each step )
  • git log --stat --summary ( To see overview )

Setting up tracked repositories to start pushing

  1. If you have cloned read-only version, do the following first
    1. git remote rm origin
  2. Add the proper remote
    1. git remote add origin git@gitorious.org:tubetk/tubetk.git
  3. You can push your changes
    1. git push origin master

Use a global ignore file for editor backups

Different developers' editors use different backup file names. Rather than put every possible editor backup file name in every project .gitignore, use a personal gitignore file to ignore your own editor backup files:

git config --global core.excludesfile ~/.gitignore
echo '*~' >> ~/.gitignore

Now, the exclusion pattern '*~' will be applied in every directory of every git project you use.

View history

Take a look at the history:

git log -C --stat

(git log has a bunch of options; this set detects renames and copies, and shows a summary of what files are changed in each commit) Get a closer look at a particular change by commit:

git log -C -p -1 57c609

(-p shows a patch, -1 restricts to a single change, and 57c609 is the start of a commit shown by the first 'git log' command) Get a list of commits to a particular file since vBeta branch:

git log --oneline vBeta..origin/v1.0 -- CMakeLists.txt

View history graphically, if you installed the necessary program:

gitk --all
qgit --all

You can also [view the history online in gitorious/github], but viewing the history locally is often more powerful.

View commits affecting a certain file only:

git whatchanged filename

Additional References