Mohan pd.
Mohan pd. Author of The Coders Blog. Also a professional fullstack javascript developer working with various tech company and client around world.

Useful git commands to become git quickie.

Sep 10, 2019 · 4 minutes
Useful git commands to become git quickie.

How to fix the last commit I made?

Make your corrections and stage them with git add, then: $ git commit --amend Add -a to automatically stage all changes to tracked files (skip‐ ping git add). Add -C HEAD to reuse the previous commit message without stopping to edit it.

How to edit the previous n commits?

$ git rebase -i HEAD~n The history involved should be linear. You can add -p to preserve merge commits, but this can get tricky depending on the changes you want to make.

How to undo my last n commits?

$ git reset HEAD~n This removes the last n commits of a linear history from the current branch, leaving the corresponding changes in your working files. You can add --hard to make the working tree reflect the new branch tip, but beware: this will also discard any current uncommitted changes, which you will lose with no recourse.

How to reuse the message from an existing commit?

$ git commit --reset-author -C rev Add --edit to edit the message before committing

How to reapply an existing commit from another branch?

$ git cherry-pick rev If the commit is in a different local repository, ~/other: ``` $ git –git-dir ~/other/.git format-patch -1 –stdout rev | git am ``

How to list files with conflicts when merging?

git status shows these as part of its report, but to just list their names: $ git diff --name-only --diff-filter=U

How to Get a summary of my branches?

List local branches: git branch List all branches: git branch -a Get a compact summary of local branches and status with respect to their upstream counterparts: git branch -vv Get detail about the remote as well: git remote show origin (or other named remote)

How to get a summary of my working tree and index state?

$ git status Add -sb for a more compact listing

How to stage all the current changes to my working files?

$ git add -A This does git add for every changed, new, and deleted file in your working tree. Add --force to include normally ignored files; you might do this when adding a new release to a “vendor branch,” which tracks updates to other projects you obtain by means other than Git (e.g., tarballs).

How to how the changes to my working files?

git diff shows unstaged changes; add --stage to see staged changes instead. Add --name-only or --name-status for a more compact listing

How to save and restore my working tree and index changes?

shell git stash saves and sets your outstanding changes aside, so you can perform other operations that might be blocked by them, such as checking out a different branch. You can restore your changes later with git stash pop.

How to add a downstream branch without checking it out?

shell $ git branch foo origin/foo This adds a local branch and sets up push/pull tracking as if you had done git checkout foo, but does not do the checkout or change your current branch.

How to list the files in a specific commit?

shell $ git ls-tree -r --name-only rev This listing is restricted to the current directory; add --fulltree for a complete list.

How to show the changes made by a commit?

git show rev is easier than git diff rev~ rev, and shows the author, timestamp, commit ID, and message as well. Add -s to suppress the diff and just see the latter information; use --namestatus or --stat to summarize the changes. It also works for merge commits, showing conflicts from the merge as with git log --cc

How to list all remotes?

git remote does this; add -v to see the corresponding URLs con‐ figured for push and pull (ordinarily the same): shell $ git remote -v

How to change the URL for a remote?

shell $ git remote set-url remote URL

How to remove old remote-tracking branches?

$ git remote prune origin This removes tracking for remote branches that have been deleted upstream.

How to have git log: Find commits I made but Lost?

perhaps after editing history with git rebase -i or git re set, or deleting a branch: $ git log -g

Not Show the diffs for root commits?

A root commit always shows the addition of all the files in its tree, which can be a large and uninformative list; you can suppress this with: $ git config [--global] log.showroot false

Show the changes for each commit?

git log -p shows the complete patch for each commit it lists, while these options summarize the changes in different ways: $ git log --name-status $ git log --stat

Show the committer as well as the author?

$ git log