*                                                                             * ***** Practical Introduction to Git ***** SEN group meeting — 2013-01-17 Pablo Rauzy rauzy_at_enst_dot_fr * 1- Global setup ** 1.1- SSH key Important if you don't want to type your password all the time and use remote autocompletion. ** 1.2- Global configuration git config --global user.name "Firstname Lastname" git config --global user.email "name@enst.fr" * 2- Creating a personal repository ** 2.1- git init Creates a git repository. Everything important is in the .git directory. ** 2.2- .gitignore This file contains file name pattern that Git will ignore. For instance: a.out *~ *.o tmp/* * 3- Basic usage ** 3.1- The staging area The place where files and/or modifications go to before being commited. This is very important to understand, plus it adds a lot flexibility to the users, which in turn yields better commits. ** 3.2- git status Gives information about the status of the repository: untracked files, modified files… Understanding its output is important too. ** 3.3- git add Adds files and/or modifications to the staging area, ready to be commited. ** 3.4- git commit [-a] Commits the staging area to the repository. With the -a option it adds all modifications of tracked files to the staging area before commiting. * 4- Refering to commits ** 4.1- git log [--oneline] [--name-status] [-n] [--author] [--since] Shows the history of the repository, commit authors, and modifications. More importantly, it shows commit names (a sha1 hash). An unambiguous prefix of the commit hash is enough to identify it. ** 4.2- Relative references In addition to their hash, commits can be identified using names, and relatively to other commits. The HEAD commit is the "current commit" of the current branch. A branch name is a synonym for its current (last) commit. Given a commit C, C^ is the previous commit, C^^ is the commit before the previous commit, etc. C~, C~~ are the same but C~42 is the 42nd commit before C (this is a simplification, google or ask me if you need details). ** 4.3- Commit range Commit ranges are useful for a number of operations, starting with showing a the log or the diff between two specific commits. A range between commits C and D can be specified in two ways: C..D and C...D. Both are useful but the difference between the two are subtle, for now just try one and if it doesn't work, use the other one. * 5- Undoing ** 5.1- git diff [--cached] [--color-words] [ | | ] Shows the specified diff. With --cached it diffs commited changes with modifications in the staging area. By default staged changes are considered "done". With --color-words the diff is done per word rather than per line. ** 5.2- git reset [--soft|--mixed|--hard] Resets the repository to a specified commit. With --soft it only sets HEAD to the specified commit. With --mixed it also removes the modifications from the staging area. With --hard it also resets the files in the working directory. ** 5.3- git revert [--no-commit] Undoes changes introduced by in a new commit except if -n is present then just modify the files in the working directory. ** 5.4- git checkout Reverts a specific file to its state at commit . * 6- Branching ** 6.1- git branch [] Creates a new branch named or lists existing branches if no branch name is supplied. ** 6.2- git checkout Switches to branch . ** 6.3- git checkout -b Creates a new branch named with HEAD on . ** 6.4- git branch [-d|-D|-m|-M] Deletes or renames a branch. * 7- Merging ** 7.1- git merge Merges the changes from into the current branch. ** 7.2- git cherry-pick Imports a commit into the current branch, applying its modifications to the files. ** 7.3- Resolving conflicts Conflicts are usually handled by git using different merge strategies, but sometimes it needs your help. In those case git tells you which files have conflicts so you can edit them and then commit the result. ** 7.4- git checkout --ours|--theirs Chooses one of the version of the files in case of conflicts. * 8- Working with others ** 8.1- Distributed VCS Git is a *distributed* version control system. This means that every repository is self-sufficient. It does not mean that you can't have a central repository. ** 8.2- git init --bare A central repository can be created using the --bare option of git init. Such a repository does not have a working directory. ** 8.3- git clone This is how you "clone" a repository. The can be a path, an http(s) URL or an SSH path. ** 8.4- git remote With no argument, lists the remote repositories. git remote add sen rauzy@telecom:test.git git remote rename sen test … git help remote ** 8.5- git pull [ []] Pulls modifications from ("origin" by default) on branch (matching the local current branch name by default) and merges it with the current branch. In order not to merge the changes you can use git fetch, but that's not what you want in most cases. ** 8.5- git push [ []] Pushes the commited modifications of the current branch to ("origin" by default) on branch (matching the local current branch name by default). ** 8.7- git branch -r Lists remote branches. * 9- Useful tips ** 9.1- git add -p Allows to choose which parts of the to put in the staging area. This is very useful if you don't want to commit every 5 minutes while developping but still wants commits to be atomic and meaningful. ** 9.2- git checkout -p Allows to choose which part of 's modifications to revert on the files. ** 9.3- git commit --amend Permits to edit the last commit message and possibly add missing files or modifications. ** 9.4- git blame Shows who and which commit is responsible for each line of . ** 9.5- git archive [] Creates an archive of the tracked files as they were at or HEAD if it is not specified. git archive --format=.tar.gz HEAD^ > project.tgz Will create a file project.tgz with the tracked files as of the commit before the last one. * a- Advanced git ** a.1- git rebase Very powerful but complicated, see tutorial on the web if you're interested. ** a.2- git stash Allows to temporarily "stash" modifications on the working tree without commiting them in order to proceed to a merge for instance. ** a.3- git bisect Allows to find who and which commit is reposible for a particular behavior by bisecting on it in the history of the repository. ** a.4- Hooks Hooks are executable that can be executed automatically at different moments such as after or before a commit, after or before a receive (pull / push)… Those scripts can automatically clean the code, or refuse a commit if it doesn't compile or passes some tests for instance. They can also be used creatively… * b- Misc ** b.1- Graphical user interfaces for git magit, gitg, giggle, tig, gitk… ** b.2- Links A successful Git branching model http://nvie.com/posts/a-successful-git-branching-model/ Understanding Git Conceptually http://www.sbf5.com/~cduan/technical/git/ Git Reference http://gitref.org/ Git for Computer Scientists http://eagain.net/articles/git-for-computer-scientists/ * The End * That's all folks!