GIT

GIT

Some GIT commands I use and wanted to note for my own reference in case I don’t use it anymore for some time and can’t remember ;-)

Submodules

Adding a submodule to a project

$ git submodule add <insert.repo.uri> destination/path

Initialize submodules in a fresh checkout

$ git submodule init
$ git submodule update

Fetch changes from submodule

Doing it for all existing submodules

$ git submodule foreach git pull
Doing it for only a specific submodule is standard git usage, you have to go to the root directory of the submodule and execute git pull or any other operation (including committing and pushing).

Forking

Adding upstream to your fork

$ git checkout master
$ git remote add upstream <insert.repo.uri>
$ git fetch upstream
$ git rebase upstream/master

Hosting git repositories

Here are some projects which help you to self host your environment

Initialize local git repository and create origin master

Go to the root directory you want for this repo and execute following commands

local$ git init
Create the repo on your server
local$ ssh server
server$ cd /path/to/git/root
server$ mkdir newrepo.git && cd newrepo.git
server$ git --bare init
server$ exit
Add the origin master to your local git
local$ git remote add origin ssh://server/path/to/repo.git
local$ git push origin master

clone a git for local changes that can be pushed again to origin master

local$ git clone ssh://server/path/to/repo.git

Remove files from git

In this example it’s the .DS_Store file created automatically on a mac

local$ find . -depth -name '.DS_Store' -exec git rm --cached '{}' \; -print

Add them to the ignore list

Execute this command in the root of the git directory

local$ echo ".DS_Store" >> .gitignore
local$ git add .gitignore
local$ git commit -m ".DS_Store removed and ignored"

Ignores for mac client

.DS_Store
._*

Ignores for working with latex

*.aux
*.dvi
*.ilg
*.ind
*.log
*.lol
*.out
*.toc
*.ps
*.glo
*.nlo
*.ilg
*.nls
*.bbl
*.blg

Upload to remote master

If you have not added the origin yet, do this first

local$ git remote add origin ssh://server/path/to/repo.git

To push the files

local$ git push origin master

Stay in sync with remote repository

local$ git pull origin master

add changes

Add all changed / new files to the next commit

local$ git add .
or as a synonym you can use
local$ git stage .
Interactive session to choose which files to be added
local$ git add -i

create local branch, do your work and merge it back to local master

New Branch from whatever branch you are in

local$ git branch task1

Do your changes and commit as usual. You can even switch branches in between.

Switch to the master (or whatever branch you branched from)

local$ git checkout master

Merge your temporary branch into master

local$ git merge task1

Delete your temporary branch

local$ git branch -D task1

Show me all branches

local$ git branch -a

Switch branch

local$ git checkout master

Show me changes since last commit

To see the actual code / cleartext:

local$ git diff --cached
or
local$ git diff --staged

More details and a nice graphic can be seen at http://365git.tumblr.com/post/474079664/whats-the-difference-part-1

Save dirty code and revert back to HEAD

local$ git stash --keep-index

To restore into the dirty code (merging it into your current state)

local$ git stash apply

Detailed description of the stash command can be found at http://www.kernel.org/pub/software/scm/git/docs/git-stash.html

Links

Michel Racic avatar
About Michel Racic
I’m a coding and technology freak. I finished once my bachelor of engineering in computer science or on german “Dipl. Ing.” and currently I’m mostly coding in Java as main language besides some stuff in C, C++, C#, HTML, JS, … - In my day job I work as a Software Engineer in Test and deal with all kind of Software from Native over Web into Mainframe where I use existing tools or write my own testing apps or libraries and the Frameworks around that they can be used in the most efficient way.
comments powered by Disqus