User Tools

Site Tools




Git is a distributed version control system. FIXME

Useful commands

To check what has been locally committed into your local branches, but not yet pushed on the remote repository:

$ git log --name-status --branches --not --remotes=origin

To check what has been commited to branch A but not yet to your local master:

$ git log --name-status master..A

To check what has been commited to your local 'master' (or any branch name) but not yet pushed to the remote repository:

$ git log --name-status master --not --remotes=origin

To create a new local branch:

$ git branch test

To see what local branches do you have:

$ git branch

To switch to one prticular branch (for instance 'test')

$ git checkout test

You are on the test branch and want to merge all changes into the master branch:

$ git checkout master
$ git merge test

Commands dump

  • git status
  • git pull

Quick start

Let's assume you have already some files you would like managing under a revision control system. The procedure to start using Git to track (“import”) these files is as follow:

$ cd ~/project
$ git init
$ git add .
$ git status
$ git commit -m "All your code are belong to us"

Note that before committing, you can remove any unwanted directory or files by issuing one of the following command depending if this a directory or a file:

$ git rm -r --cached tmp/
$ git rm --cached test.old

The two above commands remove the directory (and its sub-tree) or file from the git branch. It does not delete the working copy.


I'm going to talk about a workflow that I'm using on one of my project. This project has one Git development repository which contains C++ code (for server) and Java/Eclipse RCP code (for clients).

I'm using Eclipse CDT for the C++ development and Eclipse EMF for the Java one. I cannot have just one got cloned repository locally, because I cannot then use both Eclipse CDT and EMF at the same time (the workspace would be the same, which Eclipse does not want).

So I have created to cloned version of your remote dev repository. One for C++ and one for Java.

However, I had some common IDL (for Corba) projects that I needed to use and update for both C++ and Java development. So I created the following workflow (I will create a better image later on):

Remote dev repository <---- clone ----> Local master repository

Local master repository <---- clone ----> Local C++ repository

Local master repository <---- clone ----> Local Java repository

So I pull and push my work (either C++ or Java) in a local master repository, which I keep synchronised with the remote repository.

How did I do it (there are other, probably better ways, but that was my trial and error path which works):

$ cd ~/Projects
$ git clone ssh://<username>@<remote-git-hostname>/path/to/dir.git master$ git clone ssh://<username>@<remote-git-hostname>/path/to/dir.git cpp
$ git clone ssh://<username>@<remote-git-hostname>/path/to/dir.git java
$ cd cpp
$ git remote add localmaster file://$HOME/Projects/master/
$ cd ../java
$ git remote add localmaster file://$HOME/Projects/master/

I thought I was done and that I could do $ git pull localmaster master and git push localmaster master inside the cpp or java directory to get and push new commits. But I got the following problem when pushing:

git push localmaster master 
Counting objects: 13, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (7/7), 578 bytes, done.
Total 7 (delta 5), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error: 
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To file://$HOME/Projects/master/
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'file://$HOME/Projects/master/'

The problem is that I have a checked-out version of the master in $HOME/Projects/master/ and this could make conflicts which git prevents me to do. One solution is to do a bare repository (no checkouts) out of the master one. This is how to do it:

$ cd ~/Projects/master
$ git config --bool core.bare true

AND you will have to delete the code that is under master (be careful with that command, but it should be something like rm -Rf ~/Projects/master/*).

Now you can pull and push from your local master.

foss/wikishelf/vcs/git.txt · Last modified: 2015/05/01 23:32 (external edit)