Link Menu Search Expand Document

Git

Quick git cheat sheet for things that I keep forgetting how to do because I don’t do them often enough.

Table of contents

  1. Server setup
  2. Submodules
    1. Add submodule
    2. Delete submodule
  3. Keep fork up to date with upstream
  4. Create a new branch
  5. Multiple remotes

Server setup

This should be only for local servers where you trust the users (meaning that you are probably the only person using it).

Assigning /bin/bash as the shell for the git user is most likely a bad idea.

sudo groupadd git
sudo useradd -s /bin/bash -d /home/git -g git -m git

Now change into the git user

sudo su - git

Perform the regular setup of SSH keys for the users that will be allowed to access the repos.

Create a new repository:

mkdir ~/myrepo.git
cd ~/myrepo.git
git init --bare

In another machine you can now clone this repository:

git clone git@your_server_ip:~/myrepo.git

Congratulations! Add your stuff to this repo, commit, push and be happy!

Submodules

Add submodule

git submodule add [url to git repo]

Delete submodule

git submodule deinit -f path/to/submodule
git rm -rf path/to/submodule
git commit -a

Keep fork up to date with upstream

  1. Clone your fork:
git clone git@github.com:YOUR-USERNAME/YOUR-FORKED-REPO.git
  1. Add remote from original repository in your forked repository:
cd into/cloned/fork-repo
git remote add upstream
git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream
  1. Updating your fork from original repo to keep up with their changes:
git pull upstream master

Create a new branch

git checkout -b name-of-branch
git pull upstream master

Multiple remotes

If you want to be able to push a repo to multiple remotes, go to the directory where you cloned or created your git repository and do this:

git remote add remote-all url_to_remote1
git remote set-url --add --push remote-all url_to_remote1
git remote set-url --add --push remote-all url_to_remote2

Then to push the master branch to all remotes:

git push remote-all master

Gilgalab Knowledge Base