Helpful git tips and tricks

juin 16th, 2013

I’ve used git since a while now but I don’t consider myself a git expert, far from it. The following tips and tricks is a series of GIT commands that will get you out of the mess and will make you more confortable.

Create Aliases

make « com » alias for « commit »

git config alias.ci commit

make « co » alias for « checkout »

git config alias.co checkout

make « br » alias for « branch »

git config alias.br branch

make « lg » alias for « log »

git config alias.lg log

make « df » alias for « diff »

git config alias.df diff

make « st » alias for « status »

git config alias.st status

Get a project on remote server

git clone <git user>@<domain repo name>:. <repository> <target-dir>

Create a new branch from the master

git checkout -b <new branch>

Positioning the reference branch back to the master branch

git co master

Update the master from the original filing

git pull --rebase origin master

Push the history of the branch on the remote branch of the same name

# The -u options is used to associate the local branch to the remote branch.
git push -u origin <new branch>

To change the branch with modified files

git co --merge <branch>

Modify a commit that you just made

When you come to a commit and it contains errors (forgotten or too wrong message files), it is possible to amend the last commit.

Change the last commit (message files)

git ci --amend -m "message"

You can also undo the last commit while keeping changes in the workspace.

Undo the last commit

git reset --soft HEAD~1

Resetting the work environment

To clean all modified files. Warning, all changes are lost.

Deletes all local changes

git reset --hard HEAD

Remove unversioned files

git clean --force

Make a rebase a branch from the master

Before a branch is merged, it is best to re-apply every commit branch after history master.

Position on the branch to rebase

git co <new branch>

Get the historical branches on the server

git fetch origin

Make the rebase

git rebase origin/master

Conflicts may arise. We must then deal with commit. For each conflict, after editing the file in your favorite editor:

Add conflict resolved files

git add <file1> <file2> ...

We continue the rebase

git rebase --continue

When you go to push the changes on the server, it will reject them because you change the existing historic. It is therefore necessary to force the operation.

Push the rebased branche

git push --force origin <new branch>

Create a commit on behalf of someone else.

When you apply a patch from someone else, you can give him.

git ci -m "Message" --author="Fabrice Sommavilla <fs@physalix.com>"

Remove branch

Where a branch was built, it must be removed from the local repository and remote repository.

Delete locally

git br -d <branch-name>

Delete the server

git push origin: <branch-name>

Updating submodules

Install and update Submodules

#The --init only serves to install the new Submodules
git submodule update --init

Tips

Add and remove all modified / deleted files

git add --all

Delete all files marked as « deleted »

git ls-files --deleted -z | xargs -0 git rm

Put the file chmod 644 instead of 755 before adding the files

git df --summary | grep --color 'mode switches 100644 => 100755' | cut -d '' -f7 - | xargs -d '\n' chmod-x

Squash your commit before asking for merge request

# HEAD~4 is teh HEAD numbers of last commit you want to squash
git rebase -i HEAD~4

Change the last 3 commits into squash when editing files

pick 01d1124 Adding license
squash 6340aaa Moving license into its own file
squash ebfd367 Jekyll has become self-aware.
squash 30e0ccb Changed the tagline in the binary, too.

Move the changes made in a dirty working directory away to work on other branches

git stash

Get the changes back when return to the branch

git stash pop

List all stash on a branche

git stash list

Git is a very powerful tool to manipulate with precaution when working on big project with severals branches. Hope this will help ! Please share !