Skip to content

N6_Git and Github_version control

Tahsin Afroz Hoque Nishat edited this page Sep 30, 2023 · 7 revisions

Installation

  1. Goto the official website of Git to download git Click here.

Launching commands for git and github in terminal

  1. Installation on windows comes with "Git Bash" terminal.
  2. Can use "powershell" or cmd terminal from windows too.
  3. Can use "hyper.is" which works across all platforms.

Configuring git for the first time

You need to configure your name, your email address and default branch to know who is doing the change

  1. git config --global user.name "type your name w/ quote"
  2. git config --global user.email <type your email address w/o quote>
  3. git config --global init.default branch main

Pulling from or cloning a github repository

  1. To clone: git clone https://github.com/user-name/repository.git
  2. To pull first git init or git init repo-name and then git pull https://github.com/user-name/repository.git

Go to a specific directory in gitbash

  • option 1: Go to the project directory in your PC and open git bash there.
  • option 2: $cd /c/Users/project
  • option 3: $cd "C:\\Users\project"

Important git commands for versioning

  1. goto the specific dir from the cmd (cd or cd.. is to navigate to different folder): mkdir "name of the folder"
  2. cd to be inside that dir. make a sample .txt file.
  3. Make a .git folder to track the changes git init. Before or after this command you can list all your folder dir or dir \ad note: ideally this command is ls or ls -a. A .git file will be created
  4. git status (To check if the files are being tracked or not)
  5. git add . or git add <file.txt> (to track and update the changes)
  6. git commit -m "message" (a specific message to see what the changes are you made to your files" > it tracks every changes you make.
  7. git remote add origin "https://githubfolder" (to sync with github repository. Before this you need to create a github repository)
  8. git push origin master (at this stage you need to use your ssh keys passphrase or other security pass for the first time). Be careful about the current branch you are working and the github branch you want to push.
  9. git log (summarize all changes until)
  10. to revert last git commit git revert <alphanumeric code from git log>

Troubleshooting

Problems with master and main!

  1. To set the default branch as main do the git config command git config --global init.default branch main and run VScode afterwards (did not work for my case and faced a lot of issues!!!!)
  2. The second option was to set the current master branch as main and delete the master branch. Follow the steps
  • rename your local branch: git branch -m master main
  • change the tracked branch: git fetch -p origin and then git branch -u origin/main main
  • change the main local branch: git remote set-head origin -a
  • optionally, remove the master branch, local and remotely: git branch -D master and then git push origin :master

Problem with authentication!

When I was working with cloud shell, I got stuck with authentication for github.It was asking for password and when I was entering the pass it got denied. I was looking at different solutions that are provided online. I have found the following documentation useful for my understanding on different types of authentication methods (https://docs.github.com/en/authentication)

For me, I have solved my problem by creating and using SSH keys for my windows (from local cmd). To create SSH I have followed the below command:

  1. Make sure you are in the .ssh directory from cmd where you want to generate the ssh key (https://www.howtogeek.com/762863/how-to-generate-ssh-keys-in-windows-10-and-windows-11/)

  2. ssh-keygen > Generating public/private rsa key pair. > Enter file in which to save the key (C:\Users\tahsi/.ssh/id_rsa): (you can press enter to use the default path or can set a new one) > Enter passphrase (empty for no passphrase): (This is by far the most important part. While typing you cannot see the character but need to memorize or store the pass phrase) > Finally, you have created an id_rsa type ssh key on your local machine! But I don't know why it cannot be added on github! or, 2. ssh-keygen -T id_ed25519 -C "[email protected]" it will create an id_ed25519 type ssh key locally.

  3. Now you have to add the ssh key to your github account. Goto ssh key>add new key>enter the 20-char key

  4. From your computer, goto settings>apps>optional features>OpneSSH client installed

  5. Goto: services>OpenSSH authentication agent>properties>automatic(delayed) this setting enable the ssh key from the startup. Do not need to enter credentials each time you restart your PC (https://stackoverflow.com/questions/18683092/how-to-run-ssh-add-on-windows)

  6. from cmd: start-ssh-agent > the agent will find the ssh and add it. Need to enter the passphrase here.

Now you can easily push your local directly to your github account

  1. git remote add origin "https URL"
  2. git push origin master
image

Tips and shortcuts

  1. Not sure which command is for what? type the following command for help on git config git config -h. Or for details type git help config
  2. To clear windows type clear and press enter (In git bash). For cmd terminal type cls and press enter

References:

  1. Manual for Git git manual
  2. YouTube tutorial link