Git commands

List of all the widely used git commands :

  1. git init - tracking the required project folder via git.

  2. git status - specifies all untracked and modified files.

  3. git add . - adds all the untracked and modified files into the staging area.

  4. git commit -m "enter your commit message" - commit the changes with a message.

  5. git restore --staged filename - remove the file from the staging area.

  6. git log - list all the commits history.

  7. git reset hashOfTheCommit - resets the commit according to the hash, deleting all the commits above.

  8. git stash - changes in the staging area can also be stored in stash w/o losing or committing those changes.

  9. git stash pop - get all the items back from the stash into the staging area.

  10. git stash clear - delete all the files under the stash area.

  11. git remote add origin URLOfRemoteRepo - Connect the local folder with GitHub online repo.

  12. git remote -v - lists all the URLs attached to the git-tracked local folder.

  13. git push origin master - push local system files to the remote repo labeled as the origin and onto the master branch.

  14. git branch branchName - create a new branch.

  15. git checkout branchName - shifts the HEAD pointer to the mentioned branch.

  16. git merge branchName - Merging the changes into the main branch.

  17. git clone URLOfRemoteRepo - clone existing project into your local system.

  18. git remote add upstream URL - the cloning project's URL is added as upstream.

  19. git push origin branchName -f - when remote repo commits are ahead of the local system commits. [ -f flag forcefully sync's changes]

  20. git fetch --all --prune - fetch all commits from branches [--all indicates origin , upstream , --prune indicates all deleted branches as well. ]

  21. git reset --hard upstream/main - reset the local system main branch to sync with the upstream main branch.

  22. git pull upstream main - get the forked copy in sync with the upstream.

  23. git rebase -i hashOfTheCommit - merge and squash multiple commits into a single commit.

**Things to remember:**

1. To make a commit untracked/modified files have to be staged first.

2. git status highlights files in green color if files are present in the staging area or else in red color.

3. Any files created/modified/deleted need to be put into the staging area & then committed.

4. Every git commit has a unique hash & every hash is dependent on the previous one.

5. Resetting or deleting commits and putting all the changes back into the staging area.

6. Always create a new branch to create a new pull request. \[Thumb rule - unique pull request for unique branch. \]

7. New Feature -> New Branch -> New Pull Request

8. HEAD pointer indicates to which branch the latest commits must go.

How to check if the local folder is tracked using git?
use command ls -a: check hidden files to verify .git file is present. This indicates the folder is being tracked by git.

Hosting a project on GitHub to be publically accessible?

  1. Create a new repo into your GitHub account.

  2. Get the URL of that repo.

  3. Use git remote add URLnameOfRepo to attach the local system version-controlled tracked folder to the online remote repo.

  4. Use git push origin master command to push the local system changes to online remote repo.

Working with existing projects:

  1. Create a copy of the project into your repo by forking the project.

  2. Clone that project into the local system using the URL.

  3. The repo from where the project is forked is the upstream URL.

  4. create a new branch to make the required changes, and make a pull request for your changes to be merged into the project.

How to create a pull request?

  1. Make changes to a new branch in your copy of the code.

  2. Push those changes into your online repo [forked project].

  3. Select compare & merge.

  4. Create a pull request.