Skip to the content.

Workshop - GIT


GIT Commands

Repository creation

Create a new local repository

$ git init

Clone an existing repository

$ git clone ssh://user@domain.com/repo.git

or using http

$ git clone http://user@git.domain.com/repo.git

Local changes

$ git status

$ git add <filename>

$ git add <filename> <filename2> ... <filenameX>

$ git add <dirname>

Note: an empty directory cannot be tracked, git only tracks files

$ git add .

Note: git add . actually adds the current folder’s content

Command must be ran in the project’s root directory, otherwise only the files in current directory will be added

Find current directory with pwd (linux) or cd (windows)

$ git add -p <file>

$ git diff

$ git commit -a

$ git commit

$ git commit --amend

Don‘t amend published commits!

Commit history

$ git log

$ git log -p <file>

$ git blame <file>

Branches and tags

$ git branch -av

$ git checkout <branch>

$ git branch <new-branch>

$ git checkout --track <remote/branch>

$ git branch -d <branch>

$ git tag <tag-name>

Update & publish

$ git remote -v

$ git remote show <remote>

$ git remote add <shortname> <url>

$ git fetch <remote>

$ git pull <remote> <branch>

$ git push <remote> <branch>

$ git branch -dr <remote/branch>

$ git push --tags

Merge & rebase

Merge into your current HEAD

$ git merge <branch>

$ git rebase <branch>

Note: Don‘t rebase published commits!

$ git rebase --abort

$ git rebase --continue

$ git mergetool

$ git rm <resolved-file>

Undo

$ git reset --hard HEAD

$ git checkout HEAD <file>

$ git revert <commit>

$ git reset --hard <commit>

$ git reset <commit>

$ git reset --keep <commit>