Githandson: Difference between revisions

From Gridkaschool
Jump to navigationJump to search
 
(56 intermediate revisions by 3 users not shown)
Line 3: Line 3:
'''All of these exercises should be followed on naf-school03 using your school account'''
'''All of these exercises should be followed on naf-school03 using your school account'''


'''''Note that lines beginning with a $ sign implies a shell command!'''''
* cd ~/school/git
==Exercise 1 (Configuration) ==
* Configure git
*'''Login to gridkalogin01'''
** git config --global user.name "Your Name"
** ssh schoolx@gridkalogin01.desy.de
** git config --global user.email you.email@domain


* cat ~/.gitconfig


* '''Create project'''
* optional extras for config (and obviously editor is mutually exclusive)
* $ mkdir project
** git config --global color.ui auto # If you like colours!
* $ cd project
** git config --global alias.co checkout
** git config --global alias.st status
** git config --global alias.ci commit
** git config --global core.editor emacs # flame bait
** git config --global core.editor vim



* git clone git@naf-school04:puppet
* '''Configure git'''
* cd puppet
** $ git config --global user.name "Your Name"
* git status
** $ git config --global user.email you.email@domain
* vi ${USER}.txt

** write something
* '''Examine the gitconfig file'''
* git status # what is updated? Working tree, Index or Repository?
* $ cat ~/.gitconfig
** git diff

** git diff HEAD

** git diff --cached
* '''Optional extras for config (and obviously editor is mutually exclusive)'''
* git add ${USER}.txt
** $ git config --global color.ui auto # If you like colours!
** git status
** $ git config --global alias.co checkout
** git diff
** $ git config --global alias.st status
** git diff HEAD
** git diff --cached
** $ git config --global alias.ci commit
** $ git config --global core.editor emacs # flame bait
* git commit -m “Add user text file”
** $ git config --global core.editor vim
** git status

** git diff
==Exercise 2 (Basics) ==
** git diff HEAD

** git diff --cached
'''Let's commit some stuff to git...'''
* git push origin master
* '''Ini new repo'''
** this might not work! Why?
** $ git init
** tip: what does “git pull --rebase” do?
** $ git status
* git checkout -b feature_${USER} master # specifying "master" here is just a good habit. Always beware of defaults.

* vi config.yaml

** Add a user to the list
* '''Create new file and commit it'''
* git add -p
** $ touch readme.txt
* git commit -m "Adding ${USER} to users list"
** $ echo "hello" > readme.txt
* git push origin feature_${USER}
* git checkout master
** $ git commit

* git merge feature_${USER}

* git push origin master
* '''Can't commit? Submit file to staging area first'''
** What happens?
** $ git add .
* git checkout -b newfeature_${USER} master
** $ git commit
* vi config.yaml

** add a server

** commit the change (with a good message!)
* '''Avoid opening editor and submit the change directly to repo: '''
** git log
** $ echo "some new change">> readme.txt
* Oops! We mistyped the server, vi config.yaml again
** $ git commit -am "fast commit to repo"
** change the name of the server

** commit the change

** git rebase -i HEAD~2
* '''Examine how to revert changes in working tree: '''
** use “fixup” or “squash” on the fix (make sure you understand the difference)
*'''Remove file from staging area:'''
** git log # examine the differences
** $ echo "bye" >> readme.txt
** $ git status
** $ git reset
*'''Remove file from staging area and and drop all changes, which were staged in working dir, take the last commit from local repo'''
** $ git reset --hard
* '''Discards all history and changes back to the specified commit'''
** $ git reset --hard [commit]
*'''Drop all local changes staged and unstaged, take the last commit from repo:'''
** git clean -df ( -d Remove untracked directories in addition to untracked files)
** git checkout -- .

==Exercise 3 (Working with remote repository) ==
* '''Clone the remote repository and examine the log '''
** $ git clone gitolite3@gridkapuppet01:test2.git
** $ git log


*'''Look in .git directory at the config file'''
**edit config file and delete [remote "origin"] section
** add remote repository by doing:
** $ git remote add origin gitolite3@gridkapuppet01:test2.git


* '''Add a new file to local repository and submit the changes to remote: '''
** $ git add <new file>
** $ git diff HEAD
** $ git commit -am "some comment"
** $ git push -u origin master ( -u - set-upstream ref)


* '''Drop all local changes and get the latest commit from remote repo:'''
** $ git fetch origin
** $ git reset --hard origin/master
* '''try: '''
**$ git fetch
**$ git rebase





=== Git cheat sheet ===

====Git configuration ====
* Git levels:

/etc/gitconfig - global git config for any user

~/.gitconfig -user specific

~/repo/.git/config -repo specific

'''each level ovewrites prevous.'''

git config --global user.name "Your Name"

git config --global user.email you.email@domain

git config --global color.ui auto # If you like colours!

git config --global alias.co checkout

git config --global alias.st status

git config --global alias.ci commit

git config --global core.editor emacs


==== Git basics ====

<pre>
git init - create new local repo
git clone /path/to/repository - check out a repository. Clone creates a new git repository by copying an existing one located at the URI you specify.
git clone https://github.com/ - clone repo from github

git status - List the files you've changed and those you still need to add
git add <filename> - add specified file to staging area
git add . - add all subdirectories and files recursivly
git commit - commit the staged files
git commit -am "some comment" - stage changes and commit in one step
git rm <filename> - remove deleted file
git reset - removes all added files from staging area, keeps the changes
git reset --hard - removes all added files from staging area, drops the local changes after last commit
git reset HEAD^ - the changes a now only in working dir, HEAD and Index moved one step back
git reset --hard HEAD - deletes last commit as it were never happend
git checkout -- . - discards all local changes which are not staged

