April 20, 2022

Git // tips

Remove existing files from the repository:

find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

Add this line:

.DS_Store

to the file .gitignore, which can be found at the top level of your repository (or create the file if it isn't there already). You can do this easily with this command in the top directory:

echo .DS_Store >> .gitignore

Then commit the file to the repo:

git add .gitignore
git commit -m '.DS_Store banished!'

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Branches

  • Git branch listing

git branch -a #all local and remote branches are listed git branch -r #remote branches are listed git branch #only local branches are listed

  • Create new git branch with switching to this branch

$ git checkout -b <branch-name>

  • Create new git branch without switching

$ git branch <branch_name>

  • Switch between branches

$ git checkout <branch_name>

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

git rm

The "rm" command helps you to remove files from a Git repository. It allows you to not only delete a file from the repository, but also - if you wish - from the filesystem.

Deleting a file from the filesystem can of course easily be done in many other applications, e.g. a text editor, IDE or file browser. But deleting the file from the actual Git repository is a separate task, for which git rm was made.

Important Options

<filename>

The name of a file (or multiple files) you want to remove. Naming the file you want to remove can be as simple as providing the filename / path to a single file. But you can also provide multiple filenames (delimited by spaces) or even a wildcard pattern (e.g. test.*).

--cached

Removes the file only from the Git repository, but not from the filesystem. By default, the git rm command deletes files both from the Git repository as well as the filesystem. Using the --cached flag, the actual file on disk will not be deleted.

-r

Recursively removes folders. When a path to a directory is specified, the -r flag allows Git to remove that folder including all its contents.

--dry-run

No files are actually removed. With this option (or its shorthand -n notation), you will only see an output of the files that Git would remove - but no files are actually deleted.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

.gitignore