Basic Tasks

Create a new repo from scratch (Option I, github or gitlab)

  1. Log on to github or gitlab
  2. Make sure you have ssh keys set up correctly before you proceed.
    • See docs for or
    • Test it with ssh -T or ssh -T and don't proceed until it works.
  3. Create a new repo on the web at or This isn't hard... read the online docs if you need to. Note that on they're called "repositories" while on they're called "projects".
  4. Clone the (empty) repo to your local machine, as...
  $ git clone    # or
  $ git clone

Now you're ready to add files, commit, push etc. You can always check where the remote repo is with:

  $ git remote -v
  origin (fetch)
  origin (push)

Create a new repo from scratch (option II, local)

Normally you'd do it this way if you already have a git repo you want to connect to github or gitlab.

  $ cd ~/work              # best not your login directory, suggest "work"
  $ mkdir eggbeater        # use a short, memorable name
  $ cd eggbeater
  $ git init
  $ echo "# Test project" >      # optional but nice for Github/gitlab
  $ git add
  $ git commit -m "initial commit"

Now you will want to connect it with a remote. For example, for gitlab:

  $ git remote add origin
  $ git push --set-upstream origin master

This doesn't work (easily) for you have to use the other recipe above where you create the repo first on the webpage.

Random Tips

Alias lg to see log

From Stefano M: "Alias to see all commits in the history of branches, tags and other refs in a graphically half-decent way"

  Open ~/.gitconfig and add the following:
    lg = log --all --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(auto)%d%C(reset)'

  From now on you can run
   git lg 

Deal with "Your local changes...would be overwritten

  git reset -- hard       # this kills all local changes, careful!
  git pull

Switch to (remote) branch

  $ git checkout -b <branch> --track <remote>/<branch>

Update submodule to latest

  cd submodule_name
  git checkout master && git pull
  cd ..
  git add submodule_name
  git commit -m "updating submodule to latest"

See what branch we are on

  $ git branch -a

View graph of branches

git log --graph --oneline --decorate
git log --graph --oneline --decorate --all

Undo changes to one file


  $ git checkout -- file

Avoiding to always type password

Normal editing

  $ git clone ssh://
  $ cd Proto0_Blade
  $ (edit files)
  $ git commit -a     # commit changes (not new files) to local
  $ git push --all    # push changes back to master on remote

To check out a specific revision, figure out the SHA1 code (long hex string), then:

  $ git fetch origin <SHA1>
  $ git reset --hard <SHA1>

Apply .gitignore to mess already committed

First of all, commit all pending changes.

Then run this command:

git rm -r --cached .

This removes everything from the index, then just run:

git add .

Commit it:

git commit -m ".gitignore is now working"

git push

Please be careful, when you push this to a repository and pull from somewhere else into a state where those files are still tracked, the files will be DELETED

Last modified 10 months ago Last modified on Feb 6, 2023, 4:53:09 PM