diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 0000000..9ad9c1c --- /dev/null +++ b/docs/index.html @@ -0,0 +1,1035 @@ + + + + + Read Me + + + + + + +
+

Git cheat sheet

+ + + + + + + + + +
gitversion >= 2.28
+

Basic git setup :

+

Git config:

+

Set Global config (Omit --global to lit config to current repo)

+
These will be reflected as Author details in each commit you make
+ + + + + + + + + + + + + + + + + +
CommandDescription
git config --global user.name "your_username"Author
git config --global user.email "your_email"Author email
+ + + + + + + + + + + + + + + + + + + + + +
CommandDescription
git config --global core.editor "vim"Change editor to vim
git config --global core.ignorecase falseDetect changes in filename case change
git config --global init.templatedir '~/.git_templateSet git template dir (to configure hooks)
+

Git alias to create custom command shortcuts

+ + + + + + + + + + + + + + + + + +
CommandDescription
git config --global alias.<short-alias> 'full command'Create a new git alias in the form of a new git command
git config --global alias.ucc 'commit --amend --no-edit'Run git ucc instead of git commit --amend --no-edit
+

Unset config:

+ + + + + + + + + + + + + + + + + +
CommandDescription
git config --global --unset <config-key>Unset global config
ex: git config --global --unset user.name
git config --unset <config-key>Unset local config
ex: git config --unset user.name
+

Troubleshoot git config

+ + + + + + + + + + + + + +
CommandDescription
git config --list --show-originList all the configs and their location
+

More operations on config

+

Adding git to the project:

+ + + + + + + + + + + + + + + + + + + + + +
CommandDescription
git init .Initialize git inside the project root directory.
git add .Stage all files for commit when run from the root directory
git commit -m "commit-message"Commit staged files to git.
+

Adding Remote repo to Project:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CommandDescription
git remote add <remote_name> <remote_address>Add remote repository where
remote_name: origin,upstream etc
remote_address:(URL) Github, Bitbucket etc
git remote set-url origin <remote_address>Update remote address
git remote rename origin upstreamRename remote from origin to upstream
git remote remove <remote_name>Delete remote
git remote rename origin upstreamRename remote
git push <remote_name> <branch-name>Push all the commits to remote default branch name master
+

Synchronizing local with remote (upstream):

+ + + + + + + + + + + + + + + + + + + + + +
CommandDescription
git fetchDownload changes from remote (default origin) without making any changes to files in the working directory.
git pullDownload remote commits from the origin repository configured as upstream for the current branch.
git pushUpload local commits to remote.
+

Branches in git:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CommandDescription
git branch -aShow all branches.
git branch <new_branch_name>Create a new branch (Will not switch working tree to new branch).
git checkout -b <new_branch_name>Create a new branch out of the current branch and switch to the new branch.
git checkout --orphan <new_branch_name>Create a new branch without any parent commit (empty branch)
git branch -m <old_branch_name> <new_branch_name>Rename branch.
git push <remote_name> <branch_name>Push local branch to remote.
git checkout <branch_name>Switch to specified branch.
git branch -d <branch_name>Delete local branch (replace -d by -D for force delete)
git push <remote_name> :<branch_name>Delete remote branch.
git branch --contains <commit_id>Search branches containing the given commit id
+

Git stash:

+
Save current changes without committing before doing pull and checkout to another branch
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CommandDescription
git stash save "message"Save changes with give message after running git add <path>
git stash listShow stash list
git stash popApply the last stash to the current branch and delete stash
git stash apply stash@{n}Apply nth stash without deleting from stash
git checkout stash@{n} -- <filename>check out a file from nth stash
git stash clearClear all stash
git stash show -pview last stash content
git stash show -p stash@{n}view nth stash content
+

Commits in git:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CommandDescription
git commit --amendEdit last commit message.
git revert <commit_id>Rollback to the commit specified by commit_id
git rebase -i HEAD~nOpens list of the last n commits,
replace "pick" by "reword" to alter the commit message of those commits.
git reset --soft HEAD~nUncommit the last n commits without overwriting the working directory files.
git reset --hard HEAD~nUncommit last n changes and overwrite the working directory files
git cherry-pick <commit_id>Pull commit from any branch by its commit id into the current branch
git rev-parse --short HEADPrint last commit id
git commit --all --amend --no-editAdd current changes to last commit
git merge-base <branch A> <branch B>Get the latest common commit ID between branch A and branch B
+ +

Merging in git :

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CommandDescription
git merge <branch_name>Merge any branch into current branch
git merge <branch-name> --no-commitMerge without merge commit.
git pull <remote_name> <branch_name>Pull from remote branch and merge
git checkout <branch_name> -- <file_name>Checkout single file from any branch
git checkout <branch_name> -- .Checkout all the files from any branch
+

