Skip to content

Instantly share code, notes, and snippets.

@AnmolTomer
Last active April 29, 2020 01:41
Show Gist options
  • Save AnmolTomer/173a61c67160b5bb882db35692d73b5e to your computer and use it in GitHub Desktop.
Save AnmolTomer/173a61c67160b5bb882db35692d73b5e to your computer and use it in GitHub Desktop.

Basic Git Commands

  1. To track files and folders inside a directory by git use following :

    git init
  2. Connecting your local directory with GitHub repo as we have an existing repo :

    git remote add origin https://git.521000.beste/UserName/Git_Repo_Name.git
    				OR
    git remote add origin [email protected]:UserName/Git_Repo_Name.git
  3. Add everything inside the local directory to staging area and see if the files and folders were added

    git add .
    git status
  4. Creating a commit or a checkpoint you can revert back to in case something goes wrong

    git commit -m "First Commit"
    git push -u origin master
  5. Creating a branch : -b flag stands for branch

    git checkout -b dev
  6. To see different branches available in the repository and push it to GitHub :

    git branch
    git push origin dev
  7. To create another branch, view branches and push

    git checkout -b feature/calendar
    git branch
    git push origin feature/calendar
  8. How delete branch on repository as well as locally :

    git push :feature/calendar
    git checkout dev
    git branch -d feature/calendar
  9. Creating and merging branches :

    git checkout -b feature/calendar
    git push origin feature/calendar
  10. Do some changes to file and then do following :

    git status
    git add .
    git commit -m "Dev Ready"
    git push --set-upstream origin feature/calendar
  11. If using VSCode at the left bottom you might see branch name and change it as per your needs. If you are on master and create a new branch dev then at that point of time whatever you have in master will be copied over to dev. Again if you create feature/calendar while you were at dev then everything inside dev would be copied to feature/calendar.

  12. After pushing the code we go to website, and we might see prompt showing us that something was uploaded to the branch and an option to do a pull request PR after comparing. If you don't see that go to branches and select the one you want to merge in the drop down select the base : as the destination of the PR where to push it to, if you see able to merge you are good to go.

  13. If 2 piece of code have changes on the same line then that is a case of merge conflict and we have to rectify that. As git asks us which one of those two we want.

  14. Revert a Commit GitHub

  • Revert Git Repo to a previous commit

  • Re-writing History

  • Rollback

  • Explanation

  • Scenario: You have pushed changed to remote and you want to go back to a previous commit both on remote and local.

    1. Get the commit ID of the wrong commit using git log, commit ID of the top-most commit is the id you need to put in the command
      git push origin +wrong_commit_ID^:master
      Explanation: Delete the last commit Deleting the last commit is the easiest case. Let's say we have a remote origin with branch master that currently points to commit dd61ab32. We want to remove the top commit. Translated to git terminology, we want to force the master branch of the origin remote repository to the parent of dd61ab32:
      git push origin +dd61ab32^:master.

Where git interprets x^ as the parent of x and + as a forced non-fastforward push. If you have the master branch checked out locally, you can also do it in two simpler steps: First reset the branch to the parent of the current commit, then force-push it to the remote.


git reset HEAD^ --hard
git push origin -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment