Git Guide

Introduction

Git was created by Linus to manage the linux kernel (as svn was too slow). It's especially useful when you have lots of people developing simultaneously on lots of different versions of code / software.

Use it for:

  • Communally editing text based files (code)
  • Analysis scripts / code
  • Any code base or simulation
  • Latex paper writing

Don't use it for:

  • As a replacement for a backup
  • Storing data (typical very large repository is 100Mb including all history)

Obtaining

Available in linux. You can get Git easily using Macports. Search for it port search git.

Using

There are two ways to use git 1) through the command line / terminal, and 2) through a graphical user interface (GUI). Working in the terminal first is a good step as you understand what's going on. Working with a GUI is sometimes convenient and a mixture of both may be quite productive.

Through the Terminal

You can use git by typing git followed by a selection of commands. These should be executed from the root folder of your repository - not any subfolder.

Through a GUI - ie SourceTree

A great free GUI is SourceTree for mac and windows (sadly no linux). The GUI is useful to visualise what is going on and to commit only certain lines of a file very easily.

  • Example of SourceTree visualisation of a git respository:
    Screen_Shot_2015-10-29_at_09.46.05.png

Guide

  • The information here is only as a quick guide
  • The following links are external guides that will be helpful

git-model2x.png

First Steps

  • After editing a few files, see what's changed git status

  • "Stage" a file to be committed git add somefolder/mycode.cc. "Staging" is the process of selecting which files you would like to record.

  • Record these changes in the version history git commit -m"This is a description of the changes I've made that others can see in the log."

  • Send your changes to the remote server git push

  • Get any new changes from remote server git pull

  • View all branches (versions) in the repository git branch -a

  • Switch to another branch called "develop" git checkout develop

  • Check which branch you're on and see the changes again git status

Terminology

  • *commit* - a recorded set of changes to the files in the history - it has a unique hexidecimal commit ID as a reference
  • *pull* - fetch and apply any changes from the remote server
  • *push* - send your committed changes to the central repository
  • *merge* - the process of resolving different changes from different people or branches
  • *branch* - in the "tree" analogy, a branch is a version of the software that sprouted off at some point from the original
  • *checkout* - the command to switch to a particular version / branch / commit ID - this changes the view of files you have in the repository
  • *master* - the name of the default branch and version of the software
  • *tag* - a label attached to a particular commit such as "v1.0". Useful to record a particular state by a recognisable name - big versions. You can also checkout a tag to switch to that version.

Working Notes

  • Note, these are just a copy of my working notes and may not be completely accurate

GET CHANGES
git pull

SEND CHANGES
git push

PUSH PULL - WHICH ORDER
you’ve committed things locally and want to push them - push fails as the server has changed (“your branch is behind the tip”). To avoid this, do your commits, then PULL, then PUSH.

COMMIT
the saves the changes
git commit -a -m”your message”
-a means stage all changed files (careful!)
-m the commit message for that change

CHECKOUT
changes your current view / branch you’re on.  changes the contents of the folder - only do from the root of the repository - ie not in a subfolder.
git checkout branchname
ie
git checkout develop

master is always the default branch in any repository and develop is the most common one to work in, so usually when you get a repository, you need to checkout the develop branch


VIEW ALL BRANCHES
git branch -a

CREATE A BRANCH
How to create a branch.  Merge is into what you're working locally on.  Use checkout to change 'focus' of what you're on now.


git fetch origin  #get status update from server
git checkout branchA
git merge master
     uh oh - resolve conflicts here
git checkout master
git merge branchA
git pus origin

CREATE A BRANCH USING CURRENT CHANGES
So you’ve worked in develop and changed things and want to move the current changes into a branch:

git checkout -b newbranchname
maybe have to do a commit now


MERGING / RESOLVING CONFLICTS

setup merge tool to meld

sudo port install meld
git config --global merge.tool meld 
launchctl load -w /Library/LaunchAgents/org.freedesktop.dbus-session.plist 


DELETE A BRANCH
 - make sure you're not in a branch
 - by checking something else

git branch -D branchname

DELETE A REMOTE BRANCH
git push origin —delete branchname   

WHEN IN YOUR BRANCH
git merge sourcebranchname

do this often while in my feature branch to pull in the changes into my branch from sourcebranchname

RESET STUFF
git reset HEAD --hard
git clean -fd

SUBMODULES INIT
git clone —recursive 

initialise submodules too

SUBMODULE UPDATE
git submodule update

-- Public.LaurieNevay - 25 Sep 2014

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng Screen_Shot_2015-10-29_at_09.46.05.png r1 manage 917.8 K 26 Nov 2015 - 14:18 LaurieNevay Example of SourceTree visualisation of a git respository
PNGpng git-model2x.png r1 manage 122.0 K 26 Nov 2015 - 14:22 LaurieNevay A successful git branching model
Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions

Physics WebpagesRHUL WebpagesCampus Connect • Royal Holloway, University of London, Egham, Surrey TW20 0EX; Tel/Fax +44 (0)1784 434455/437520

Topic revision: r2 - 26 Nov 2015 - LaurieNevay

 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding RHUL Physics Department TWiki? Send feedback