TubeTK/Development/GITCheatSheet

From KitwarePublic
Jump to navigationJump to search

Workspace tips

  • Show branch in 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 wrt origin:master
gitk
  • git gui is a great tool for performing git commands with a graphical user interface
git gui

Work with branches

To list just the branches in your local repository:

git branch

To list all the branches in the remote repositories:

git branch -r

To check out a new local branch based off of a published branch, for example the 2.4 branch:

git checkout -b vBeta upstream/vBeta

To work on the Beta branch instead of master:

git branch --track vBeta origin/vBeta
git checkout vBeta

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

git checkout master
git checkout vBeta

You can create your own branch based off another branch:

git branch <topic> master

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.

Stash local changes temporarily

  • git stash

Create a local branch

  • git branch test

Swtich to a local branch

  • git checkout test

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

Remove a local branch

  • git checkout master
  • git branch -d <branch>

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

Delete a remote branch

  • Don't do this unless you're incredibly confident in what you're doing
  • git push 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