After the first post on Git 101, here is a set of commands you will use after the first 15-20 minutes of working with it. Some are very useful (e.g. stash your work before you can commit it in order to go for a quick coffee when your code is not done) and some are a quite rare (e.g. setting up a git on a remote server). Good luck.
Update & Merge
Creating a branch (and switching to the new branch) in one line
git checkout -b "The new branch name"
git pull – to update your local repository to the newest commit. It will fetch and merge remote changes.
git merge – to merge another branch into your active branch (e.g. master).
Remember that in both cases, git tries to auto-merge changes. IF you have conflicts, You are responsible to merge those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged with git add
Preview changes before merging them git diff
Creating a stash (think of it as a temporary place, like a clipboard to save changes without marking them deep in history) of changes to allow you to switch branches without committing.
A few commands I found myself using daily… Well, it might be a good idea to share it with others and see what can be done better. If you like to get out of the command line, I found Source Tree to be a powerful free application that give you many options to see the code, changes and flow. You might want to check it out. Another good option is the GitHub official client app. Ok, let’s jump into the list of git commands. Continue reading →
Here is a set of commands I found using… There might be a lot of other useful commands.
Setting up a Git project
If you want to work with an existing project, clone it:
$ git clone <url> - for example: git clone https://firstname.lastname@example.org/greenido/html5-boilerplate.git
If you do not have an existing git project, create one:
$ cd project/
$ git init # initializes the repository
$ git add . # add those 'unknown' files
$ git commit # commit all changes, edit changelog entry
$ git rm --cached <file>... # ridiculously complicated command to undo, in case you forgot .gitignore
Git will look for a file named .gitignore in the root of your repository which contains a set of shell patterns to ignore in file paths.
Branching and merging
$ git checkout -b linux-work # create a new branch named "linux-work"
$ <make changes>
$ git commit -a
$ git checkout master # go back to master branch
$ git merge linux-work # merge changesets from linux-work (Git >= 1.5)
$ git pull . linux-work # merge changesets from linux-work (all Git versions)
$ git apply < ../p/foo.patch
$ git commit -a
Exporting a patch
$ <make changes>
$ git commit -a -m "commit message"
$ git format-patch HEAD^ # creates 0001-commit-message.txt
# (HEAD^ means every patch since one revision before the
# tip of the branch, also known as HEAD)
# clone from the primary Git repo
$ git clone email@example.com:greenido/html5-boilerplate.git
$ cd git
# pushing changes to a remote repo with SSH
$ git push firstname.lastname@example.org:my-repository.git/
# fetch changes to a remote branch into a local branch
$ git fetch email@example.com:my-repository.git/ remote-branch:local-branch
# merge changes from a remote machine
bar$ git pull git://foo/repo.git/ branch
# Serve repository via git protocol
$ cd /my/repository/
$ touch .git/git-daemon-export-ok
$ git daemon # now others can fetch from git://your.machine/my/repository/.git/
# Set up a bare (= without working directory) repository (e.g. on a webserver)
$ mkdir my-repo.git
$ cd my-repo.git
$ git --bare init
$ chmod a+x hooks/post-update # this is needed for HTTP transport
# you need to populate this repository via push
# inspect history visually
$ gitk # this opens a Tk window, and shows you how the revisions are connected
# inspect history
$ git log # this pipes a log of the current branch into your PAGER
$ git log -p # ditto, but append a patch after each commit message
# inspect a specific commit
$ git show HEAD # show commit info, diffstat and patch
# of the tip of the current branch
Referring to revisions
# by name
$ git log v1.0.0 # show history leading up to tag "v1.0.0"
$ git log master # show history of branch "master"
# relative to a name
$ git show master^ # show parent to last revision of master
$ git show master~2 # show grand parent to tip of master
$ git show master~3 # show great grand parent to tip of master (you get the idea)
# by output of "git describe"
$ git show v1.4.4-g730996f # you get this string by calling "git describe"
# by hash (internally, all objects are identified by a hash)
$ git show f665776185ad074b236c00751d666da7d1977dbe
$ git show f665776 # a unique prefix is sufficient
# tag a revision
$ git tag v1.0.0 # make current HEAD known as "v1.0.0"
$ git tag interesting v1.4.4-g730996f # tag a specific revision (not HEAD)
# diff between two branches
$ git diff origin..master # pipes a diff into PAGER
$ git diff origin..master > my.patch # pipes a diff into my.patch
# get diffstat of uncommitted work
$ git diff --stat HEAD