-
To track files and folders inside a directory by git use following :
git init
-
Connecting your local directory with GitHub repo as we have an existing repo :
git remote add origin https://git.521000.beste/UserName/Git_Repo_Name.git OR git remote add origin [email protected]:UserName/Git_Repo_Name.git
-
Add everything inside the local directory to staging area and see if the files and folders were added
git add . git status
-
Creating a commit or a checkpoint you can revert back to in case something goes wrong
git commit -m "First Commit" git push -u origin master
-
Creating a branch : -b flag stands for branch
git checkout -b dev
-
To see different branches available in the repository and push it to GitHub :
git branch git push origin dev
-
To create another branch, view branches and push
git checkout -b feature/calendar git branch git push origin feature/calendar
-
How delete branch on repository as well as locally :
git push :feature/calendar git checkout dev git branch -d feature/calendar
-
Creating and merging branches :
git checkout -b feature/calendar git push origin feature/calendar
-
Do some changes to file and then do following :
git status git add . git commit -m "Dev Ready" git push --set-upstream origin feature/calendar
-
If using VSCode at the left bottom you might see branch name and change it as per your needs. If you are on master and create a new branch dev then at that point of time whatever you have in master will be copied over to dev. Again if you create feature/calendar while you were at dev then everything inside dev would be copied to feature/calendar.
-
After pushing the code we go to website, and we might see prompt showing us that something was uploaded to the branch and an option to do a pull request PR after comparing. If you don't see that go to branches and select the one you want to merge in the drop down select the base : as the destination of the PR where to push it to, if you see able to merge you are good to go.
-
If 2 piece of code have changes on the same line then that is a case of merge conflict and we have to rectify that. As git asks us which one of those two we want.
-
Scenario: You have pushed changed to remote and you want to go back to a previous commit both on remote and local.
- Get the commit ID of the wrong commit using
git log
, commit ID of the top-most commit is the id you need to put in the commandgit push origin +wrong_commit_ID^:master
Explanation: Delete the last commit Deleting the last commit is the easiest case. Let's say we have a remote origin with branch master that currently points to commit dd61ab32. We want to remove the top commit. Translated to git terminology, we want to force the master branch of the origin remote repository to the parent of dd61ab32:
git push origin +dd61ab32^:master
.
- Get the commit ID of the wrong commit using
Where git interprets x^ as the parent of x and + as a forced non-fastforward push. If you have the master branch checked out locally, you can also do it in two simpler steps: First reset the branch to the parent of the current commit, then force-push it to the remote.
git reset HEAD^ --hard
git push origin -f