{"id":1484,"date":"2021-03-18T14:49:46","date_gmt":"2021-03-18T14:49:46","guid":{"rendered":"https:\/\/dft.wiki\/?p=1484"},"modified":"2026-04-22T07:18:51","modified_gmt":"2026-04-22T11:18:51","slug":"working-with-github-repository","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=1484","title":{"rendered":"Working with Git Repositories in GitHub"},"content":{"rendered":"<p>First things first. Create an account and log in to GitHub [<a href=\"https:\/\/github.com\">Link<\/a>].<\/p>\n<p>Enter <strong>Settings &gt; SSH and GPG Keys &gt; Add SSH Key<\/strong>.<\/p>\n<p>Give a meaningful name and paste your <span style=\"color: #ff0000;\"><strong>SSH Public Key<\/strong><\/span>.<\/p>\n<p>Create a <strong>New Repository<\/strong>. It can be public or private.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1488\" src=\"https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/03\/Screenshot-from-2021-03-18-10-43-52.png\" alt=\"\" width=\"225\" height=\"199\" \/> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1489\" src=\"https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/03\/Screenshot-from-2021-03-18-10-45-09.png\" alt=\"\" width=\"483\" height=\"239\" srcset=\"https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/03\/Screenshot-from-2021-03-18-10-45-09.png 483w, https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/03\/Screenshot-from-2021-03-18-10-45-09-300x148.png 300w\" sizes=\"auto, (max-width: 483px) 100vw, 483px\" \/><\/p>\n<p>It is always good to <strong>Add a README.md<\/strong> file.<\/p>\n<p>Add description and instructions about this repo in the README.md file and Save.<\/p>\n<p>Then click on DOWNLOAD CODE, select SSH, and copy the URL:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1490\" src=\"https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/03\/Screenshot-from-2021-03-18-10-46-43.png\" alt=\"\" width=\"353\" height=\"238\" srcset=\"https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/03\/Screenshot-from-2021-03-18-10-46-43.png 353w, https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/03\/Screenshot-from-2021-03-18-10-46-43-300x202.png 300w\" sizes=\"auto, (max-width: 353px) 100vw, 353px\" \/><\/p>\n<p>On your Linux terminal, where you have the Private Key that matches the uploaded Public Key, issue the command to download the repository.<\/p>\n<pre>git clone <strong>git@github.com:user\/private.git\r\n<\/strong><\/pre>\n<p>Pulling changes from remote to local:<\/p>\n<pre>git pull\r\ngit pull --verbose<\/pre>\n<p>It is important to give Git your identification in order to be able to make changes to your repositories:<\/p>\n<pre>git config --global user.email \"<strong>Your E-Mail<\/strong>\"\r\ngit config --global user.name \"<strong>Your Name<\/strong>\"\r\ncat ~\/.gitconfig<\/pre>\n<p>Or for setting user configuration to a repository only:<\/p>\n<pre>git config user.email \"<strong>Your E-Mail<\/strong>\"\r\ngit config user.name \"<strong>Your Name<\/strong>\"<\/pre>\n<p>Modify the text in the README.md file you pulled from the repository and Save.<\/p>\n<p>Check the status of the online repository against the local pulled repository:<\/p>\n<pre>git status<\/pre>\n<p>Output:<\/p>\n<pre>...\r\n<strong>modified:<\/strong> README.md\r\n...<\/pre>\n<p>Check what is different by issuing:<\/p>\n<pre>git diff\r\ngit diff --staged\r\ngit diff --staged README.md\r\ngit diff --cached README.md \r\ngit diff file1.txt file2.txt\r\ngit diff COMMIT1-ID COMMIT2-ID\r\ngit --no-pager diff COMMIT1-ID COMMIT2-ID<\/pre>\n<p>Commit and push the modification to the online repository:<\/p>\n<pre>git <strong>add<\/strong> README.md\r\ngit <strong>commit -m<\/strong> \"message about the update\"\r\ngit commit <strong>--amend<\/strong> -m \"Updating message for the previous commit\"\r\ngit add anotherFile.txt\r\ngit commit --amend <strong>--no-edit<\/strong>\r\ngit <strong>push<\/strong><\/pre>\n<p>Remove one file or all files from the stage:<\/p>\n<pre>git reset README.md\r\ngit reset<\/pre>\n<p>Check if the updates were done on GitHub.<\/p>\n<p>To create a repository locally, first create a folder and change into it:<\/p>\n<pre>git <strong>init<\/strong>\r\ngit <strong>remote add origin<\/strong> git@github.com:user\/private2.git\r\ngit <strong>remote -v<\/strong>\r\ngit <strong>push -u origin master\r\n<\/strong>git push -u origin<strong> branchName\r\n<\/strong><\/pre>\n<p>Check what branch you are in:<\/p>\n<pre>git branch<\/pre>\n<p>List all branches, including remote ones:<\/p>\n<pre>git branch -a<\/pre>\n<p>Create a branch.<\/p>\n<pre>git branch firstBranch<\/pre>\n<p>Create a branch and switch to it:<\/p>\n<pre>git checkout -b firstBranch<\/pre>\n<p>To make the first push to the new branch issue:<\/p>\n<pre>git push --set-upstream origin firstBranch<\/pre>\n<p>Note: <strong>&#8211;set-upstream<\/strong> is the same as<strong> -u<\/strong> used previously.<\/p>\n<p>Eventually, to delete the beach locally (safe way):<\/p>\n<pre>git branch -d firstBranch<\/pre>\n<p>Force deleting a local branch and losing all changes of the branch:<\/p>\n<pre>git branch -D frstBranch<\/pre>\n<p>Pushing the branch deletion to remote:<\/p>\n<pre>git push origin --delete firstBranch<\/pre>\n<p>Show the differences between the current branch and another.<\/p>\n<pre>git diff firstBranch<\/pre>\n<p>Then merge the branch&#8217;s final version to the master:<\/p>\n<pre>git merge firstBranch<\/pre>\n<p>Switch to another branch.<\/p>\n<pre>git checkout master<\/pre>\n<p>Renaming or moving files:<\/p>\n<pre>git mv fileName.txt fileNewName.txt\r\ngit mv dir1\/fileName dir2\/<\/pre>\n<p>To list all of the commits:<\/p>\n<pre>git log\r\ngit log -p\r\ngit log --all --graph --oneline --decorate<\/pre>\n<p>View one specific commit:<\/p>\n<pre>git show b82ed4bb1a7dbe3ce291af17e73721dbe0c40011\r\ngit show b82ed4<\/pre>\n<p>Discard not committed changes to a file:<\/p>\n<pre>git checkout -- fileName<\/pre>\n<p>Revert last changes (creates a new commit to invert changes):<\/p>\n<pre>git revert HEAD<\/pre>\n<p>Undo last changes:<\/p>\n<pre>git reset HEAD<\/pre>\n<p>To revert the changes of a specific commit:<\/p>\n<pre>git reset 79645cdce5ef42595fee98306b9644c93f098e55<\/pre>\n<p>Or<\/p>\n<pre>git reset --hard 79645cdce5ef42595fee98306b9644c93f098e55<\/pre>\n<p>Put all your changes aside to work on another issue in the original code:<\/p>\n<pre>git stash<\/pre>\n<p>List all code changes you have stashed:<\/p>\n<pre>git stash list<\/pre>\n<p>Show what is in the stash:<\/p>\n<pre>git stash show<\/pre>\n<p>To get back the changes put aside:<\/p>\n<pre>git stash apply<\/pre>\n<p>Search for a string through all history of commits:<\/p>\n<pre>git log -S 'text' --source --all<\/pre>\n<p>Searching with a regular expression:<\/p>\n<pre>git log -G \"regex expression\" --source --all<\/pre>\n<p>Squash all commits in one (use with caution):<\/p>\n<pre>git rebase --root -i<\/pre>\n<p>Removes a file from Git\u2019s index, and the flag <code>--cached<\/code> stops tracking the file without deleting it from your local filesystem.<\/p>\n<pre>git rm --cached .aws\/credentials<\/pre>\n<p>Define the timestamp of the commit.<\/p>\n<pre>GIT_AUTHOR_DATE=\"2024-01-15T10:30:01\" GIT_COMMITTER_DATE=\"2024-01-15T10:30:01\" git commit -m \"Initial commit.\"<\/pre>\n<hr \/>\n<p><strong>BONUS<\/strong><\/p>\n<p>Use Sandfly&#8217;s SSH Hunter to audit SSH keys. Unfortunately, it is not open source, but this agent-less tool might give precious information to visualize what the keys present in the system are and how they are being used (or misused), orphan, or unauthorized keys [<a href=\"https:\/\/sandflysecurity.com\/platform\/ssh-credential-security\/\">Link<\/a>].<\/p>\n<p>Create your own <strong>Private Git Server<\/strong> [<a href=\"https:\/\/dft.wiki\/?p=1728\">Link<\/a>].<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First things first. Create an account and log in to GitHub [Link]. Enter Settings &gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[],"class_list":["post-1484","post","type-post","status-publish","format-standard","hentry","category-linux","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/1484","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1484"}],"version-history":[{"count":29,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/1484\/revisions"}],"predecessor-version":[{"id":5509,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/1484\/revisions\/5509"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}