Fix diverged branches:

+ + + + + + + + + + + + + + + + + +
OptionCommand
1git rebase
2git rebase <remote_name>/<branch_name>
+

Caching in git :

+
Run these commands after updating the .gitignore file and then commit the changes.
+ + + + + + + + + + + + + + + + + +
CommandDescription
git rm --cached <filename>Remove specific file from git cache
git rm -r --cached .Remove all files from git cache
+

Tags in git

+

Taggign is widely used to create releases and mark milestones

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CommandDescription
git tag -lList all the tags
git tag -a <tagname> -m <message>Create an annotated tag, with detailed info about the tagger
git tag <tagname>Create a lightweight tag, tag with only a reference to commit
git tag -l "v1.0.*"List all the tags matching given pattern
git show <tagname>View tag details
git push origin <tagname>Transfer tag to remote
git push origin --tagsTransfer all local tags to remote
git tag -d <tagname>Delete local tag
git push origin --delete <tagname>Delete remote tag
+

Upstream in git :

+
Upstream is used when we have multiple repositories for a single project
+ + + + + + + + + + + + + + + + + + + + + +
CommandDescription
git branch --set-upstream-to=<remote_name>/<remote_branch_name> <local_branch_name>Set new upstream for the branch.
git branch --set-upstream-to=<remote_name>/<remote_branch_name>Set new upstream for the current branch.
git push -u <remote_name> <branch_name>Push to remote branch and set as upstream
+

List commits in git (log):

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CommandDescription
git logList all commits (comprehensive)
git log --onelineLit all commits (brief)
git log -nThe n is number of commits to list
git log --all --grep='<message content>'List/Find commits by message content
git log master..newBranchList commits that newBranch has but master doesn't
+

Git diff

+

Note: ref1 and ref2 could be branchNames, remoteName/branchName, commit SHAs, etc

+ + + + + + + + + + + + + + + + + + + + + +
CommandDescription
git diff ref1:path/to/file1 ref2:path/to/file2Show difference between two references
git diff -- path/to/file ... originShow file difference between the local file and remote file of the current branch
git diff branch_name:path/to/fileSimplified version of the above command
+

Git patch

+

without author info:

+ + + + + + + + + + + + + + + + + +
CommandDescription
git diff > my_changes.patchCreate patch file
git apply my_changes.patchApplying patch file
+

List file activity since the last commit:

+ + + + + + + + + + + + + + + + + +
CommandDescription
git statusList all created/modified/deleted/staged files
git status . -- ':!dir1' ':!dir2'skip status result for the given directories
+

Managing files:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CommandDescription
git ls-files --others --exclude-standardList all untracked files.
git ls-files --modified --exclude-standardList all modified files.
git update-index --assume-unchanged <ignore file>Ignore file modification.
git update-index --no-assume-unchanged <ignored file>Revert above action.
git show <branch>:<file>Peek into file from different branch.
+

Git worktree:

+

It is useful when working on multiple branches simultaneously without maintaining multiple clones for each branch.

+ + + + + + + + + + + + + + + + + + + + + + + + + +
CommandDescription
git worktree add ../myProject-master masterMaster branch code can be accessed at ../myProject-master
git worktree listlist all worktrees from project directory
git worktree remove ../myProject-masterDelete worktree (folder will still be there)
git worktree repair ../new/worktree/pathRepair worktree after moving worktree or bare repository locally to a different location
+

Git Submodule:

+

Commands to manage submodules.

+ + + + + + + + + + + + + + + + + +
CommandDescription
git submodule add <repo_address> <submodule_path>Add new submodule at submodule_path
git submodule update --initDownload all the submodules after cloning the main repo and also reset the submodule commit to the parent commit
+

Git Restore:

+

Commands to copy the directory.

+ + + + + + + + + + + + + +
CommandDescription
git restore --source <branch> dirnameCopy the directory from any branch without git history
+

Git repo maintenance

+

Commands keep the local repo clean and faster.

+ + + + + + + + + + + + + +
CommandDescription
git pruneClean up orphaned objects/commits
+

Time savers

+
Update the last commit which is already pushed to remote.
+

git add . && git commit --amend --no-edit && git push --force

+

Merge multiple local commits into a single commit before pushing to remote

+

git rebase -i HEAD~10
+replace pick with fixup for the commits you want to merge.

+
Note: The oldest commit is at the top.
+

Tips:

+

Disable terminal screen (oh-my-zsh)

+

Add the below line to ~/.zshrc for permanent change.

+
unset LESS
+

Reference:

+ +
+ + \ No newline at end of file