One of the common scenario of using Git is to make fixes to a repo A not belonging to me but I have already customized the source in the repo for my personal usage before hand. In this case, I can make a new branch called patch-1, for example, in my personal repo just for the fix:
git remote add A <url of A>
git fetch A
git checkout -b patch-1 A/patch-1
Once I commit all necessary fixes, I can make a pull request through the web interface of the Git repository. After the PR is merged, I can then delete the patch-1 branch remotely and locally by
git push origin --delete patch-1
git branch -d patch-1If we just want to make a quick fix and then push it back right away, there is another way by calling the remote url directly without adding it:
git fetch https://github.com/theirusername/reponame.git theirbranch:ournameforbranch
where ournameforbranch should be the target branch haven’t been created. Then git checkout FETCH_HEAD to make changes in the new branch. Once things are done,
git push https://github.com/theirusername/reponame.git HEAD:theirbranch
If working in detached state worries you, by all means create a branch using :ournameforbranch and replace FETCH_HEAD and HEAD above with ournameforbranch.
Remove a file stored in Git history: If the change is not published yet, use
git rm --cached FILENAME
git commit --amend -CHEAD
or remove the file and then redo the commits in the past 7 commits in the dev branch:
git filter-branch --prune-empty -d /dev/shm/scratch --index-filter "git rm --cached -f --ignore-unmatch FILEPATH" --tag-name-filter cat -- HEAD~7..HEAD
git update-ref -d refs/original/refs/heads/dev
git reflog expire --expire=now --all
git gc --prune=now
In the commands above, -d names a temporary directory that does not yet exist to use for building the filtered history. If you are running on a modern Linux distribution, specifying a tree in /dev/shm will result in faster execution. Without this -d option, it might end up with a large backup blob in the directory after all the operations.
If the error /refs/original/ backup has exist pops up, use
rm -r .git/refs/original/
to delete the backup first.
To compress loose objects, use
git gc --aggressivegit diff --color-words [<path> ...] can highlight the changes of a file or tree since the last commit in color to the word-level. As git diff is not merely comparing indices, git diff --no-index <path1> <path2> can compare two files without git history and highlight the changes in color. The last command can also go with the --color-words switch to show the word differences.
To reset a local repository to be synchronized with the remote, use
git fetch origin
git reset --hard origin/masterRemove a submodule from a Git repository:
.gitmodules file..gitmodules changes git add .gitmodules.git/config.git rm --cached path_to_submodule (no trailing slash).rm -rf .git/modules/path_to_submodulegit commit -m "Removed submodule <name>"rm -rf path_to_submoduleRef: this one.