From Christophe Demarey

Tech: Git

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

git remote -v

List branches

Remote branches:

git branch -r

Local branches:

git branch

Git configuration

cat .git/config

Most recent commit

git —no-pager log —max-count=1

Work on a remote branch

git fetch origin
git checkout -b mybranch origin/mybranch

Remove untracked files

git clean -f

If you want to also remove directories, run

git clean -f -d

If you just want to remove ignored files, run

git clean -f -X

If you want to remove ignored as well as non-ignored files, run

git clean -f -x

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?

  1. First, in your forked repository, add the origin as a remote
git remote add <aname-refering-to-the-origin-repo> <origin-git-url>
  1. Then, fetch origin changes into your forked repository
git fetch <aname-refering-to-the-origin-repo>
  1. List branches to see new available branches from the origin repository
git branch -a
  1. Now, we can merge
git merge remotes/<aname-refering-to-the-origin-repo>/<branch-name>

How to diff between a fork with its origin repository?

  1. First, in your forked repository, add the origin as a remote
git remote add <aname-refering-to-the-origin-repo> <origin-git-url>
  1. Fetch origin changes into your forked repository. It won't change your working copy.
git fetch <aname-refering-to-the-origin-repo>
  1. Then, compare any branch from your local repository to any remote you've added.
git diff master <aname-refering-to-the-origin-repo>/master

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:

git config --global --bool pull.rebase true

You can also check this useful post

How to push a new branch to the remote?

git push origin newbranch

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)

git tag -a v1.4 -m 'my version 1.4'

To list tags:

git tag
git tag -l v1.4/*

How to delete a remote branch/tag?

To remove a remote branch or tag

git push origin --delete branch_or_tag_to_delete

To remove a local tag

git tag -d tagtodelete

How to synchronize a branch with the remore repository?

If the branch has no upstream:

git pull origin v1.4-maintenance

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

git checkout -t origin/feature

It creates and checks out "feature" branch that tracks "origin/feature".

How to revert a commit?

It is as simple as

git revert 5f1b62549477

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

git rebase --interactive other_branch_name

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:

git rebase --interactive HEAD~7

Remove the last commit from Git history

git reset --hard HEAD~1

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):

git push -f
Retrieved from http://chercheurs.lille.inria.fr/~demarey/Tech/Git
Page last modified on August 02, 2017, at 07:01 AM