Git basics

→ Get a worksheet with exercises, solutions and a command summary here: GIT_Command_Summary_GERMAN.zip (GERMAN LANGUAGE).

Update local master

  • The local master-branch should be up-to-date with the current UPSTREAM/master.
  • The master-branch is never used for doing some work.
pull master
git rebase upstream/master # rebase-method (recommended, because commit-graph will be cleaner)
or
git pull upstream master   # merge-method '--force' (if you want to dismiss local changes)

Checkout new branch

If there is a new incoming ticket you need to checkout an extra branch for this job.

  • Branchname = ticketname (ex. TSK-45)
  • Only commit changes for this ticket there.
  • Untracked/uncommited changes should kept there.
checkout to branch
mle@NB170920MLE MINGW64 /c/Taskana/taskana (master)
$

mle@NB170920MLE MINGW64 /c/Taskana/taskana (master)
$git checkout -b TSK-45

mle@NB170920MLE MINGW64 /c/Taskana/taskana (TSK-45)
$
  • git branch printing all existing branches.
  • git branch <name> creating a new branch.
  • git checkout -b <branchname> will create a new branch and switch directly to it.

Commit changes

Show/add current changes

show changes
git status

This command will print all current changes.

  • RED colored lines are changes which are untracked/unstashed.
  • GREEN colored lines are changes which are marked for the next commit.

Add/remove changes

add/remove current changes
git add .   # Adding ALL changes
git add /src/java/....  # Add only this file

git checkout . # remove all uncomitted changes
git checkout /src/... # remove changes at this file or untrack it again.

Squash commits

After finishing the current ticket all new commits should be squashed together.

  • Show the log and count commits for this ticket. git log
  • Squash all commits into one. 
  • Avoiding to long/big histories/logs and keep the graph clean.
squash X commits
git rebase -i HEAD~X     # X = count of commits you want to summarise

→ Upcoming example used X=4 for squashing 4 commits into 1.

Now you need to change "pick" into squash(s) to apply changes into next picked commit. 

All 3 marked commits will be merged into the picked one. So now there is only 1 commit for this example.

Previous commit-massages can be changed if you want to.
So commits like "TEMP" or "ZWISCHENSTAND" can be deleted/changed.

Update and rebase ticket-branch

After squashing your commit into only 1 you need to update and rebase it against the current UPSTREAM/MASTER (or HEAD).
This will keep the graph clean and there will be no confusing problems later.

update/rebase branch
git rebase upstream/master # rebase it for comparing and order commits.
  • CONFLICTS should be solved and then add them.
  • Use rebase --continue for going on OR --skip if there are no more changes
  • --abort can cancle and roll back this action.
Show only files with conflicts
git diff --name-only --diff-filter=U  # show only files with conflicts

Create a pull-request

  • Current branch needs to be pushed to your remote repo. git push origin <branchname>
  • Click "create pullrequest" 

If you want to update a pull-request you just need to push your branch again.
Maybe  push --force ... needs to be used, because squashed commits are not recognized as a newer version.

Integrate a Pull-Request

Workflow for integrating a pull-request.

  • Checkout to your local master-branch
  • Get the latest update of the remote-repo
    • pull from upstream master (merge-method)
    • rebase to upstream master (rebase-method, recommended) 
  • Download the branch which is marked for pull-request

    download pull-request branch
    git fetch upstream pull/<#PULL_REQUEST_ID>/head:<BRANCHNAME>
  • Checkout to the new downloaded branch

  • Rebase against upstream/master
    • No conflicts - go on
    • Conflicts → REJECT and let the branch-owner update the pull request to new version.
  • Build all projects and test code and quality
    • REJECT if quality low, subjects missing, tests not complete, ...
  • ONLY MERGE-METHOD
    • Checkout back to your master

    • Merge the branch into master (rebase again optional)

      merge an other branch into current one
      git merge <BRANCHNAME>
  • push to remote (upstream) master and optional to your own remote
  • DONE

Log - Graph (Commithistory)

view commit-graph
git log  # showing logs with meta-data, escape with 'q'
git log --graph  # showing logs with commit-graph
git log --one-line  # only first line of commit-messages without any other meta-data - very short overview
git log --graph --one-line  # showing commit-graph with 1 lone commit-messsage

# Add it as short alias
git config --global alias.lg 'log --graph --one-line' # without GIT!
git lg  # showing commit-graph with 1 lone commit-message

This command can be used to see the current git-graph, which should be only 1 line after merging.

Transfer Commit from branch A to B


Maybe your current branch crashed or you can´t fix the merge-conflicts with the current upstream/master, then you can pick a commit from another branch (A)
and copy it ontop of another one (B).
This process called and can be done by cherry-picking.

→ Using this command is just a custom selective way to apply single commits and just should be done locally.
→ You need to solve conflicts the normal way if there are some occurring.

cherry-pick (basic)
git cherry-pick <COMMIT-HASH>  # apply commit on top of current branch.

First you need to get the short or long hash of the commit you want to pick. This can be done by showing the log at source-branch.

(warning) Use log --all, and if you want to clear your history pick them one by one, because you can get the same trouble at the new branch.