Comp:git

From Theochem
Jump to navigationJump to search

To share your software you can use our gitlab server. Extensive documentation for git is available online, see e.g. git tutorial.

Here are instructions to set up a git repository on disk. This is not only useful to share your software or develop software together, it also lets you keep track of versions of you scripts or programs, and synchronize projects between, e.g., a laptop and a desktop computer.

Create .gitconfig

In your home directory create a file with the name .gitconfig with the content:

 [user]
         name = Firstname M. Lastname
         email = username@science.ru.nl

This is all that is needed, but more settings can be added:

 [init]
         defaultBranch = main            # change the default branch name "master" to "main"
 [color]
          ui = auto
 [core]
          pager = more                   # or less
 [diff]
          tool = vimdiff                 # if you use vi or vim
 [alias]
          log1 = log --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(bold yellow)%d%C(reset)' --all
          log2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all

Start a new project

Here we give an example of creating a git repository with a single bash script. First create a directory with the script:

  mkdir hworld
  cd hworld
  

Create a small bash script with the name hw.sh, e.g.:

  #!/bin/bash
  echo "hello world"
  
  chmod +x hw.sh
  ./hw.sh              # see if it works

Next, initialize git

  git init             # this creates a subdirectory .git where git keeps info
  git add hw.sh        # we want to use git to keep track of this file, it will be added to the "staging area"
  git commit -m "feat: initial commit" hw.sh

With the git commit command, git will make a snapshot of the current version of the files that are under its control, i.e., files that have been added with the git add command and have been committed.

  You can get information on git files in a directory with:
  git status           # will show, e.g., when files have been "added", but not "committed"
  git log2             # this shows the history
  git ls-files         # this lists the files that are under git control
  git branch           # if you created the above .gitconfig file the branch will be "main"

Setup a "bare git repository"

To be able to work on this project on another computer it is convenient to set up a "bare git repository":

  cd ~                  # go to your home directory
  mkdir repos           # create a directory for "bare" git repositories
  cd repos
  git init --bare hworld      # creates directory "hworld" with git repo

Now go back to the hworld project to upload the files to the "bare" repository:

  cd ~/hworld
  git remote add origin lilo.science.ru.nl:/home/<your username>/repos/
  git remote -v               # to see the name you just entered

You can change the "remote" repository with

  git remote set-url origin lilo.science.ru.nl:/home/pietje/repos/hworld

If the remote repo is setup properly, you can now copy your git files to it:

  git push --set-upstream origin main               # assuming you use "main" as branch name

Possible workflow

You can now create new files and modify files:

  vi hw.sh                 # modify the script and save it
  git status               # this will show a file under git control has changed
  git diff                 # this will show what has changed
  git difftool hw.sh       # if you set "tool = vimdiff" in ~/.gitconfig you can see the changes in vi
                           # you may have to learn some new vi commands to take full advantage of this
  git add hw.sh            # if you are happy with the change
  git commit               # this will start vi and let you write a commit message, e.g.
                           # fix: corrected a typo

You can repeat this cycle. There are git commands to "undo" a git commit. At the end of the day, after all changes have been committed, you can "push" all files under git control to the bare repository:

  git push