Sie sind auf Seite 1von 27

Git Basics

Introduction
● I’m not a git master :p
Introduction
● I’m not a git master :p
● We’ll use Eclipse built-in Git Repository
Introduction
● I’m not a git master :p
● We’ll use Eclipse built-in Git Repository
● We’ll try to cover up some of the cli
commands
Introduction
● I’m not a git master :p
● We’ll use Eclipse built-in Git Repository
● We’ll try to cover up some of the cli
commands
● We’ll use BitBucket but of course in reality,
we’re going to set up our test server.
What is GIT?
- is a distributed version control system rather than centralized version control system (SVN, CVS, etc.)
- it means that when you ‘clone’ (checkout), you’re actually copying the repository to your local. Having a local
repository(full backup) has many advantages. File history, for example, is then looked up to your local machine rather
than remotely.
- Furthermore, when server dies(everything destroyed!). Any updated client that has the full backup can copied back up
to the server to restore it.
Why GIT over SVN?
● Speed. Git is very fast when it comes to committing and updating your
working copy.
● Work Locally. You can commit, create branch, create tag, etc. locally in
GIT. This feature is really useful when you are creating a production
feature and want to pause at the moment and maybe fix a critical bug.
● Mature Branching. switching branch in GIT is very fast. SVN is way behind
it. Branching allows us to keep a copy of the repo and make changes to
that copy. You may later ‘merge’ changes that branch to ‘master’ branch or
other branch.
● Pro Agile. But that doesn’t mean that SVN is anti-agile. It’s just GIT has
lots of ‘speed of light’ kind of features that’ll make development faster. One
of them is again, branching.
Git States
● Modified. File was changed.
● Staged. File was modified and marked as ready to commit.
● Committed. File changes were committed to your local repo. See it in
history
● Tracked. Files that are in repository.Either modified,staged, or committed.
● Untracked. Files that are ‘newly’ created and are not yet in repository
Creating a repository (CLI)
1. Create BitBucket repository.
2. Create a local directory and change directory to it.
3. git init. This will create .git which contains all necessary information of your repo.
4. git remote add origin <http://url/blah/blah/blah.git>. This will set remote information of newly initialized repo.
5. git add README.txt. This will add the README.txt to ‘index’ or basically means stage the modified/new file.
Don’t get confused with ‘svn add’ which adds NEW file. ‘git add’ will set file(s)’s state, whether new file or existing
file to ‘Staged’.
6. git commit -m ‘message here’. This will commit the staged files to local repo.
7. gitk. [Optional] Shows a gui to check history
8. git push -u origin master. Push changes to remote server. (-u means set the upstream to ‘origin master’. So that
on next pull you may ‘git pull’ instead of ‘git pull origin master’.)
Creating a repository (Eclipse)
1. Create BitBucket repository.
2. ‘Create Git Repository’ on Git repository perspective.
3. Type in name and destination directory
Creating a repository (Eclipse)
4. Right click at ‘Remotes’ and choose ‘Create Remote...’
5. Type in ‘origin’ in ‘Remote name’. Let configure push selected as we will do a push later.
6. Click ‘Change’ in Configure Push dialog
7. Paste in the URI. Notice the dialog is same as when cloning.
Creating a repository (Eclipse)
8. Click save
9. Create README.txt file on destination directory
10. Go back to Eclipse and hit ‘Refresh’. You should see something like
Creating a repository (Eclipse)
11. Right click README.txt and ‘Add to index’
12. On lower part of Eclipse fill out the following and finally press ‘Commit and Push’!!!
GIT States Demo
go back to slide 8

commands used in this demo

git status - check repo status. what branch? what file changes?
git commit - commit local
git add <file> - stage/index the file.
git rm --cached <file> - unstage/unindex the file. (Important: --cached must be present to retain the file)
gitk - quick gui commit log
git log - cli type commit log
git diff - show differences
Ignore Files
create .gitignore file and add pattern per line

/target
/.classpath
/.settings
/.project
/bin

On Eclipse you may right click -> Ignore file.


Deleting ‘tracked’ files
CLI way:
git rm <file>

