Git commands I use a lot but can't remember!

By Gavin Phillips @ Cowshed Works Ltd

Blog Picture

I'm posting these reminders here so they're all in one place, because yes I do forget some of these, a lot...

Remove all deleted files before staging


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

This just lists all deleted files and pipes them through to git rm, very useful when you've been a bit busy on the deletions.

See current config settings

git config -l

See all config settings.

Clone a remote repo

git clone https://github.com/user/repository.git

Clone the remote repository into current directory.

List remote urls

git remote -v

Show a list of all of the remotes for this repo.

Change message for last commit

git commit --amend -m 'New message goes here'

Useful for when you've missed something from the commit message or you've just ballsed it up.

Show diff for last commit

git log -p -1

Lists the diffs for the most recent commit

Show log on single line for last 5 commits

git log --pretty=oneline -5

Lists the last 5 commits on a single line, a lot more readable.

Revert to previous commit removing all work since

git reset --hard COMMITID

Resets to COMMITID and removes all work since in the worktree.

git clean -f -d

Removes all untracked files, use this after a git reset to remove new files, git reset will have reverted to previous commit versions of the files but won't delete any new files, git clean does this.

Branching

git branch new-feature

Create a new branch called new-feature.

git checkout new-feature

Checkout the new feature branch ready for working on it.

git checkout -b new-feature

This combines the above two and creates the feature branch then checks it out.

git checkout master

Checkout master again for fixes etc whilst working on new-feature.

Merging

git merge new-feature

Merge the new-feature branch into the current branch. Reminder, this merges into the current branch!

git merge --no-ff new-feature

Merge new-feature into current branch and also generate a merge commit.

Author: Gavin Phillips
Published: May 19, 2017 (2 months ago)

Work with Cowshed:Works

Ready to start your next website or app project? Give us a call or send us an email.



© Cowshed Works Ltd: UK Staffordshire-based website design, build and management

VAT: 251 4480 22 - Registered in England: 10399485

Terms: Website | Service