Git and Github

Git

  • distributed version-control system for tracking changes in source code during software development
  • works for any text-based files
  • can be accessed via commandline (git bash) or graphical interface (git GUI)

Github

  • a web-based service for version control with git
  • has a GUI for ease of use
  • including features like "issues", that are independant of git
  • alternative Plattforms:
    • GitLab: allows also private repositories in free plan
    • BitBucket: another competitor

Basics

  • initialization: creates a .git folder and starts tracking the changes through version control software.
  • staging area: when changes are recognized and prepared to be committed, they first will be added to the staging area.
  • commit: update the current branch with the files that are in the staging area.
  • branches: one feature = one branch!
  • merge: merge one branch with another
  • pull: pulling data from a source
  • push: pushing data to a target
  • forking: copying a full repo
  • pull request: request that someone else pulls your code (= code review). Typically a feature of github, not of git itself (true?).

Configuration

# set username and email (GLOBAL)
git config --global user.name 'thomas starzynski'
git config --global user.email 'thomas.starzynski@unibas.ch'

# check current username and email (GLOBAL)
# will return nothing when not set
git config --global user.name
git config --global user.email
git config --list       # list all configurations
git config -l           # --list = -l

Initialization

git init                # initializes new git repository in current directory!
                        # a .git folder is created
                        # by typing "ll" you can see the .git folder (it is hidden)
git clone <githublink>	# clones github repo to local machine (in working directory)
                        # remote connection established automatically

Commits

2-step process

  1. adding changes to staging area
git status            # shows changes made on local machine
git diff <filename>   # displays all changes of a modified file

# adding files or full directory to "staging area"
git add <file>
git add .

# removing files from "staging area"
git rm --cached <file>

# commits changes (that were added staging area) to current branch
git commit -m 'commit message'
  1. commiting changes in staging area to current branch
# commits changes (that were added staging area) to current branch
git commit -m 'commit message'

Overview: log of all Commits and Branches

# complete (long) format
git log
# pretty format
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative

Branches and Merges

# branches
git branch                    # shows all branches
git checkout -b <newBranch>   # creates new branch and moves into it
git checkout <existingBranch> # switches to an existing branch

# go to previous commit
git checkout <commit-hash>    # goes back to commit XY

# merge branch 
git checkout master        # switch to master branch
git pull origin master     # pull data from remote (to be up to date)
git merge branchToMerge    # master <- branchToMerge        
git push origin master     # push updated masterbranch to remote

# discarding all changes that show up with "git status"
git checkout .          # forgot -> ??
git checkout <file>     # forgot -> ??

Remote (pull, fetch, rebase, push)

# connect to remote
git remote add origin <githublink>  # connects to remote (which we call "origin" here)
git remote -v                       # check connection to remote

# pull from remote before pushing
# -> in case someone commited changes to the remotes
#    master while you were working on your local repo
git remote -v  # check if connected right
git pull       # pull lates without destroying local changes

# push local changes to master
git push origin master

# PULL REQUEST = Code Review
# -> via GitHub?

Usefull Commands by (Github)

# create a new repository on the command line
cd target-folder
echo "# title-of-repo" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin <github-repo-link>
git push -u origin master
# push an existing repository from the command line
git remote add origin <github-repo-link>
git push -u origin master

Open Questions

  • wie kompletten subfolder entfernen für git remove
  • wenn man "git status" eingibt -> sieht man nur den gangen Folder, nicht die einzelnen files. Kann man mit git status auch alle files aufgelistet haben in den subfoldern?