# ____ ___ _____
# / ___|_ _|_ _|
# | | _ | | | |
# | |_| || | | |
# \____|___| |_|
#
First and foremost git is a version control system, and it is the most popular version control system in use today. Git can do a lot of things for you, and has an intuitive command structure to allow this. Git would not be as popular as it is today, if it were not for the site Github. Which allows anyone to host and share software source code for free.
Below are a few examples on how to use git that are not often encountered.
A pull request is how another user can contribute code to a repository they are not the creator of. This proceess can be performed completely remotely from the command line, but for our purposes will be performed using both the command line and a browser with Github.
git clone https://github.com/USERNAME/REPOSITORY
git add . && git commit -am 'some message about the commit'
git clone --mirror $REPO
java -jar BFG.jar -rt $SENSITIVE_DATA_FILE.txt --private $REPO_NAME
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push
This is a small script that should perform everything needed to remove git lfs from the repository. Unfortunately, it will also uninstall git-lfs from your system, but it can be easily installed again.
#!/usr/bin/env bash
git add .
git commit -am 'preparing for lfs uninstall' && git push
git lfs pull -all
git lfs uninstall
git lfs ls-files | cut -f 3 -d ' ' | xargs git rm --cached
rm -rf .git/lfs .git/hooks/pre-push .gitattributes
git add . && git commit -am 'removed lfs' && git push
git status
When it comes to removing a submodule from a repository, it is not as easy as simply removing the submodule’s
directory, not is it as easy as removing the modules entry from .gitsubmodule
. There is a method to these
things.
.gitmodules
file.git add .gitmodules
..git/config
.git rm --cached .git/modules/$YOURMODULE
git commit -am 'removed module'
To remove a submodule you need to:
Delete the relevant section from the .gitmodules file.
Stage the .gitmodules changes:
git add .gitmodules
Delete the relevant section from .git/config.
Remove the submodule files from the working tree and index:
git rm --cached path_to_submodule (no trailing slash).
Remove the submodule's .git directory:
rm -rf .git/modules/path_to_submodule
Commit the changes:
git commit -m "Removed submodule <name>"
Delete the now untracked submodule files:
rm -rf path_to_submodule
See also: alternative steps below.
–John Douthat
You committed a change that you were not supposed to and need to undo the commit. In git this is referred to as “reverting”, because you are returning to a previous state.
Before you get over exuberent reverting shit, you need to discover the hash of the latest commit. This can be
done using git log
.
git log --all --decorate --oneline --graph
Here is a hint: It is the one on the top line.
Once you discovered your commit hash, you then can revert your commit.
git revert -m 1 $COMMIT_HASH
The addition of the -m 1
flag tells git to favor changes from the first parent.
[!caution] Do not use
git reset
git reset
can be a destructive command if used incorrectly.
Git revert will create a new commit reverting the repo to it’s previous state.
Undoing a previous revert is almost identitical to doing the revert. This is because git revert
does not
wipe the previous undesired commit from the history of the git log. Rather, it creates a new commit that then
changes things back to how they were.
So to undo a previous git revert, you follow the same steps.
git log --all --decorate --oneline --graph
The commit that reverted the repo to it’s previous state should be on the top line. So you will want to use
that hash and run git revert
again.
git revert -m 1 $COMMIT_HASH
You can also run git cherry-pick $COMMIT_HASH
for a similar result.