Git Untrack Already Tracked Files

To remove files that are currently being tracked by git, you have to remove them from the “cache”. Note, doing this will NOT delete the file on your local machine. It will still be there but not be tracked.

 git rm -r --cached supersecretpasswords.txt 

You then need to add the file to the .gitignore file in the root of the project so that it isn’t tracked again on your next commit.


vim .gitignore
supersecretpasswords.txt

.gitignore files are tracked so remember to check in these changes.

git commit -am'my super secret passwords are safe!'

If you want to completely delete the file, on your local machine and from git

git rm supersecretpasswords.txt

If you’re working with a directory remember to add the -r flag for recursive removal!

How to Remove Your Last Git Commit

Remove your last commit (if you haven’t pushed yet)

git reset --hard HEAD~1

To see changes that have been committed and their position in HEAD

git reflog 

And to undo your previous reset and advance the cursor to the reference immediately behind the current state

git reset --hard HEAD@{1}

If you have already pushed you can

git revert HEAD

which will reverse your last commit by creating a new commit

Working with Branches in Git

Show all the branches

git branch

Create a new branch

git branch my_experimental_feature

Use that branch

git checkout my_experimental_feature

Pushing the new branch to a remote server

git push origin my_experimental_feature

Pulling that branch down on another machine

git pull origin my_experimental_feature

Listing all branches on other machine

git branch -a

Updating other machine

git pull origin my_experimental_feature

git checkout — file-in-question.oh-my

Problem: using git and one file, like your db/schema.rb, is out of whack with the latest branch. If you run git pull and you get a failed merge and no update. You can of course, edit the conflicts manually. But what if you’re confident that the latest schema.rb is correct? Instead of manually editing the file and doing all that work, you can grab the latest copy with the git checkout command.

Solution:  git checkout — <file>
… or for the Rails application example above…
git checkout — db/schema.rb

the simple things with git

Git can track the file permissions on your source. This can be good, but for small projects on a webserver where permissions change from time to time, and permissions on development app don’t match or matter anyway, it’s often easier to just skip this check alltogether. For the application in question look in the .git directory in the application root. You’ll see a config file with the application specific directives. Open the file with your editor of choice and change the filemode to false. It’s set to true by default.

[core]
repositoryformatversion = 0
filemode = false