3.1 Git basics
If necessary, review Version control systems
Practical exercise
- Install git
- Set user data
- Create bare Git repository on server
- Create Git project in your workspace
- Check configuration
- Commit and push changes
- Update local repository and push changes to remote repository
- Optional tasks
- Additional info
Install git
(If it is not done already) install git command line and git GTK GUI on your computer, enter command:
sudo apt-get install gitInstall meld diff tool:
sudo apt-get install meld
Set user data
Set user data for Git version metadata with commands:
git config --global user.name "Name Surname"
git config --global user.email name.surname@example.comSet default editor for commit totes etc. in git with command, e.g.:
git config --global core.editor mousepadSet meld as default merge and diff tool for git:
git config --global merge.tool meld
git config --global diff.tool meld
Create bare Git repository on server
Connect with SSH to the bcserver and create remote Git repository with commands:
ssh student@bcserver
mkdir /home/student/git/Aaaaaa
cd /home/student/git/Aaaaaa
git --bare init
exit
Create Git project in your workspace
Go to your project:
cd ~/workspace/AaaaaaInitialize local Git repository for the project:
git initAdd remote repository to the project:
git remote add origin ssh://student@bcserver/home/student/git/Aaaaaa/or, if you want to change settings, open .git/config file in the project and change settings:
...
[remote "origin"]
url = ssh://student@bcserver/home/student/git/Aaaaaa/
...Create .gitignore file in your project folder with following content:
.classpath
**/.~lock*
*.log
logs/**
.metadata/**
.project
.settings/**
target/**
wbp-meta/**
Check configuration
Run GitTest1 unit test in package jtm.activity021 and check results.
Commit and push changes
All changes can be commited to local repository and pushed to remote repository using following commands1:
git add -A
git commit -m "Put yor comment here"
git push -u origin master
Update local repository and push changes to remote repository
- Use git status to see, what is changed comparing to last commit
Add all changes to your local repository and push them to remote repository:
cd ~/workspace/Aaaaaa
git pull
git add -A
git commit -m "Your message here"
git pushIf you have local git repository with different history than remote project, and know, that local project is OK, then you can forcefully push your project to the server by adding -f switch, e.g.:
git push -f origin masterOptional tasks
Use Egit plugin
- If your Eclipse doesn't have it already installed, install Egit plugin for Eclipse
- Learn How to use Egit plugin
- Learn how to use Git branches and merging tools
Clone your project from server
Clone project from server
cd ~/workspace
git clone ssh://student@bcserver/home/student/git/Aaaaaa/where Aaaaaa is code of your project.
To clone it in different folder add e.g. Aaaaaa1 at the end of previous command.
Use key exchange for SSH connections
If you are tired of writing password, you can enable SSH key exchange by following these instructions:
Additional info
- ^ origin is remote repository name, but master is branch mapping (shortened from master:master) from local to remote repository
-u switch will set master branch of local repository as a default for pushing to remote repository, so after that simple git push and git pull can be used.