DEV Community

Surjith S M
Surjith S M

Posted on • Updated on

My Learnings from using Git along with VSCode

I decided to start with Git & Github for one of my existing project.

I started with creating a Github Repo with my account.

Mistake #1

I initiated the repo with Readme & .gitignore. Since I already have a project to publish to github, you should keep it blank or otherwise you will get error asking to pull first and that pulling will also fail due to history unmatch error, same as me :)

Initialize Git

First install git from https://git-scm.com/ and its better to install Powershell as well if not installed before. (Only for Windows users ๐Ÿ˜‹)

Then open project folder in VSCode and click the third icon from the left menu called "Source Control" and click "Initialize Respository"

Init Repo on VSCode

Terminal way.

Open VSCode and press Ctrl + ` to open integrated terminal. In my case its powershell.

Now type the following command.

> git init
Enter fullscreen mode Exit fullscreen mode

Alt Text

Adding a Remote Server (Connect to Github)

Inside VSCode press CTRL+SHIFT+P and type "Git Origin", Select the same from the list and add your Origin URL (ie github *.git URL)

The same can be done via terminal. See below.

$ git remote add origin https://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode

Once added you can commit your changes by clicking the "Tick Icon" in Source control Tab. When you are ready to push to github, click the 3 dots ... menu and choose "Push".

Alt Text

This is where I did wrong. As I already have an existing project but Github is initiated with Readme, it asks to pull first. When doing that it pops another error says the history is not matching.

If you got this error, please read Mistake #1 to avoid this later. For now, you can use the below code to overwrite it.

$ git pull origin master --allow-unrelated-histories
Enter fullscreen mode Exit fullscreen mode

Git Stash & POP

This is useful when there are multiple people working on same project or working on multiple branches.

What Git Stash do is to save all of our working files in a temporary storage and revert to the original state. So that we don't lose our changes. From there you can pull latest and commit using Git Stash Apply or Pop.

When do you need this?

  1. When any other developer modified same files and you want to see / work on that updated files.
  2. When you mistakenly worked on X feature branch but should actually be Y feature branch.

Commands


$ git stash 
// Stash current directory and revert to original

$ git stash save "message"
// save in a spearate stash (multiple stash)

$ git stash list
// See current stash

$ git stash apply
// Apply saved stash but keep the stash

$ git stash pop
// apply saved stash and delete

Enter fullscreen mode Exit fullscreen mode

Then after these commands, you will be able to to pull / push as usual.


Some stackoverflow answers also mentions the use of --force or -f tag but if its in case of big project it might get messed up.

Git Checkout Branch

Branch is a powerful feature in git where you can make edits to a project without affecting the main source code.

Checkout existing branch

To checkout existing branch, use the following command


$ git checkout existing-branch-name 

Enter fullscreen mode Exit fullscreen mode

Creating new branch

To create a new branch, use the following command


$ git checkout -b new-branch-name 

Enter fullscreen mode Exit fullscreen mode

These commands will change the current working directory to your newly selected branch.


I will be adding more findings when I learn them, If you have any suggestions to improve my Git workflow, comment below.

Peace!

Top comments (2)

Collapse
 
oladele_td profile image
Oladele David Temidayo

So true had similar issues too recently. Had to use the --f tag to push

Collapse
 
Sloan, the sloth mascot
Comment deleted