I'm posting these reminders here so they're all in one place, because yes I do forget some of these, a lot...
Change author for last commit
git commit --amend --author="John Doe <firstname.lastname@example.org>"
Git offers a really simple way to do this, I guess a rebase happens behind the scenes but this is a less scary way to do it.
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.
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.
git push -u origin new-feature
This will push the new feature branch up to the remote origin such as github (other git services are available, but let's face it, not as good). The -u flag tells git to consider the origin the upsteam branch to track.
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.
Merge (squash) previous commits into one
git rebase -i HEAD~2
This is a really useful way to disguise an ugly trail of commits like ('fixed it..... fixed it again...... three times a fix......this really should fix it') etc.
Essentially all that you're doing here is telling git to go backwards from HEAD - 2 commits and recommit them (rebasing is the correct term I think, given the command name it's a fair guess). The recommit process will allow you to tell it which ones to 'squash' into the others. Generally I just squash all commits beneath the top one and this works, because ultimately git rebase scares me and I know that this works so why risk anything else.
Add Remote Tracking
git branch -u upstream/foo
This will track the remote branch upstream/foo from the current branch, add the required local branch after upstream/foo if not tracking from the current branch.
Published: May 19, 2017 (10 months ago)