{"id":20455273,"url":"https://github.com/alexmhack/using_git","last_synced_at":"2025-07-03T22:05:59.918Z","repository":{"id":114089778,"uuid":"145019456","full_name":"Alexmhack/using_git","owner":"Alexmhack","description":"learning the basics and advanced git usage","archived":false,"fork":false,"pushed_at":"2018-08-17T04:22:59.000Z","size":22,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-03T22:05:57.615Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Alexmhack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-08-16T17:27:18.000Z","updated_at":"2023-08-27T14:25:38.000Z","dependencies_parsed_at":"2023-06-12T13:45:25.425Z","dependency_job_id":null,"html_url":"https://github.com/Alexmhack/using_git","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Alexmhack/using_git","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fusing_git","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fusing_git/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fusing_git/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fusing_git/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Alexmhack","download_url":"https://codeload.github.com/Alexmhack/using_git/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fusing_git/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263410762,"owners_count":23462297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T11:18:24.800Z","updated_at":"2025-07-03T22:05:59.882Z","avatar_url":"https://github.com/Alexmhack.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Learning git and github\n\nlearning the basics and advanced git usage \n\nmake a directory and run command to make the directory a git repository\n\n```\n\t\u003e git init\n```\n\ncreate a file sample.py and add a simple print statement into the file\n\nrun command\n\n```\n\t\u003e git status\n```\n\ngit status gives us the status of the files that are changed recently inside the git repository\n\nthe git gives us the command to add the files into the staging area and commit the changes\n\n```\n\t\u003e git add \u003cfile\u003e\n\t\u003e git add sample.py\n```\n\nnow the file sample.py is in the staging area, now we can use the command for commiting the changes\n\n```\n\t\u003e git commit\n\tthe file that is in the staging area will be the one that will be commited\n\tif we have set the sublime editor for commiting then editor will open for writing the message\n```\n\nafter typing the message, save the file and close the editor, you will see the number of lines \nchanged and the insertion of lines in the file\n\n```\nto remove a file from staging area run the command \n\t\u003e git rm --cached sample.py\n```\n\nIf we want to keep a file away from the git repo and the staging area then we can add that file to\n.gitignore\n\n** When we import a local file into another file and use it then python3 creates a __pycache__ file \nin which all the pyc files will be stores which are bytecode compiled version of the file\n\nTo make a .gitignore file in the repo and add the files that are to be prevented from staging area\n\nmake a new file from ST3 and save it as .gitignore and type in the full names of the files along\nwith the extension\n\nthe git will show the .gitignore text file as the untraked file so\n\n```\n\t\u003e git add .gitignore\n\t\u003e git commit -m \"create .gitignore and add __pycache__ and README.rst\"\n```\n\n-m and a message in quotes after that will add the message in the commit\n\nafter typing in the names of the files that are to be ignored from git, those files and folder will\nnot be in the git status anymore\n\nNOTE: there are limitations for the files to be added in the git repo, only those files that are\n\t  source files (files that we write in the editor) are to be added and not the generated files\n\t  or the files that are made by the computer\n\n** There are times when you have to work on your code seperately and while working in teams you \nhave to work on your code and then commit the changes afterwards, for that you have to move away\nfrom master branch and create a seperte stream for you own project\n\n```\n\t\u003e git checkout -b rishabh_branch\n```\n\nthis command checks out in the new branch just created by the command -b \u003cbranch-name\u003e\n\nnow git status, you can look at the branch you currently are in\n\nonce you have created the the new branch and checkout the branch, the changes you make in the files\nwill show up in status and you can commit those changes and also git log to see the commit you just\nmade but it will have a different SHA in the rishabh_branch\n\nnow checkout in the master branch and git log again, can you find your recent commit made in the \nrishabh_branch, NO!!!\n\nthis is magic of branches\n\nNow to review your branches you can use the command \n\n```\n\t\u003e git show-branch rishabh_branch master\n```\n\nthis will show the commit in the master and rishabh_branch with their names and if you want to\nlook at their SHA then use \n\n```\n\t\u003e git show-branch --sha1-name my_new_feature master\n```\n\nnow if you want to merge the two branches then first\n\n```\n\t\u003e git checkout master\n\t\u003e git merge rishabh_master\n```\n\nnow look at the git log and you can see the changes from the rishabh_branch in the master branch\n\n# Advanced Features\ndouble dot notation\n\n```\ngit log \u003ccommit_id_1\u003e..\u003ccommit_id_2\u003e\n```\n\ncommand will print the commits after commit_id_1 till commit_id_2 including commit_id_2 commit\n\nWhen logging commits from an id at top from one parent and at top from second parent will only\nprint the commits from the common commit to the commit_id_2\n\nvisit [advanced git tutorial](https://realpython.com/advanced-git-for-pythonistas/) for whole \ntutorial.\n\nTriple dot notation\n\n```\ngit log \u003ccommit_id_1\u003e...\u003ccommit_id_2\u003e\n```\n\ncommand will print the commits that are in between either commits but not in both commit ids\n\n```\ngit rebase \u003cbranch-name\u003e\n```\n\nthe above command is for those cases when we have,\n\nHEAD , MASTER on the last commit\nWe have to make some changes in some 5 level before revision\nSo, we checkout that commit\nCheckout a new branch\nCommit our changes there\nCheckout the master branch\nFind our changes haven't affected the master branch\nSo, we rebase the whole commits after our branch using\n\n```\ngit rebase \u003cbranch-name\u003e\n```\n\n```\ngit stash save | git stash pop\ngit stash save == git stash\ngit stash save | git stash pop --index\ngit stash save \"save-work\" | git stash pop\ngit stash save --include-untracked \"saving-work\" | git stash pop --index | git stash pop\ngit stash list\ngit stash show | git stash show stash@{1} | git stash show -p stash@{1}\ngit stash pop stash@{1} | git stash apply | git stash apply stash@{1}\n```\n\nabove command will save our non-committed work including untracked files or files that are in staging\narea or index and using stash pop will recover all our work and we can also name our stash, stash \nlist returns all the stashes that we have so far. For getting all the information of the files that \nare in the stash we use the command stash show, mentioning the stash number will show details for \nthat particular stash or we can use -p to see very detailed path info with the changes in each file\n\nWe can pop the stash into the working directory from the stash list by specifying the stash or we can\nkeep our work in stash as well as in the working directory using the stash apply\n\n```\ngit diff | git diff --staged\n```\n\nWe can use diff to get the changes that are currently in all the files in the working directory\nThe default diff shows all the changes that are unstaged, for getting all the changes in files that \nare in the staging use the optional --staged\n\n```\ngit diff SHA SHA\n```\n\nTo compare any two commits in the your repo use the diff with the SHA\n\n```\ngit diff master temp\ngit diff master^ master\ngit diff HEAD~3 HEAD\ngit diff HEAD~3 HEAD --greeting.py\ngit diff HEAD^ HEAD\ngit diff HEAD^ HEAD --name-only\n```\n\nTo get all set of changes between the master and any branch say temp then use git diff\nThe --name-only option will show you the list of filename that were changed between two commits, but \nnot what changed in those files.\n\n```\ngit config --global diff.tool meld\ngit config --global difftool.prompt false\ngit difftool HEAD^ HEAD\ngit difftool stash@{1} | git difftool stash\n```\n\nDifftool launches a seperate window for showing the changes which is much easier to look at than our\ncommand line interface, you can launch the difftool with the same keyword.\n\n```\ngit commit --amend | git commit --amend -m \"commit message\"\n```\n\nIf you have made some mistakes in the commit message then you can edit the message for that \nparticular commit using the --amend option and this amends should be made only when there are no\nother commits based on this particular commit, then too you should only --amend when the commit is \nlocal and not has been pushed to github.\n\n```\ngit pull -r\n```\n\nIf there are changes in the remote that you want to pull and have in your local repo you will git \npull which will merge the changes to the remote branch, git pull -r will rebase your commits on top \nof the changes that were on the remote.\n\n```\ngit rebase -i\n```\n\nThere is a -i flag you can add to the rebase command that will put it into interactive mode, this \nfeature lets you have full list of commits before you push them to the remote\n\n```\ngit log --oneline\n```\n\nFor showing a short listed details with only the SHA (short) and the commit message, you can use\nthe --oneline option along with git log\n\n```\ngit rebase -i SHA\n```\n\nSuppose you have added a new feature and in that process you have made a lot of commits and those\ncommits appear in the log which belongs to single feature, those commits won't be needed by you in \nthe future and neither by your team members so you can put all those commits inside the single one\nusing the rebase -i option, choose the commit from the log above which all the other commits exists\nfor your feature and then use it with the above replacing its SHA in the command.\n\nThen you will be given all the log commits in reverse order from bottom to top but not including the\nSHA that was used with the rebase command. There will be instructions given, generally you will pick\nthe first commit message that appears for the parent one for feature (which happens to be the commit \njust after the commit used with the rebase command), then you can either use the full keyword or just\nthe short 's' for squasing all the commits under the picked commit.\n\nSave the sublime window (if sublime is set your default editor for git) and git will show you the \npreview of how your commits will look after rebasing and you can edit them, save and git will rebase \nall those commits onto the one you picked, git log again and you can see your changes\n\n```\ngit revert HEAD | git revert HEAD -n\n```\n\nWhen you want to remove the changes made from the most recent commit you can revert your HEAD using\nthe above command and those changes will be back but the commit history won't change but a new \ncommit message (default) will be added to the log, there is no option to add a message in the \nrevert but you can skip the process of showing the window using the -n.\n\n```\ngit clean | git clean -x | git clean -xd\n```\n\nCAUTION: Use git clean carefully as the actions from clean command are not reversible\nThis command cleans all the files that are untracked in the working directory\nIf you want to delete the files that are in the .gitignore for ignoring you can use -x for that\nFor deleting any folders that are ignored, you can include 'd' as in -xd\n\n```\necho add-this-line \u003e\u003e file.ext\n```\n\nUsing the echo command the two angle brackets we can add the words add-this-line into the file after\nthe angle brackets, no need to use the quotes unless you want the quotes in the file itself.\n\n```\ngit show-branch master \u003cbranch-name\u003e\non branch master,\ngit merge \u003cbranch-name\u003e\n```\n\nYou can look at the conflicting changes on \u003cbranch-name\u003e and master visually\nWhen merging another branch from the checked out master branch, suppose there is a conflict, git\ntries to automerge but in case of conflicting you can look at the file again which has the conflicts\nand you will find your file divided into two sections, the first section with and, are from HEAD, \nwhich in your case is master. The bottom portion, between and \u003cbranch-name\u003e are from your branch\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexmhack%2Fusing_git","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexmhack%2Fusing_git","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexmhack%2Fusing_git/lists"}