GIT client (EP)
This page explains how to work with GIT. It is based on our current way of working for European Parliament - DG Trad - Cat4trad.
Last update: 2018-11-19
Contents
Get source code
- Clone repository
git clone http://gdiaz@scm.ep-foundry.ep.parl.union.eu/git/scm/tradcat4trad/trad-das-cat4trad.git
cd trad-das-cat4trad
(i) by default you work on the 'master' branch
Switch to REMOTE branch
- View current branch
git branch
- View available branches
git branch -r
- Switch to REMOTE branch (ex: c4t-v3.5.1)
git checkout -b c4t-v3.5.1 origin/c4t-v3.5.1
- Check that you work with the branch
git branch
- Update to latest version
git pull
Get remote changes
Git has a 2 step process for gettings changes from others:
- fetch
- merge
Careful: make sure that you don't have any staged or unstaged changes. GIT won't let you merge changes if you have anything staged for the next commit. You must commit everything first before pull.
Fetch changes
FETCH
does not merge any changes into your local branches, it only downloads the new commits for your review.
To download changes into the local repository but without applying them:
git fetch
Merge changes into local repository
Apply changes downloaded through fetch
using the MERGE
command. MERGE
takes the commits retrieved from fetch
and tries to add them to your local branch. The merge will keep the commit history of your local changes so that when you share your branch with push
Git will know how others should merge your changes.
The challenge with merge is when a commit taken from fetch conflicts with an existing unpushed commit on your branch. Git is generally very smart about resolving merge conflicts automatically, but sometimes you must resolve merge conflicts manually and complete the merge with a new merge commit.
To apply fetched changes to the local repository and branches:
git merge
# You can merge without committing using --no-commit to attempt to perform the merge but not commit the final changes
# which gives you a chance to inspect the changed files before finalizing the merge with a commit.
git merge --no-commit
Fetch and merge
PULL
does a fetch
and then a merge
to download the commits and update your local branch in one command instead of two. Use PULL
to quickly bring your branch up to date with the remote when you aren't worried about reviewing the changes before merging them into your own branch.
branch update
To apply PULL
on the current branch
git pull
GIT stash
When you want to keep your changes - but not commit them - you can use STASH
.
Example:
- You have change a setting file, but you do not want to commit
- In current state this change is preventing you to pull (because of a merge issue)
- Apply
stash
to save your change,pull
the latest version of the branch,stash apply
to reput your changes again
Stashing means secretly hiding something and when we stash changes, they are stored in safe place. This is where git reset --hard contradicts. Git hard reset will get rid of changes in tracked files while stash will do the same but it can save the changes in secret location. These change can be re-applied if needed.
// TO BE CONTINUED
integrate other changes
To directly merge the work from remote branch into your local branch, PULL
a remote branch into a local one by passing remote branch information into pull:
# let's assume you're working on c4t-v3.5.1
git pull origin/master .
GIT patch
Creation of patch
- Create GIT patch
- Commit and push your changes on the branch
- Extract the changes into a patch file
# to extract last changes (single commit)
git format-patch HEAD~1
# to extract last 5 commits changes
git format-patch HEAD~5
- Copy the generated
.patch
file to the other folder you'd like to apply it
- Copy the generated
- Apply patch
- Go to the folder were you want to apply the patch
- Ensure the folder is up to date (to avoid conflicts afterwards)
git pull
# Apply patch
git am
In ECLIPSE
To add a GIT repository in Eclipse IDE:
- add an existing repository
- Import maven project from FILE SYSTEM (not from GIT view!)
Documentation
- Short introduction: Git short guide
- Git complete guide (with videos): Visual Studio GIT howTo
- Complete and excellent introduction, all in 1 blog post: Become a Git pro in just one blog: a thorough guide to git architecture and command line interface