Skip to content

Version control with Git: a quick how to

lionel-rigoux edited this page Aug 6, 2014 · 4 revisions

Content:

  1. Install and open a git session

  2. Start a project

  3. Record file changes

  4. Collaborate with github

  5. References


Install and open a git session

Windows

Using a simple git

  • Install git.
  • Open the file explorer and go to the working directory. Right click somewhere in background of the explorer window and click git bash.

Using the Github GUI

  • Install Github for windows
  • Open the Github app, click on the small gear on the top right and click open git shell

Linux

  • Open a terminal
  • Install git: apt-get install git
  • That's all

Start a project

Get a local copy of a Github repository

  • On the project's page, on the bottom right, copy the 'clone URL'. If you are at the ICM, choose 'https' beforehand. You should get somtehing like https://github.com/MBB-team/project-name.git
  • In the command window, type:
git clone https://github.com/MBB-team/project-name.git
  • Voilà!

Record file changes

The typical workflow is:

  1. Start a git session

  2. Select the files you changed and want to track

  3. Take a snapshot of the modifications (commit) and give a label to the snapshot

A good practice is to commit changes that are minimal and independent: prefer a lot of small meaningful commits ('typo corrections', 'cleaning file', 'that process now relies on this and that functions') to a big one collapsing all recent changes (eg. 'all changes made since two weeks ago'). Then you can revert a specific commit (eg. a functionnal change) without loosing other independant modifications (eg. typos)

Track files

  • If you want to save all changes at once:
git add --all
  • If you want to select only a subset of files:
git add filename1 filename2 

You can use jocker (*) to select multiple files.

  • Use git status to get a list of tracked changes.

Take a snapshot

  • Just give a label to identify the set of changes (it will appear publicly on the website):
git commit -m 'a meaningful label'

Tip: Adding modifications to the last commit

Did you forget a file in your last commit and does not want to make a new lousy commit just to add it? You can append some modifications (after using git add) to the last commit (and only the last one) like this:

git commit --amend

Modify the message, then quit the editor (ctrl+c, then type :quit + enter) and you're good. However, do not do this if you have already pushed on Github !!

Collaborate with github

All commits are local: they only affect your working directory. You can however send (push) your commit online and retrive (pull) modifications made by other contributors to synchronize your work.

The typical workflow is:

  1. Commit your modifications

  2. Pull other's commit to be sure to have the last public version.

  3. Test your code and resolve possible conflicts

  4. Commit patches if necessary

  5. Push your work on Github

Get the last public version

git pull

Tip: pulling with untracked changes in your directory, (ie without having to commit first)

git stash
git pull
git stash pop

The stash command discard (and save) all changes untracked since the last commit. The stash pop reapply previouvsly stashed changes. Thus, the three lines above allow you to perform a pull without having to commit your changes first (but you know it's bad to do that...).

Upload your work on the public repo

git push

To avoid warnings, use the non-ambiguous syntax git push origin master You can check on the website if it worked!

To go further

Git cheat sheet

The official documentation.

Clone this wiki locally