Skip to content

Instantly share code, notes, and snippets.

@loru88
Last active January 31, 2019 14:56
Show Gist options
  • Save loru88/df9226dae16d8ab3321556a948942f22 to your computer and use it in GitHub Desktop.
Save loru88/df9226dae16d8ab3321556a948942f22 to your computer and use it in GitHub Desktop.
Command line snippets for common Git tasks

Stage only parts of a file

$ git add --patch <filename> #(or -p for short),

Source

a series of handy git command I use often

Dry-run a merge

Performing a git merge with no commit or no fast-forward will merge the two code bases together. This will allow you to examine, test, and undo the merge if required.

$ git merge --no-commit --no-ff <branch-name>

If you need to undo the commit you can use:

$ git merge --abort

This will return git to its state before the merge occurred.

Source

Howto add a changes to an older (not last) commit in Git

Source

  1. Use git rebase -i HEAD~10 (or whatever you need to see far enough back).

  2. Mark the commit in question (a0865...) for edit by changing the word pick at the start of the line into edit. Don't delete the other lines as that would delete the commits

  3. Save the rebase file, and git will drop back to the shell and wait for you to fix that commit.

4a. to edit a pre existing commit

  1. Add your file with git add
  2. Amend the commit with git commit --amend

4b. to split an older commit

  1. git reset HEAD~ to delete the last commit and restore files in the index.

  2. git add and git commit the files you want in as many commit as you need.

  3. Do a git rebase --continue which will rewrite the rest of your commits against the new one. Follow steps 2 to 6 if you have marked more than one commit for edit.

Reset

reset last commit but keep changes in staging

$ git reset --soft HEAD^

Delete

last commit

$ git reset --hard HEAD^

delete multiple commits from the top, for example the last 2

$ git reset --hard HEAD~2

Source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment