Git How-tos
The Git community book (french) (english)
Some other git tips
Git info
There is no git info (like svn info) command available with git :( Instead you can use following commands.
List remote URL
List branches
Remote branches:
Local branches:
Git configuration
Most recent commit
Work on a remote branch
git checkout -b mybranch origin/mybranch
Remove untracked files
If you want to also remove directories, run
If you just want to remove ignored files, run
If you want to remove ignored as well as non-ignored files, run
How to synchronize a fork with its origin repository?
MyA is a fork of the repository A. You use MyA for your devs and made updates. In the same time, the origin repository A still follows its way (new push, etc.). How to do if you want to (re)synchronize your forked repository with its origin?
- First, in your forked repository, add the origin as a remote
- Then, fetch origin changes into your forked repository
- List branches to see new available branches from the origin repository
- Now, we can merge
How to diff between a fork with its origin repository?
- First, in your forked repository, add the origin as a remote
- Fetch origin changes into your forked repository. It won't change your working copy.
- Then, compare any branch from your local repository to any remote you've added.
How to avoid many branches in your repository?
If you are many people working on the same software, your central git repository will end with a lot of branches and merge. Indeed, developers start to work on a revision and, when they are ready to push their changes, there will probably be new revisions from other developers. The default behavior of git when pulling changes from the central repository is to define a branch, add your commits to this branch and then merge the branch back to the master. To avoid that, especially if you know there is no conflict, you can use git rebase as explained here or here (french).
Hopefully, there is a shorcut for that. You can use pull with git pull --rebase. Git can be configured to make it the default behavior:
You can also check this useful post
How to push a new branch to the remote?
Note: It is better to don't use an already existing tag name to name your branch.
How to create a tag?
It will create an annotated tag (recommended)
To list tags:
git tag -l v1.4/*
How to delete a remote branch/tag?
To remove a remote branch or tag
To remove a local tag
How to synchronize a branch with the remore repository?
If the branch has no upstream:
The best option is to provide the -u option when pushing to the remote to specify the upstream automatically.
To easily track a remote branch from someone else
It creates and checks out "feature" branch that tracks "origin/feature".
How to revert a commit?
It is as simple as
It will create a new commit. The reverted commit is still present in the history.
How to rewrite history
The git rebase command allows you to easily change a series of commits, modifying the history of your repository. You can reorder, edit, or squash commits together.
Warning: Because changing your commit history can make things difficult for everyone else using the repository, it's considered bad practice to rebase commits when you've already pushed to a repository.
Rebase all the commits between another branch and the current branch state
Rebasing commits against a point in time
To rebase the last few commits in your current branch, you can enter the following command in your shell:
Remove the last commit from Git history
If the commit has already been pushed to the remote, and you want to propagate the change (WARNING: this is bad if someone already has the commit that will be deleted into his history):