Git - Staging area, adding and removing commits
Table of Contents
https://git-scm.com/about/staging-area
Initializing or cloning repositories
command | description |
---|---|
git init (project name) |
Creates a new local repository with the specified name |
git clone (url) |
downloads a project and its entire version history |
Staging changes
command | description |
---|---|
git add . |
To stage all changed files from your current location and it’s child directories |
git add -A |
To stage all changed files in the git repo (including parent directories) |
git rm --cached <file> |
To remove a tracked file from index |
git rm -r --cached <directory-name>> |
To recursively remove the tree rooted at directory-name from the index. Don’t forget to update .gitignore |
git add (fileName) |
To stage individual changed files |
~git checkout! | To copy a file from some other commit to your current working tree. It doesn’t automatically commit the file. (Switch branches or restore working tree files) |
git checkout fileName |
Command to undo changes to a single file in working directory |
git commit -m "commit message" |
To commit with a custom message |
GIT LOG --ONELINE |
To view recent commit messages |
Unstaging changes
command | description |
---|---|
git reset . |
If there are some changes in staging area and we want to unstage everything |
Clean-up
command | description |
---|---|
git clean |
To remove local untracked files from the current Git branch |
git clean -n |
To see which files will be deleted you can use the -n option before you run the actual command |
git clean -f |
When you are comfortable (because it will delete the files for real!) use the -f option |
git clean -f -d or git clean -fd |
To remove directories |
git clean -f -X or git clean -fX (Note the case difference on the X) |
To remove ignored files |
git clean -f -x or git clean -fx (Note the case difference on the X) |
To remove ignored and non-ignored files |
git clean -f -x -d |
Reindexing the repo
If you have a file that you want to add to the .gitignore file, but is already in the git index, you will notice it can be a pain to remove it as git will ignore your ignore command for this file. To work around this, you can re-index the repo
git rm -r --cached .
git add .
git commit -m "re-indexed"
This may look scary at first as it kicks every single file out of the repo and restages them. But dont worry, your change set will only show the files you are now not indexing.