Eclipse way:
Right click delete…

Windows way:
delete file
git add <file> or “Add to index” in eclipse

Restore deleted CLI way:


git reset HEAD <file>
git checkout -- <file>

Restore deleted Eclipse way:


“Remove from Git Index”
“Replace with HEAD revision”
History
Right click -> Show In -> History

“Compare with Version in Ancestor” -> opens compare window and compares the file from previous version
“Compare with Workspace” -> opens compare window and compares the file from workspace (modified file)
Pushing to remote
● Pushing to upstream is very simple just “git push” or “Push to Upstream” in Eclipse.
● In Eclipse, there is a single click “Commit and Push”.
● CLI, “git push” or “git push origin master”. The latter pushes the changes to ‘master’ branch if upstream is not
configured. To configure upstream you do a “git push -u origin master”.
● Remember: You can’t push codes if you’re local repo is not updated. You will see ‘non-fast forward’ dialog.
Pulling (No Local Changes)
1. Do a Fetch
2. Do a Merge
3. Or simply ‘Pull’ (fetch and merge)
Pulling (With Changes)
1. Do a Fetch (origin/master will show the latest commit id)
2. Important modified files must be committed
3. Do a merge (Fast-Forward means that if there is not changes on your machine, GIT will be smart enough to skip
merging and just fast forward to latest commit). Often there will be conflicts, see next slide.
Resolving Conflicts
When you merge origin/master to your local master, there’s a
large possibility that you will encounter conflicting files.
Resolving Conflicts
Right click the file then open “Merge Tool”.
Choose “Use HEAD …”
Resolve using GUI
Click “Save”
You’re not done yet. Go back to Git Staging
area. Add to index. Then “Commit”
Reset File
“Replace with HEAD revision” in Eclipse

git checkout HEAD <file>


Reset a commit
git reset --hard <commitid>
reverts a commit but potentially dangerous as it could destroy your important commits.

e.g.
git reset --hard HEAD~1 (undo previous commit)
git reset --hard ff9283… (reset up to commit id)

git reset --soft <commitid>


safe as it reverts a commit but commits will just go back to index.

e.g.
git reset --soft HEAD~1 (undo previous commit and move changes on that commit to index)
git reset --soft HEAD~2 (undo 2 previous commit and move changes on that commit to index)
Branching
git branch <branch name> - creates branch from HEAD (head is a pointer to your current working area)

git checkout <branch name> - switch to branch

git branch -d <branch name> - delete a branch

Careful with branch names conflicts.


In Eclipse there are lots of places to work with branch. Even on the History pane!!! I’ll let you guys explore… :)
http://stackoverflow.com/questions/8625406/how-to-delete-a-branch-in-the-remote-repository-using-egit

It is possible to create a branch from a branch or from a commit history.


git branch <branch name> <commit SHA-1> - creates branch from commit history

Pids loves going back to dec 5 release branch and testing ChartStream :)
Branching
Pushing branches to remote

In Eclipse, right click branch then “Push Branch…”


Enter ref name e.g.
refs/heads/newbranch
refs/heads/origin/test-branch
refs/heads/topics/feature1
refs/heads/zek/experiment
refs/heads/sprint3/audittrail

In CLI, it’s easy


git push origin <branch name>
Tips and Reminders
● If you plan to use “Pull” than “Fetch” and “Merge”. Always commit your changes before pulling to allow a
successful merge and deal with conflict later.
● Eclipse’s “Push to Upstream” and “Fetch from Upstream” may save you some clicks.
● git checkout <filename> will reset changes on your file.
● git checkout <commit SHA-1> <filename> will reset file and use the file in commit id.
● Always remember that you are working on local repository.
● Remember HEAD is a pointer (not HEAD revision in SVN Terms). HEAD~1 means 1 commit before HEAD…
HEAD~10,, HEAD~n
● Leverage the power of branching!!!
● If you run into problems with some error message like git-upload-pack. Got to Eclipse Preferences -> Team ->
Git -> Configuration. And on users tab add http.sslVerify = false

Das könnte Ihnen auch gefallen