Star

Created With

linkHandy Commands

linkEasy Revert A Mistake

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

linkCREATE

linkFrom existing data,

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 .

linkFrom existing repo

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)

linkRemote repository for existing local data

mkdir repo.git && cd repo.git git init --bare[--shared=group]



linkUPDATE

linkFetch latest changes from origin

git fetch (this does not merge them)

linkPull latest changes from origin

git pull (does a fetch followed by a merge)

linkApply a patch that someone sent you

git am -3 patch.mbox (In case of conflict, resolve the conflict and) git am --resolve



linkPUBLISH

linkCommit all local changes

git commit -a

linkCommit previously staged changes

git commit -m "descriptive message"

linkPrepare a patch for other developers

git format-patch origin

linkPush changes to origin

git push [origin][branch]

linkMake a version or a milestone

git tag



linkSTASH

linkStash uncommitted changes before moving to another branch

git stash

linkGive the stash a name for easy reference

git stash push -m

linkReturn local copy of branch back to how it was when you stashed it earlier

git stash pop



linkBRANCH

linkSwitch to the BRANCH branch

git checkout

linkMerge branch B1 into branch B2

git checkout git merge

linkCreate branch based on HEAD

git branch

linkCreate branch based on another

git checkout

linkDelete a branch

git branch -d



linkREBASE

linkRebase with git

git debase origin/master

1linkWhich would apply all the changes in master, below your branch, to make your commits grouped together and more organized.

linkInteractive rebase

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.



linkREVERT

linkReturn to the last committed state

git checkout -F | git reset --hard (you cannot undo a hard reset)

linkRevert the last commit

git revert HEAD (Creates a new commit)

linkRevert specific commit

git revert $id (Creates a new commit)

linkFix the last commit

git commit -a --amend (after editing the broken files)

linkCheckout the ID version of a file

git checkout



linkCHANGES

linkFiles changed in working directory

git status

linkChanges to tracked files

git diff

linkChanges between Ida and Ida

git diff

linkHistory of changes

git log

linkHistory of changes with files changed

git whatchanged

linkWho changed what and when in a file

git blame

linkA commit identifies by ID

git show

linkA specific file from a specific ID

git diff :

linkAll local branches

git branch (star "*" marks the current branch)

linkSearch for patterns

git grep[path]



linkCONFIG

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



linkPGP SIGNATURE FOR COMMITS

linkSet up GPG

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.

linkSet up Git

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.

linkSet up GitHub

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-----

linkError: secret key not available

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.



linkSECURITY

linkRemove sensitive files from GitHub from your commit history [e.g passwords or secrets]

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.



linkFYI

1linkmaster is the default development branch

2linkorigin is the default upstream repository

3linkHEAD is the current branch

Handy CommandsEasy Revert A MistakeCREATEFrom existing data,From existing repoRemote repository for existing local dataUPDATEFetch latest changes from originPull latest changes from originApply a patch that someone sent youPUBLISHCommit all local changesCommit previously staged changesPrepare a patch for other developersPush changes to originMake a version or a milestoneSTASHStash uncommitted changes before moving to another branchGive the stash a name for easy referenceReturn local copy of branch back to how it was when you stashed it earlierBRANCHSwitch to the BRANCH branchMerge branch B1 into branch B2Create branch based on HEADCreate branch based on anotherDelete a branchREBASERebase with gitInteractive rebaseREVERTReturn to the last committed stateRevert the last commitRevert specific commitFix the last commitCheckout the ID version of a fileCHANGESFiles changed in working directoryChanges to tracked filesChanges between Ida and IdaHistory of changesHistory of changes with files changedWho changed what and when in a fileA commit identifies by IDA specific file from a specific IDAll local branchesSearch for patternsCONFIGPGP SIGNATURE FOR COMMITSSet up GPGSet up GitSet up GitHubError: secret key not availableSECURITYRemove sensitive files from GitHub from your commit history [e.g passwords or secrets]FYI
Content

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



Developer Student Clubs

logo2.png