Git

Add previously ignore files to git

Lets assume we had a project that was ignoring the /proj/mymodule directory.

 

To add it to git for tracking. We need to make sure it is not a submodule. A submodule is another git project that is within our main project. If that is the case we need to delete the .git/ folder in /proj/mymodule.

Next we need to stop ignoring the file, ex, by removing it from our .gitignore file.

 

Clear the git index. (may not really be needed)

Recover files deleted in git

Recover Files Deleted From Drive But Not Committed Yet

If you removed a version file from your computer's harddrive but did not yet commit the changes you can recover the files with the following command

#Syntax: Recover
#git checkout HEAD -- FILE_OR_FOLDER_NAME

#restore a deleted folder
git checkout HEAD -- sites/all/modules/contrib/mymodule

#restore a deleted file
git checkout HEAD -- sites/a.txt

 

git ls-tree(List Git Tree)

GIT ls-tree Command

This command works similar to the ls commands on windows, linux and mac. It takes many parameter the most important being a treeish.
List the tree using the HEAD. Remember, the head points to the last commit on the current branch.

git ls-tree HEAD


List Directory Content

In this example we are listing the content of the src directory.

Treeish

Treeish

A Treeish is just a way for us to reference a commit in the git tree in a convient way. These treeish allows us to reference a parent commit with having to know its commit hash id.
Example Usage:
#get the parent commit of the current head
git checkout HEAD^
#get the grand parent commit of the current head
git checkout HEAD^^

Parent Commit

  1. HEAD^          - References the parent of the current HEADd
  2. ab23cda32^   -References the parent of the commit with the given ID

.gitkeep (Track Empty Directory)

Track Empty Directory

Does does not track empty directory. In order to track a directory, a file must exist within it. A trick that many people use is to create a hidden file usually called .gitkeep. The name of the file doesn't matter. As long as there is a file in the directory .gitkeep, readme.txt etc, git will track directory.

If you have a empty directory that you'd like to track, create a hidden file. ex ".gitkeep" and commit it to the repository
touch .gitkeep.
git add .gitkeep
git commit -am "adding hidden file to track empty folder"

Git Reset

Git Reset

The git reset command is a very power and very dangerous command. It allows you to move the HEAD pointer to any position. You could for example, move the head power to any previous commit(rewind). Once you perform a commit(recording) all the commits that came after HEAD will be lost forever.

Types Of Reset

There are 3 types of reset.

Get Old Version Of Files

Get Previous Version

Let's say we've made a commit a few days and we've since made many more commits. However, what if we'd like to get the version that we had a few days ago. We could use git log to look up the commit id. The commit id can then be used to checkout out the specific file from the given commit. Note: we don't need to use the full ID, 15 or so digit should be uinquie enough for git to figure it out.
Once we checkout the file, it will be placed on the index(staged). Ready to be committed.

Modify or Revert Commit

Modfiying A Git Commit

Git places restrictions on what type of modification we can make to commits already in the repository. Only the last commit in the respository can be altered. This is because every commit is linked to the previous commit via the SHA1 commit 1. When you alter a commit the SHA1 ID changes. This would cause a ripple in the history where altering one commit would wire altering any commits above it. The last commit can be alter because no other commits point to it yet. To modify the commit we can use the --amend argument.

Pages