git help - Help for most commonly used git commands
git help <command> - Detailed help for specified command

git init --bare --shared - Creates empty repository meaning no working directory in it.
git remote add origin REMOTEURL - git remote add creates an entry in your git config that specifies a name of a particular URL of the remote
git remote rm origin - removes added remote repository
git repo. This command can be used only after creation of local repo with git init or git clone.
git remote -v - This should show you all of the push/fetch remotes for the project.
git diff master origin/master - compare local branch with remote

git branch - list of all local branches
git branch -a - list of all branches including remote
git checkout -b <branchname> - creates new branch and switches to it
git checkout <branchname> - switch to another branch
git branch -d <branchname> - delete branch
git push --all origin - push all branches to remote repository

</pre>

Latest revision as of 23:52, 30 August 2016

Git Hands-on

All of these exercises should be followed on naf-school03 using your school account

Note that lines beginning with a $ sign implies a shell command!

Exercise 1 (Configuration)

  • Login to gridkalogin01
    • ssh schoolx@gridkalogin01.desy.de


  • Create project
  • $ mkdir project
  • $ cd project


  • Configure git
    • $ git config --global user.name "Your Name"
    • $ git config --global user.email you.email@domain
  • Examine the gitconfig file
  • $ cat ~/.gitconfig


  • Optional extras for config (and obviously editor is mutually exclusive)
    • $ git config --global color.ui auto # If you like colours!
    • $ git config --global alias.co checkout
    • $ git config --global alias.st status
    • $ git config --global alias.ci commit
    • $ git config --global core.editor emacs # flame bait
    • $ git config --global core.editor vim

Exercise 2 (Basics)

Let's commit some stuff to git...

  • Ini new repo
    • $ git init
    • $ git status


  • Create new file and commit it
    • $ touch readme.txt
    • $ echo "hello" > readme.txt
    • $ git commit


  • Can't commit? Submit file to staging area first
    • $ git add .
    • $ git commit


  • Avoid opening editor and submit the change directly to repo:
    • $ echo "some new change">> readme.txt
    • $ git commit -am "fast commit to repo"


  • Examine how to revert changes in working tree:
  • Remove file from staging area:
    • $ echo "bye" >> readme.txt
    • $ git status
    • $ git reset
  • Remove file from staging area and and drop all changes, which were staged in working dir, take the last commit from local repo
    • $ git reset --hard
  • Discards all history and changes back to the specified commit
    • $ git reset --hard [commit]
  • Drop all local changes staged and unstaged, take the last commit from repo:
    • git clean -df ( -d Remove untracked directories in addition to untracked files)
    • git checkout -- .

Exercise 3 (Working with remote repository)

  • Clone the remote repository and examine the log
    • $ git clone gitolite3@gridkapuppet01:test2.git
    • $ git log


  • Look in .git directory at the config file
    • edit config file and delete [remote "origin"] section
    • add remote repository by doing:
    • $ git remote add origin gitolite3@gridkapuppet01:test2.git


  • Add a new file to local repository and submit the changes to remote:
    • $ git add <new file>
    • $ git diff HEAD
    • $ git commit -am "some comment"
    • $ git push -u origin master ( -u - set-upstream ref)


  • Drop all local changes and get the latest commit from remote repo:
    • $ git fetch origin
    • $ git reset --hard origin/master
  • try:
    • $ git fetch
    • $ git rebase



Git cheat sheet

Git configuration

  • Git levels:

/etc/gitconfig - global git config for any user

~/.gitconfig -user specific

~/repo/.git/config -repo specific

each level ovewrites prevous.

git config --global user.name "Your Name"

git config --global user.email you.email@domain

git config --global color.ui auto # If you like colours!

git config --global alias.co checkout

git config --global alias.st status

git config --global alias.ci commit

git config --global core.editor emacs


Git basics

git init                        - create new local repo 
git clone /path/to/repository   - check out a repository. Clone creates a new git repository by copying an existing one located at the URI you specify.
git clone https://github.com/   - clone repo from github 

git status                      - List the files you've changed and those you still need to add
git add <filename>              - add specified file to staging area 
git add .                       - add all subdirectories and files recursivly
git commit                      - commit the staged files 
git commit -am "some comment"   - stage changes and commit in one step
git rm <filename>               - remove deleted file
git reset                       - removes all added files from staging area, keeps the changes 
git reset --hard                - removes all added files from staging area, drops the local changes after last commit
git reset HEAD^                 - the changes a now only in working dir, HEAD and Index moved one step back 
git reset --hard HEAD           - deletes last commit as it were never happend 
git checkout -- .               - discards all local changes which are not staged 

git help                        - Help for most commonly used git commands
git help <command>              - Detailed help for specified command

git init --bare --shared        - Creates empty repository meaning no working directory in it. 
git remote add origin REMOTEURL - git remote add creates an entry in your git config that specifies a name of a particular URL of the remote 
git remote rm origin            - removes added remote repository
                                  git repo. This command can be used only after creation of local repo with git init or git clone. 
git remote -v                   - This should show you all of the push/fetch remotes for the project.
git diff master origin/master   - compare local branch with remote 

git branch                      - list of all local branches 
git branch -a                   - list of all branches including remote 
git checkout -b <branchname>    - creates new branch and switches to it 
git checkout <branchname>       - switch to another branch 
git branch -d <branchname>      - delete branch 
git push --all origin           - push all branches to remote repository