A reference for useful git commands with explanation of its usage.
- To creates a folder
mkdir <DIR NAME>
- To open the folder in vs code.
code .
- To initialize an empty git repository.
git init
Note:
- Creates .git file which implies this is a repository.
- Deleting .git removes the repository status of the folder.
- To check for tracked and untracked files.
git status
- To create the .gitignore file,
touch .gitignore
Then open the .gitignore file and add paths of files and folders that needs to be neglected when syncing to remote repository(for instance the node modules directory).
Note: Before adding files to track using the following commnad. Add path of file or folder that need not be tracked to .gitignore file. - To track all files.( i.e, add these files to tracked label so that it can be commited later)
git add .
- To commit changes locally.
git commit -m "<COMMIT MESSAGE>"
- At times with latest git the branch might be master you can change it to main using,
git branch -M main
Assuming you have now created some files in your directory and you want to save them in your GitHub.
-
To set up remote where the changes need to be saved or synced later
git remote add origin <REMOTE GITHUB REPO URL>
-
To check if remote origin is configured,
git remote -v
-
To push changes,
git push origin <BRANCH NAME>
The following might even come handy for setting the upstream branch
git push -u origin <BRANCH NAME>
or
git push --set-upstream origin <branch name>
Note: Once upstream is set next time onwards you can push just using git push.
-
To pull changes, (it does fetch + merge)
git pull origin <BRANCH NAME>
Note: if git pull gets aborted due to files in the local might be lost during merge and if you want to override and still merge.
git fetch <URL> <branchname>
git reset --hard fetch_head
- To make a copy locally of some repository and then work
git clone <REMOTE REPO URL>
- To work on a feature without disturbin the main or base branch use the following command to copy and create a new branch
git checkout -b <FEATURE BRANCH NAME>
- To check which branch we are locally working on,
git branch
Ref: https://www.nobledesktop.com/learn/git/stage-commit-files
Remove Sensistive Data: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository