Git overview

Tilbage til oversigtsside for Git-Workshop

Denne side indeholder en generel introduktion til GIT med de mest anvendte kommandoer og en række tilhørende øvelser.

Quick recap:

  1. Creating repository on github
  2. Add collaborators (team members)
  3. Cloning repository
  4. git commands like: add, commit, push, fetch, merge, stash

alt text

Global settings

First repository

git clone <remote repo> or
git init inside a directory to make it a git repo.

Small exercise in basic git/github

  1. Create a repo online from github.com
    • Create github repo
    • Add collaborator
    • clone the project
    • add .gitignore
    • add netbeans project, add, commit and push
    • See result at github
    • Create some changes on the remote repo and commit
    • fetch and merge locally.
    • try merging when there is unstaged content in working area
    • then use git stash to temporarily remove the dirty files
    • make the merge
    • run git stash apply and git add . to put the stashed files back into the staged area.
  2. Starting locally (do this one at home)

Hint: you can work with git locally without ever needing to push to remote (if you dont need to collaborate with others / or backup in cloud)

Small exercise done in pairs

Finally delete the repository both remotely and locally

Lets demo


Demonstrating how files move through the states with git status:

Small exercise

Work individually and locally

Git diff

Lets demo


Demonstrate git diff

Remove stuff from staged area with git

View the project history - git log

Undo stuff

Overwrite previous commit message

Undo staging (remove file from staged area)

If you did git add and regret it

Undo changes since last commit on a particular file

Undo changes since last commit on all files

Undo last commit entirely

Small exercise

Remote (eg. github)

Remote is a server that can host our repository like

  1. github
  2. bitbucket
  3. gitlab
  4. more..

Set up reference to remote

  1. by cloning a remote repo
  2. git remote add <some name> <url> //git remote add + a name for the remote + the url for the remote
    • You can add several remotes if you like
    • If you clone - then the name will be ‘origin’
    • see list of remtes git remote -v

Tagging

Tags in git can be used to reference a particular commit (e.g. version1 or sprint1)

Git alias (Not necessary but maybe nice to have)

Git aliasses are typically longer commands refered to with a few key strokes. This can be usefull if you find you have long commands that you are using frequently.

Examples

  1. git config --global alias.unstage 'reset HEAD --' //git unstage will now unstage file:
  2. git config --global alias.last 'log -1 HEAD' // git last will now give the last commit
  3. git config --global alias.add-commit '!git add -A && git commit' //combines 2 commands into one
  4. git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
  5. Create your own……?

Modify the bash.bashrc file (windows)

You can write new commands to the git bash (teach it new tricks): Located at: C:\Program Files\Git\etc
Try adding this:

function gitp() {
    git add .
    git commit -a -m "$1"
    git push
}

Now you can open git bash and inside a git repo you can use the command: gitp “commit message” to add, commit and push in one single command.

Small exercise

Tilbage til oversigtsside for Git-Workshop