Commit Changes To Repo / Check-In Changes

Discard A Locally Modified File With Latest File On Repo / Revert Local Changes To Use Repo Latest

For single file just use checkout command

git checkout [filename]

For discarding, all local un-committed changes use below command

git checkout -- .

How To Create An Empty Directory In Git Repo

Git does not allow an empty directory to be committed in the repo. Therefore we can use a hack for it. To create an empty directory you need to add a file called .gitkeep in the empty directory (which means it will not remain empty) You can use touch command to create empty file

cd DirectoryToKeepEmpty
touch .gitkeep

How To Export Repository Sources In A Zip File

You can use git archive command option for this, below command will archive all source from the master branch. This will export a zip file name myarchive.zip that will contain only project files (no git meta files will be included)

git archive --format zip --output myarchive.zip master

How To Change Commit Message Of Locally Committed Changes

You can change the last commit message using amend command

git commit --amend -m "New commit message"

IF you need to make changes to several commits including commits already pushed to remote then use rebase command like this.

The first step, check the output of git log to find out how many commits need to be modified.

git log --oneline

Make a note of the 'oldest commit' to be modified (let us say its 5th commit)

git rebase --interactive HEAD~5

This will pop an editor for you to make all commit message changes in one single file. Make all necessary changes and save the file. After this continue the rebase using below command.

git rebase --continue

How To Create A Patch From Local Repo Changes In Git

Creating patch is very easy in git. You can use diff command to do it. For local un-committed (un-staged) changes use below command

git diff > filename.patch

For locally committed changes (staged changes) use below command

git diff --cached > filename.patch

How To Apply Git Patch

Use apply command

git apply filename.patch

Please note that I have used .patch as file name extension. This is just a convention, it can be any extension or no extension at all. Git does not care about the file extension for apply command.

How To Compare A File With A Past Revision

This is easy in git, just do a diff on the version like below. This command will compare the current latest version on your local repo to version cf39cd1

git diff cf39cd1 filename

How To Remove Untracked Files

Use git clean with force option, Below command will delete all untracked files and directories in your local working copy.

git clean -f -d

You can use git status command to check before and after to confirm. Not using force option may show you this error