Colmjude

/ notes
Notes
A collection of thoughts, things I've learnt and things I want to remember
(Updated )

Useful Git commands

Git commands I find useful.

Stage only some changes in a file

Use git add -p (short for --patch) to review the changes in a file one hunk at a time and choose which ones to stage:

git add -p data/analysis/applications/submissions/DAC/26_01163_DRC/submission.json

Git will show each hunk and ask what to do with it. The most useful options are:

  • y — stage this hunk
  • n — do not stage this hunk
  • s — split the hunk into smaller hunks
  • e — manually edit the hunk
  • q — stop reviewing

This is useful when a file contains changes that belong in different commits.

Remove files from git without deleting them (aka keep a local copy)

git rm --cached <file>

Add -r flag removing a <folder>.

Remove untracked filed

Use this command

git clean -f -d

-f forces it to remove untracked files -d will make it remove the untracked directories

Reset changed files

Sometimes files have changed but you don't want to commit the changes. These changed files might also stop you pulling the latest changes from github. In this case you want to remove or reset the changes.

One way to do it is by checking out the file. This will checkout the version from the last commit.

# this will reset a single file
git checkout <file>

# this will reset a whole directory
git checkout <dir>

I've set up an alias so I use

git co <dir/file>