Courtesy: The Git & GitHub Bootcamp - Colt Steele
-
HEAD
refers to a branch reference and not a commit and the branch reference points to a commit- In detached HEAD, it refers to a commit hash
-
gitignore.io - it provides a general git ignore file for whatever programing language we choose
-
git rm -r --cached .
- removes all tracked files from the staging area without deleting them from the local filesystem. Useful when we want to reflect the newly added gitignore or made some changes in it. -
git reset --hard HEAD~1
git push origin HEAD --force
-
git checkout <commit_hash>/ HEAD~1/2/3
- moves in detached HEAD stategit switch <branch_name>
orgit switch
- will re-attach the HEAD
-
- git merge preserves the history
- git rebase re-writes the history
git merge <branch_name>
- merge<branch_name>
into whatever branch you are ingit rebase -i HEAD~4
- interactive rebase a series of commits onto the HEAD(upto 4 commits) in the same branch
-
git clean -n
- to do a dry rungit clean -d
- to clean untracked directoriesgit clean -f
- to force clean
-
-
These public webpages thar are hosted and published via GitHub.
-
These are static webpages means just HTML/CSS/JS and does not support server-side code.
-
Helpful for creating portfolio websites or documentation websites for your repositories.
-
<gh-username>.github.io
- user sites : allowed one per GitHub account. Ex: saurabhssahu.github.io -
<gh-username>.github.io/repo-name
- project sites : https://saurabhssahu.github.io/git/
NOTE: Enable GitHub Pages properly through Settings by routing to the correct directory where the HTML, CSS, and JS code reside.
-
git config --global user.name
git config user.name
- at the root level of project to override the default settingsgit config --global user.email
git config --local user.name
- for local git config (within a repository)git config --global core.editor "code --wait"
- to set VS Code as default editorgit config --global core.excludesfile [file]
- to store global ignore filegit config --global alias.s status
- setting alias for git status. After configuringgit s
will be equivalent togit status
Reference to git aliases
Some common aliases
git a - git add
git b - git branch
git c - git commit
git d - git diff
git remote add <remote_name> <url>
- standard name is origin, or we can give upstreamgit remote set-url origin <new-url>
- to change the remote urlgit remote rename <old> <new>
- to rename remotegit remote remove <name>
- to remove remote
git add -A/--a
- stages all changesgit add .
- stages new files and modifications, without deletions (on the current directory and its subdirectories).git add -u/--update
stages modifications and deletions, without new untracked filesgit add -A
is equivalent togit add .
;git add -u
git log
- retrieves information which is a log of commitsgit log --pretty=oneline,short,medium,full,fuller,reference,email,raw,foramt:<string> and tformat:<string>
git log --oneline
- To create new branches:
git checkout -b <branch_name>
git switch -c <branch_name>
git checkout -
orgit switch -
-to go back to the previous branchgit branch
- list of local branchesgit branch -a
- list of all the branches (local & remote)git branch -m <new_branch_name>
- to rename a branchgit branch -d <branch_name>
- to delete a branch if it has been pushed and merged with remote branchgit branch -D <branch_name>
- to force delete a branch even if it hasn't been pushed and merged with remote branch
git diff
- compares staging area and working directory (only shows the unstaged changes i.e not staged for next commit)git diff HEAD
- shows changes that are staged and unstaged since HEAD/last commit (HEAD is reference to last commit)git diff --staged/--cached
- shows changes between the staging area and the last commitgit diff <branch_1>..<branch_2>
- list the change between the tips of branch 1 and 2git diff <commit_1>..<commit_2>
- list the changes between 2 commits
git push origin <local_branch>:<remote_branch>
- to push a different local branch to a different remote branchgit push --all
- to push all the local branches to remote
git fetch <remote> <branch_name>
- branch name is optional, required only if we want to fetch only a particular branchgit pull
- combination ofgit fetch
followed bygit merge
git stash
orgit stash save
orgit stash -m
- to stash somethinggit stash list
- to list the stashesgit stash apply stash@{2}
- to apply a stashed changegit stash drop stash@{2}
- to delete a stashed changegit stash pop stash@{2}
- combination ofgit stash apply
andgit stash pop
git stash clear
- to delete all the stashed changes
git restore <file_name>
orgit checkout HEAD <file_name>
orgit checkout -- <file_name>
-after making a lot of changes if I want to discard them then I just move back to HEADgit restore .
orgit checkout .
- to restore the current directory.git restore --source HEAD~3 <file_name>
- to select different source, by default HEAD is the sourcegit restore -S/--staged <file_name>
- to unstage a staged file
git reset <commit_hash>
- we move back to old commit but the changes we did will still be there in the working directorygit reset --hard <commit_hash>
- moves back to old commit but changes won't be there
git revert <commit_hash>
- it creates a new commit which undoes the latest commits which we did
git tag <tag_name>
- to create a taggit tag
orgit tag -l
- list all the tagsgit tag -l <some_wildcards>
- to view tags (filter tags with some wildcards) | ex:git tag -l "*beta*"
git diff <tag_1> <tag_2>
- to see the differences between the two tagsgit tag -a <tag_name> -m "some message"
- to create an annotated taggit show <tag_name>
- to see the details of the taggit tag <tagname> <commit>
- To tag some older commitsgit tag -f <tagname>
- to replace/move tags forcefullygit tag -d <tagname>
- to delete tagsgit push origin <tagname>
- to push a single taggit push origin --tags
- to push all the tags
git reflog
- records of when references were updated- can be found under
.git/refs
directory in your repository. - subcommands -
show
,expire
,delete
,exists
- can be found under
NOTE: git log
only includes commits and its parent and so on whereas git reflog
includes history of things ex: clones, check-outs, deleted commits, etc.
-
git reflog show HEAD
- logs of the references when HEAD was changed -
git reflog show <branch_name>
- logs of the references when -
git reflog show HEAD@{10}
- shows all the reference logs from HEADs position 10 moves ago till its position at the beginning of the repository.HEAD{0}
- refers to the most recent entry in the HEAD reflog
-
git checkout HEAD@{2}
- it takes us back where HEAD was 2 steps before (need not be same branch or can be same branch and even same commit) -
git checkout HEAD~2
- moves the HEAD back to 2 commits -
git reflog master@{one.week.ago]}
git reflog master@{1.day.ago}
git reflog master@{yesterday}
There are 4 types of git objects stored in the objects (/.git/objects) directory in your repository:
- blob - binary large object, git uses to store the contents of the files (they don't include the file name)
- tree - are git objects used to store the content of the directories. Trees contains pointers that can refer to blobs and to other trees
- commit - it combines a tree object along with information about the context that led to the current tree. Commit stores - a reference to a tree, a reference to parent commit, the author, the commiter and the commit message
- annotated tag - it is a pointer to a particular commit
- A
annotated tag
points to a particularcommit
.- A
commit
points to atree
.- A
tree
can point to many blobs and other trees
git hash-object <file>
- command that provides the hash result using SHA-1 encryption for the content of the file
Example of git hash-object
command
echo "some-text" | git hash-object --stdin
git cat-file -p <object-hash>
- To retrieve the data from a hashgit cat-file -t <object-hash>
- tells the type of the object (blob/ tree/ commit/ annotated tag)git cat-file -p main^{tree}
- command for viewing tree. the syntax main^{tree} specifies the tree object that is pointed to by the tip of our branch 9in this casemain
).