DEV Community

Cover image for My GIT cookbook
Alain Mauri
Alain Mauri

Posted on • Updated on

My GIT cookbook

A collection of commands I often use

This is a work in progress collection of common and less common GIT command I found useful in my day to day job.

Logging

# compact log and graph
git log --oneline --graph

# log actual changes in a file
git log -p <filename>

# log changes not yet merged to the parent branch
git log --no-merges <parent-branch>..

Branches management

# if you want to rename a branch
git branch -m <oldname> <newname>
# if you want to rename the current branch
git branch -m <newname>
# push and delete the old branch
git push <remote> --delete <old_name>
# or
git push <remote> :<oldname>
# push the new branch and reset the upstream branch
git push <remote> -u <newname>

# Check available local and remote branches
git branch -v -r

# Change the URL of a remote
git remote --set-url <remote URL>

# Remove old remote tracking branches
git remote prune origin

Committing changes

Multi-Line message with commit

To write a multi-line commit without opening the editor, use the -m option using single or double quotes, without closing them. Once you finish typing, close the quotes and you will have your nice message.

git commit -m 'Feature: Add ignore files to rubocop
quote>
quote>
* Added routes.rb to rubocop ignore file'
[branchname 387aa73] Feature: Add ignore files to rubocop
1 file changed, 1 insertion(+)

Commit message using the editor and a template

If you want to use an editor for the commit and prefer to have a template that will be consistent all across your commits you can drop a .gitmessage file in your home folder with something similar in it:

Refactoring/Bugfix/Feature: Title
* Description

An then tell GIT that you want to use that file, passing its absolute path, otherwise it will be interpreted as relative:

git config commit.template /absolute/path/to/.gitmessage

miscellaneous

# fix the last made commit
git commit --amend
# edit the previous n commits
git rebase -i HEAD~n
# undo the last n commits
git reset HEAD~n

General

List Files with conflicts when merging

git diff --name-only --diff-filter=U

Stop tracking files added to .gitignore

Sometimes happens that after adding a file or a folder to .gitignore you're still seeing it in a git status.
What you have to do is removing the files or the folder from the cache using:

git rm --cached <filename>
# or
git rm --cached -r /path/to/folder/**/*

This list will probably increase as soon as I will learn something new, so...

Stay Tuned!

Discussion (0)