DEV Community

Koen van Gilst
Koen van Gilst

Posted on • Originally published at blog.koenvangilst.nl on

Forgit me nots

Squashing commits on Windows

I know there’s a better way to do this (using rebase) but I like this way since it gives me more control.

git merge-base master yourBranch

# note commit hash

git reset --soft # enter commit hash

# all changes can now be committed again
Enter fullscreen mode Exit fullscreen mode

It’s easier on *nix using the following oneliner:

git reset $(git merge-base master $(git rev-parse --abbrev-ref HEAD))
Enter fullscreen mode Exit fullscreen mode

Rebasing without pulling master

Rebase using the remote master to make sure you’re really up to date. Useful when updating merge requests (on GitLab).

git pull --rebase origin master
Enter fullscreen mode Exit fullscreen mode

Copy file from master to current branch

git checkout master -- yarn.lock
Enter fullscreen mode Exit fullscreen mode

Useful aliases

Add all and commit

ac=!git add . && git commit
Enter fullscreen mode Exit fullscreen mode

Pretty log

ls = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
Enter fullscreen mode Exit fullscreen mode

Update last commit with current changes

amen = !git add --all && git commit --amend
Enter fullscreen mode Exit fullscreen mode

Undo last commit

git reset --soft HEAD^
Enter fullscreen mode Exit fullscreen mode

Or add as an alias:

git config --global alias.undo 'reset --soft HEAD^'
Enter fullscreen mode Exit fullscreen mode

Oh no, I deleted all my work!?

Use the following command:

git reflog
Enter fullscreen mode Exit fullscreen mode

and cherry-pick any commits you think could still have all your work.

If .gitignore is not working

Do:

git rm -r --cached .
Enter fullscreen mode Exit fullscreen mode

And after that:

git add --all
Enter fullscreen mode Exit fullscreen mode

After Rebasing

Instead of using -f or —force developers should use

git push --force-with-lease
Enter fullscreen mode Exit fullscreen mode

Why? Because it checks the remote branch for changes which is absolutely a good idea. Let’s imagine that James and Lisa are working on the same feature branch and Lisa has pushed a commit. James now rebases his local branch and is rejected when trying to push. Of course James thinks this is due to rebase and uses —force and would rewrite all Lisa’s changes. If James had used —force-with-lease he would have received a warning that there are commits done by someone else. I don’t see why anyone would use —force instead of —force-with-lease when pushing after a rebase.

Source: https://stackoverflow.com/questions/8939977/git-push-rejected-after-feature-branch-rebase

Top comments (0)