Git recipes
This page is made to give useful command or succession of commands to perform Git tasks in the context of the CamiTK Git workflow.
We also recommend you to check these extra 15 Git tips to improve your workflow.
The gitlab user interface is great and will allow you to perform most of the operations quickly from your web browser.
This documentation is not a git documentation, and it is not showing either how to do all these operations with the gitlab user interface.
This is a small collection of little recipes for current git operations.
Undo, cancel, cleanup
Undo/Cancel the last commit
Simply use this command to completely undo the last commit and erase history.
git reset --hard HEAD~1
~
replaces the minus (-
) sign, so this means reset to HEAD - 1
commit.
You can also simply use this command to undo the last commit but leave the history (and your files):
git reset --soft HEAD~1
Undo (uncommitted) changes
For instance, you have accidentally removed a file. To revert changes to modified files:
# Be careful: it resets the index and working tree. Any changes to tracked files in the working tree since the last commit are discarded.
git reset --hard
This is the same as git reset –hard HEAD
.
Clean the current working tree
Use with care
# check first what is going to happen
git clean -nd
# if ok, do it, f=force, d=remove directories
git clean -fd
List the files modified by the last commit
Just type:
git log -n 1 --name-only --pretty=full
Working with branches
Rename a branch
git branch -m compile-on-sid <newName>
Diff between two branches
There are a lot of different ways to compare branches:
git diff b1..b2
git diff b1...b2
git log b1..b2
git shortlog b1..b2
To get only the filenames that have changed:
git diff --name-status b1..b2
See this stackoverflow answers
Copy commits from one branch to the current branch
Just type:
git cherry-pick commit-hash
To apply the commit commit-hash (made on another branch) to your current branch.
It is also possible to copy a range of commits from one branch to the current branch.
List all local branches and display last modified dates
From stackoverflow
for k in `git branch | sed s/^..//`; do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k --`\\t"$k";done | sort
Solve a conflict after a pull
Just type:
git mergetool
Running git mergetool
will prompt the user for each conflict, and propose a default application. On Linux, we recommend to use kdiff3
as the git diff tool.
Miscellaneous
Set VIM as default message editor
git config --global core.editor "vim"
export GIT_EDITOR=vim
Visualize Git tree under Linux
If git is not directly integrated in your IDE, for linux developers we recommend qgit
In order to get qgit
:
sudo apt-get install qgit
qgit
If your distrib does not have the package yet, get the source code at https://github.com/feinstaub/qgit .
Otherwise you can do this for git-cola :
sudo apt-get install git-cola
git-cola
Switch from git@
protocol to https://
protocol
This might be useful when you develop on another machine (not your regular workstation) and you don’t really want to copy your ssh key to this (not so trusted) machine.
If you have clone the git repository with the https://
, you have nothing to do.
But if you used the git@
protocol, as it uses the ssh protocol, you have to configure your local ~/.ssh
to interact with the origin repository.
You may easily switch from git@
protocol to https://
protocol by typing:
git config --global url."https://yourgitserver.example.com/".insteadOf git@/yourgitserver.example.com:
git config --global url."https://".insteadOf git://
For instance, if your git repository is hosted on the same gitlab instance as the CamiTK Community Edition, the following will do the trick:
git config --global url."https://gricad-gitlab.univ-grenoble-alpes.fr/".insteadOf git@/gricad-gitlab.univ-grenoble-alpes.fr:
git config --global url."https://".insteadOf git://
You can check that everything is in order by typing:
cat ~/.gitconfig
Troubleshooting
pathspec did not match any file
If you get the following error:
git checkout name-of-branch-on-origin
error: pathspec 'name-of-branch-on-origin' did not match any file(s) known to git
First try git fetch
to update your local git repository.
If this does not work, it might be that you cloned only a specific branch (using the -b develop
git clone parameter for instance). In this case, git fetch
won’t help as the local repository is only aware of the develop
branch.
You can check that by typing git config --get remote.origin.fetch
. If it displays +refs/heads/develop...
you are in this case.
In order to be able to get any other available branch from origin, type:
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
(note the *
instead of develop
)
Then type git fetch
to update your local git repository. It should shows the list of new available branch that you can now checkout locally.
Git recipes related to CamiTK
Clone to contribute
First, you need to clone the CamiTK git repository and switch to develop branch (see also the install pages).
If you want to share your work, you first need to create an account on our gitlab instance. We advised to login (if you are with the University Grenoble Alpes) or create a guest account first and then add an ssh key.
# R/W clone
git clone git@gricad-gitlab.univ-grenoble-alpes.fr:CamiTK/CamiTK.git
Delete local branches once they are merged
To delete all local branches that were merged on origin:
git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D
Working on a feature branch
To version your code, do not hesitate to commit to your branch as often as possible (and to push it so that your changes can appear in the merge request for review or sharing).
When you push, the gitlab-ci system is automatically started unless you add [ci skip]
or [skip ci]
to the commit message.
You can also use git push -o ci.skip
to prevent CI pipelines from running see gitlab doc.
Updating a feature branch that have conflicts with current develop
Sometimes your feature branch will be to distant to develop
. In this case, gitlab will complain and say that you can not merge
your feature branch in develop
.
In this case DON’T FOLLOW the instructions given by gitlab. It is better to do the following steps :
# First update your local develop branch
git checkout develop
# Get the current remote version
git fetch -p origin
# Merge the changes from `origin/develop` into your local `develop` branch. Your local `develop` branch will then be in sync with the remote repository
git merge origin/develop
# Check out your feature branch
git checkout feature/new-feature
# Merge the updated `develop` into your feature branch to include the latest changes
git merge develop
Now you can solve the conflicts. Note for Linux user, we recommend using a GUI merge tool to help to solve the conflicts, for instance:
git mergetool --tool=kdiff3
This only updates the local feature branch. Then, you need to:
- check all the files (some automatic merges might not be what you want)
- if needed, add and commit your changes (e.g.
git add .; git commit -m "Solved merge conflicts"
)
Push you branch to remote:
git push origin feature/new-feature
Gitlab should start the CI pipeline and remove the merge conflict warnings.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.