GIT Commands
Installing git
Check if GIT is available
git --version
For ubuntu install git using command
sudo apt install git
Authentication Token
Git doesnot allow using password on command line, insted we need a tocken.
Generate one by going to Settings > Developer Settings > Fine Grained Token
Choose Administrator & Compose with read & write permission when generating token.
Store Token
To store the token for repated use there are two options
- Temporary Caching: To temporarily cache your token (e.g., for a set duration like 15 minutes by default), you would use the cache helper:
git config --global credential.helper cache
You can specify a custom timeout in seconds:
git config --global credential.helper 'cache --timeout=86400' # Caches for 24 hours
- Permanent Storage: For permanent storage, the store helper can be used. This writes your credentials, including the token, to a plaintext file named .git-credentials in your home directory ($HOME/.git-credentials).
git config --global credential.helper store
Token will be stored once you do a sucesfull transaction with GIT (e.g. clone, push etc.)
Clone Remote Repo
To clone a remote repo, move to the desired folder to clone into, then
git clone https://github.com/prashant3285/<repo_name>.git
Check Changes
After working on the files to view the changes inside the files
git diff
or to just view the files which were changed
git diff --name-only
If above commands are not showing the changes then check the status of your repo using
git status
Stage Changes
To stage the changes for commiting use (all change will be stages)
git add .
for specific file changes you can use
git add filename.txt
Check Stages Changes
Once changes are stage you can check the staged changes as
git diff --cached
or to just view the staged files which were changed
git diff -cached --name-only
Commiting Change to Remote
To commit changes to the remote repo
git commit -m "Your commit message here"
Pushing the Commit
To push the changes / commits to remote repositiory
git push origin main
where origin is your direcotry and man is the branch in which you want to push
If you just committed and want to see what files were part of that commit
git show --name-only HEAD
Undo Commit
If you already committed but want to roll back while keeping your changes in the working directory
git reset --soft HEAD~1
This removes the last commit but keeps all changes staged.
If you want them unstaged (just in your working directory)
git reset --mixed HEAD~1
Undo Changes to Local Repo
This removes modifications that haven’t been staged
git restore .
If you’ve already staged files with git add but haven’t committed yet
git reset
To completely wipe all local changes and reset to the last commit
git reset --hard HEAD
If you also want to delete files that aren’t tracked by Git
git clean -fd
- -f = force deletion
- -d = include directories
Publish New Local Repo
Navigate to your project folder and then
git init
Then add files and commit - see commands above
For quick creation
git init && git add . && git commit -m "Initial commit"
If you want to push this local repo to GitHub remote repos which is already created
git remote add origin https://github.com/username/repo-name.git
git branch -M main
git push -u origin main