Git Primer
Git Primer
PART 0: Basics
Install git
Here it is a good idea to create a ".gitignore" file. Contents of this file will not version controlled.
Therefore add things like *.o, *.mod, *~. This file supports wildcard
Note: You do not have to push every time you commit or stage. You can do that incrementally.
For example, you have three files, you are sure about change in file1, so you stage that and
commit that (no push). Then next time you change file3 and sure about the changes, so you add
and commit that. Now your local working directory is two commits ahead of the origin. If you
push now, the repository will advance by two increments. However, it is, I believe, a good idea
to always push after commits if you are collaborating with someone.
Use git status to check current status of your files, i.e., modified, staged, new, etc.
Use git diff to find differences between repository version and your local version of a file
Use git log to see the commit messages
Use git checkout -- <filename> to revert any local changes
Use git reset <file> to unstage a file before commit
If the repository has changed, and your local working copy has not, and you want to update your
local working copy from new repository files, use:
git pull
Branching is a useful tool for collaborative code development. "master" is the default branch of a
repository, so when you are doing "git push origin master" you are updating master branch of
the repository.
Example:
Clone the master first to your local directory
git clone ...
default git clone command always gets the master branch
In your working copy create a branch where you are going to develop new features.
git checkout -b newFeature
This will switch tracking to a new branch named newFeature
Do some modifications to files, and stage, commit, etc. This branch is only available to you, not
to other collaborator until you push it. The push command for this would now be
git push origin newFeature
At this point if your collaborators want to get this branch, they can do
git clone file:///path/to/repository --branch newFeature
--single-branch </path/to/destination/directory>
You can continue to work on this branch. Since usually collaborators will get the master branch,
they will not see the incremental changes you are making to your branch! They will only see
your pushed/published version.
The single-branch option came in git 1.7.10. With this option, you can clone only one single
branch, otherwise you can switch a branch even when you had originally cloned another
branch.
Once you are done developing and debugging, merge your branch to the master.
1. Change to master branch: git checkout master
2. Merge your branch to master branch: git merge --no-ff newFeature
Now this step may result in conflict, as someone else may have already updated the master. So
you need to go through every file where git will find conflict and manually change them. When
that is done, make sure you have all the new files staged. Then commit and push. Once you
push. you may or may not delete your branch.
--no-ff preserves history in greater detail.
CAUTION: I think this model is best suited if there are not many developers. If collaborators are
just users, this will work fine. If everyone is developing parts of code, something like the
proposed workflow (see below) may work better. Advantage of proposed workflow is that
collaborating developer not necessarily need to keep track of your incremental changes in your
feature. They only need to get when the feature is ready to be “released.”
Proposed workflow:
http://nvie.com/posts/a-successful-git-branching-model/
Update master branch only when a feature is completely implemented and validated.
Create a branch for adding, developing, and debugging a feature.
Collaboration pattern
John Mary
git pull (updates local develop branch Does more local git commits
from repository)
git merge --no-ff j_branch (merges Still working
j_branch to develop)
git branch -d j_branch (deletes local
j_branch)
git push (pushes the merged develop
branch back to repository)
Before first use you must set name and email to be able to commit to a repository.
Useful commands:
$ git log --graph --abbrev-commit --pretty=oneline --color #nice
graphical display log
$ git add --all #adds all
$ git difftool -t vimdiff -y #use vimdiff to show difference
$ git merge --no-ff --no-commit
One way to set all this as aliases is to add following lines in $HOME/.gitconfig
–-
[user]
name = Your Name
email = [email protected]
[alias]
s = status
l = log --graph --abbrev-commit --pretty=oneline --color
a = add --all
vd = difftool -t vimdiff -y
mg = merge --no-ff --no-commit
[core]
editor = vim
–-
Online resources
Retroactive tagging:
https://github.com/openmelody/melody/wiki/Devbest-tagging