Git Intermediate
Whats inside .git directory ?
- COMMIT_EDITMSG : This is the last commit’s message. It’s not actually used by Git at all, but it’s there mostly for your reference after you made a commit.
- Head : The current ref that you are looking at. In most cases its probably refs/heads/master.
- Config : Contains settings for this repository. What this file is most used for is defining where remote live and some core settings.
- description : If you are using gitweb or firing up git instaweb, this will show up when you view your repository or the list of all versioned repositories.
- hooks : this contains scripts that are executed at certain times when working with Git, such as after a commit.
- index : The staging area with meta-data such as timestamps, file names and also SHAs of the files that are already wrapped up by Git
Info : you can use this file to ignore files for this project, but beware! It’s not versioned like a .gitignore file would be.
logs : Contains history for different branches. Seems to be used mostly with the reflog command.
objects : Git’s internal warehouse of blobs, all indexed by SHAs.
refs : The master copy of all refs that live in your repository.
Just one word of wisdom when messing around with git internals
make sure you know what you are doing and if not have a backup.
.gitignore file
If you create a file in your repository named .gitignore, Git uses it to determine which files and directories to ignore, before you make a commit.
A .gitignore file should be committed into your repository, in order to share the ignore rules with any other users that clone the repository.
Changing a git message
git commit --amend -m "new message"
Unmodifying a modified file
git checkout <filename>
Untrack a tracked file
git rm --cached c.txt
Unstaging staged file
git reset HEAD c.txt
What changes you have made from last commit
git diff
What changes you have made from last commit for a particular file
git diff <filename>
Merge a branch into another
git merge <branch-name>
Note: always use --no-ff option which automatically generates a commit messege for merge
list merged branches
git branch --merged
lists branches that have not been merged
git branch --no-merged
Revert to a commit : The git revert command undoes a committed snapshot. But, instead of removing the commit from the project history.
git revert <commit-hash>
Reset to a commit: When you undo with git reset there is no way to retrieve the original copy—it is a permanent undo
git reset <commit-hash>
Options with reset
--soft : staged changes ready to be committed
--mixed : Unstaged changes ready to be changed and commited
--hard : you will not be able to see changes anywhere
cherry-pick
Cherry picking in git means to choose a commit from one branch and apply it onto another.
This is in contrast with other ways such as merge and rebase which normally applies many commits onto a another branch.
-
Make sure you are on the branch you want apply the commit to.
git checkout master
-
Execute the following:
git cherry-pick <commit-hash>
Stash
Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory
git stash
Show the last stashed
git stash show
Show last stash with changes
git stash show -p
List all the stashes
git stash list
Show changes of a particular stash
git stash show -p stash-id
Apply a recent stash
git stash pop
Apply a particular stash
git stash pop stash-id
Create a tag in git
git tag -a v1.0 -m "my version 1.0"
List all tags in git
git tag
Show latest commit from a tag
git show <mytag>
See logs of a git tag
git log -1 <mytag>
checkout tag
git checkout -b tags/<tag>
Pushing tags to the remote
git push origin tag
Pushing all tags to remote
git push origin --tags
list tags along with full messages
git tag -n9
Rebase
As an alternative to merging, you can rebase the feature branch onto master branch using the following commands:
git checkout feature
git rebase master
This moves the entire feature branch to begin on the tip of the master branch, effectively incorporating all of the new commits in master. In case of conflicts resolve it add the changed file and continue with git rebase --continue.You can also squash commits using the command below.
git rebase -i HEAD~3
Replace "pick" on the second and subsequent commits with "squash"
git blame
Annotates each line in the given file with information from the revision which last modified the line. Optionally, start annotating from the given revision.
git blame a.txt
git blame -l a.txt
git blame -L 3,6 a.txt
git fetch
In the simplest terms, git pull does a git fetch followed by a git merge. You can do a git fetch at any time to update your remote-tracking.
git fetch origin <branch-name>
Git Intermediate
By Pulkit Pushkarna
Git Intermediate
- 1,079