Cheat sheet on git basics
Note - Repository / directory means folder.
1. git status - This command is used to check the present working state of the git. It shows whether git is tracking any folder or not. It also shows whether any changes need to be added and/or committed or not.
2. cd - This command means change directory or change folder. This command is used to move further into a directory/folder so that we can enter into other directories/folders which are present into that directory/folder.
For eg - cd Desktop/git-explore/01folder
01folder(containing files) is present in the folder git-explore and git-explore folder is present in the folder Desktop.
3. git init - This command is used to initialize an empty git repository/folder in git (git-bash/terminal) which means from now onwards we are asking git to track a local folder that we want git (git-bash/terminal) to track so that we can keep a record of changes in that local folder by means of git.
4. git add - This command is used to add (or move) our file/changes from local folder to staging area so that we can decide whether to finalize those changes or revert those changes. For example- git add 01file.txt, git add 01file.txt 02file.txt, git add .
Dot means we are adding all the files to staging area.
5. git commit -m "commit message" - This command is used to finalize the changes that we have made in our local files. -m means short message for what changes we have made in the project. For example, git commit -m "google login functionality added"
6. git remote - This command is used to link our local system to the remote system. This command also gives the name of the alias with which our current directory in git bash/terminal is associated. Alias is just another short name of the url of the remote repository with which our current directory in git bash/terminal is associated. Origin is the standard name given by git to alias.
7. git remote add origin url of remote repository - Origin is the name of that remote repository where we want to push our changes or from where we have cloned the project. We can give another name to the created remote repository instead of origin like earth, for example - git remote add earth url of remote repository.
This command is used after git remote command. This command means we are linking our local system to that particular remote repository (origin) and the url of that remote repository is mentioned after origin.
8. git push -u origin branch name - -u means upstream. This command is used to set an upstream branch of our local branch on the remote which means our local branch will keep a track of its remote branch i.e. if someone else had made changes to the remote branch then we can pull those changes because we have used -u option. Also next time while pushing the changes to the remote upstream branch we have to only type the shorthand command git push instead of git push origin branch name or git push -u origin branch name again and again.
If we use the same command without -u option i.e. git push origin master then our local branch will not keep a track of the remote branch & we cannot pull the changes from remote branch if someone else have made changes to the remote branch and we have to write git push origin branch name every next time to push our changes from local branch to remote branch until we type git push -u origin branch name.
9. git remote -v - This command displays the url of the remote repository that from where we are going to fetch and push the changes. -v means verbose i.e. in detail.
10. git pull - This command is used to pull the changes from remote branch to local branch.
11. git clone url of remote repository folder name - This command is used to clone a remote repository to our local system under the given folder name. This same folder must be present in its parent folder in which git is not initialized. If we use the same command without folder name i.e. git clone url of remote repository , then it will clone the remote repository in our local system with the same name as of remote repository in the non initialized git folder.
Note - Make sure before using this command we are in the non initialized git folder in git bash / terminal using the command cd (change directory).
Subscribe to my newsletter
Read articles from Anurag Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by