For an easy revert if it's just a mistake (perhaps you forked a repo, then ended up pushing to the original instead of to a new one) here's another possibility:
git reset --hard 71c27777543ccfcb0376dcdd8f6777df055ef479
Obviously swap in that number for the number of the commit you want to return to.
Everything since then will be deleted once you push again. To do that, the next step would be:
git push --force
1linkgit init creates new repository in current directory
2linkgit add . add all latest changes to the next commit
Cd ~/projects/my project git inti git add .
1linkgit clone is used to clone a repositroy from a remote server
git clone ~/existing/repo ~new/repo git clone you@host:dir/project.git (default protocol is ssh)
mkdir repo.git && cd repo.git git init --bare[--shared=group]
git fetch (this does not merge them)
git pull (does a fetch followed by a merge)
git am -3 patch.mbox (In case of conflict, resolve the conflict and) git am --resolve
git commit -a
git commit -m "descriptive message"
git format-patch origin
git push [origin][branch]
git tag
git stash
git stash push -m
git stash pop
git checkout
git checkout
git branch
git checkout
git branch -d
git debase origin/master
1linkWhich would apply all the changes in master, below your branch, to make your commits grouped together and more organized.
git rebase -i HEAD~2
1linkWhich runs your debase in interactive mode for the most recent 2 commits (which is really helpful for squashing and renaming your commits), making it more readable.
git checkout -F | git reset --hard (you cannot undo a hard reset)
git revert HEAD (Creates a new commit)
git revert $id (Creates a new commit)
git commit -a --amend (after editing the broken files)
git checkout
git status
git diff
git diff
git log
git whatchanged
git blame
git show
git diff
git branch (star "*" marks the current branch)
git grep
1linkDefine author name to be used for all commits in current repo. Developers commonly use --global flag to set config options for current user.
git config user.name
1linkDefine the author name to be used for all commits by the current user
git config --global user.name
1linkDefine the author email to be used for all commits by the current user.
git config --global user.email
1linkCreate shortcut for a Git command.
git config --global alias.
1linkSet text editor used by commands for all users on the machine. arg should be the command that launches the desired editor (e.g. vi).
git config --system core.editor
1linkOpen the global configuration file in a text editor for manual editing.
git config --global --edit
1linkFirst you have to install GPG, if you don’t already have it. You can verify your installation (i.e. with Windows Power Shell) like this:
gpg --version gpg (GnuPG) 2.2.17 libgcrypt 1.8.4
1linkIf your system doesn't know them yet, you have to import your public and private keys (I assume you have them stored in files called public.key and private.key). If you don’t have a key pair, you can generate a new one. In that case, you can skip the import and directly jump to Set up Git. It's also possible to use your Keybase GPG key, if you have one (Stephen Rees-Carter wrote a nice article about it).
gpg --import public.key gpg --import private.key
1linkNote: when importing the private key, a GUI window appears that asks for the corresponding passphrase you set when creating your key pair.
1linkNow you can tell Git your signing key ID. It’s a 16-digit alphanumeric string that can be found with gpg --list-signatures (look for lines starting with “sig”).
git config --global user.signingkey 26A64778F76A7911
1linkIf you want, you can tell Git to sign commits per default (since Git 2.0), so you don’t always have to add the -s flag in the command line:
git config --global commit.gpgsign true
1linkNote, that I use the --global flag here to apply these settings to all my local repositories. Of course you can apply these settings only to the current repository without it.
1linkNow you have to give GitHub (or whatever Git server you’re using) your public key. You can print it with mpg --armor --export or get-content -path public.key (or open it with your favorite text editor) and copy it to your clipboard. Now go to GitHub, click on the top right menu, go to Settings > SSH and GPG keys > New GPG key and paste your key — it should look like this:
-----BEGIN PGP PUBLIC KEY BLOCK----- ...a lot of characters... -----END PCP PUBLIC KEY BLOCK-----
1linkWell, would have been too easy if it worked at first try… unfortunately I had to deal with the following weird error message, that appeared always when I tried to commit using the -s flag:
git commit -am "a message" -s gpg: skipped "26A64778F76A7911": secret key not available gpg: signing failed: secret key not available error: gpg failed to sign the data fatal: failed to write commit object
1linkAfter some research, I found, that I had to tell Git the path to the GPG executable (for whatever reason) using the git config pgp.program setting:
git config --global gpg.program "C:\Program Files (x86)\gnupg\bin\gpg.exe"
1linkNote that the path may be a different one on your system.
precursor: it might just be easier to change the password!!! if you can.. just do that
1linkfirst thing you do is change the visibility of the repo. So, if it's a public repo, make it private. This way, you're sure no one else sees the file while you're working on deleting it.
cd my-repo
1linkthen run the following command. You have to include the path to the file and not just the file name. replacing config/secretFile.json with the path to the file you want to be removed. In my case, secretFile.json is inside of a folder named config.
git filter-branch --force --index-filter
"git rm --cached --ignore-unmatch config/secretFile.json"
--prune-empty --tag-name-filter cat -- --all
1linkNote: The command above deletes the file from your local repository too, so ensure you have copied the content to a notepad or whatever you use before running the command.
2link
3linkThen create and add your file to .gitignore so you don't accidentally push it to GitHub again. You can do that with
echo "name-of-your-file" >> .gitignore git add .gitignore git commit -m 'add file to .gitignore'
1linkOnce you are satisfied with your changes, you can then push them to GitHub
git push origin --force --all And that's it! Your repo history is clean without any trace of your sensitive file.
1linkmaster is the default development branch
2linkorigin is the default upstream repository
3linkHEAD is the current branch
Home Backstory Why Git? Setting Up GitHub GitHub CLI Your GitHub Profile GitHub Pages Creating A New Repo Cloning A Repository Your First Commit Ignoring Files In Git Stashing Your Changes Branching With Git Merging Git Branches Rebasing Vs Merging Git Workflows Fork And Pull Flow Your First Pull Request Automated Security Alerts Seeing The Differences Removing/Deleting Files Reverting Your Commits Configuring your Git Commit Templates Creating Shortcuts Dotfile Management Sponsoring Open Source Handy Commands Thank You