Git commands
will start with the basics.
Setting up!
Let's say you are on a fresh system and want to configure Git here is what you need to do, I've been using MacOS and GitHub while writing this but it should be similar for everything else.
# Lets say this is your fresh system and you are setting up to clone repo from github
#1.Make sure you have git installed
brew install git
#2.Set your user
git config --global user.name <"Your Name">
git config --global user.email <"your.email@example.com">
git config --global --list #This will list user --global flag lists global user
#This is going to set user global ie: ~/.gitconfig
#We could also setup user per repo, In case you set both the repo level is going to override global
cd </path/to/repository>
git config user.name <"Your Name">
git config user.email <"your.email@example.com">
#3. Login: Generate SSH Key
#This step will make our life easy with avoiding username password everytime + some tools and corporates do not allow username/password in first place
ssh-keygen -t rsa -b 4096 -C <"your.email@example.com">
#Just select default values and random passphrace when prompted
#This will create a SSH keypair at ~/.ssh ie: id_rsa and id_rsa.pub
#4. Add key in Github / or other Git like Bitbucket
#In our case navigate to Account Settings > Add SSH
#Path : https://github.com/settings/ssh/new
#Copy and paste the public key ie: id_ras.pub key here and give it a name and save it
You are now ready to clone !
#5. Save Time step
#Add the private key to ssh agent, so that you do not have to enter passphrace everytime
ssh-add ~/.ssh/id_rsa
#Issues with SSH read : https://osxdaily.com/2022/12/22/fix-ssh-not-working-macos-rsa-issue
Now let's see commands to get your code in there!
git clone <"SSH link to project"> #This will clone the repo to current directory
#Add and update new files within this repo
git status # This will show status of files staged/unstaged and the branch
git add . # This will add all the unstaged files/directory into staged
#you could have specfied file name insterd ie: git add "hello.py"
git commit -m "< your commit message >"
#This will commit your changes locally in git
#To push the changes to remote repo
git push -u origin main # This will set git tracking current branch with main branch in remote
#after this you could just use `git push`
Branching !! ๐
git branch # This will show all the branches and have *on current branch
#Now lets create a new branch from main branch
#1. Make sure you are already on main branch
git checkout main
git branch -b <"name of the new branch">
# -b flag will switch you to this new branch after creation
#.. Now its the usual operations from here
git reset --hard <"commit_id"> #This will delete all other commits before this commit id which includes merged changes.
ย