Git commands I wish I knew early

Github Octocat logo

It has been a little more than a decade since I started coding.

Started career in this industry as a web designer, then moved on to become front-end developer with savvy HTML and CSS tricks. Later around 2012 started renaming all resume and online profiles to claim to be JavaScript developer – with Vanilla JS, AngularJS blah.. blah.. blah… and then now a full-stack developer. Heck!!!

One simple thing has remained consistent even though I have moved on from position to position, shifting focus from design to programming, changing primary language from Photoshop to PHP to JavaScript – is the tools I use to work.

Git and Github, in particular, has played a major role in the career. Along with its useful features – it brought many nightmares as well.

Committing passwords, secret keys, unwanted changes etc. – I have had my fair share of embarrassments! I wish I knew more than `git push` and `git pull`.

Git commands I wish I knew early

  1. Ignore changes from tracked files, temporarily
  2. Undo local changes
  3. Remove file from remote Git repository but keep locally
  4. Remove git tracking entirely from a project

1. Temporarily ignore changes from tracked files

Tell Git to stop tracking changes

git update-index --assume-unchanged filename.ext 

and to start tracking again –

git update-index --no-assume-unchanged filename.ext 

2. Undo All Local Changes

Please note the dot, meaning reset everything to last pull state in the current directory.

 git checkout . 

If you just wish to reset one file, replace the dot with filename.ext

3. Remove file from remote Git repository but keep locally

You join the project in the sometimes in the middle of dev activities. The File is already on the remote repository – let us say ‘.env’ or my ‘secret.keys’.

Following command to rescue:

git rm --cached filename.ext 

Or if you have to remove an entire directory, .e.g. `fakedata/` then:

git rm --cached -r directoryName 

Above, “-r” means, recursively. 

4. Remove git tracking entirely from a project

Let’s say you cloned a git repo, some boilerplate or starter template but it has all the tracking pointed towards its author git repository.

When you need to start fresh and remove the old git tracking and initialize new git repo, do following:

rm -rf .git

Example above, “-rf” means recursively force the command. Git stores all tracking data inside hidden “.git” directory. All we did here is deleted that directory.

Now if you wish to start fresh git repo on this same directory – “git init” should do.

Tips

Take a look on StackOverflow for top voted questions.

https://stackoverflow.com/questions/tagged/git?sort=votes&pageSize=15