0% found this document useful (0 votes)
48 views23 pages

Git Github COMMANDS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views23 pages

Git Github COMMANDS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Git commands

2 ways for creating git repo:


- git init (inside an empty folder to initialize git repository)
- git clone <remote-repo>
Git commands
- 1st way -> Locally initialize new git repository for an existing project:

If you already have projects locally, then you need to make those folders git
repository.

$ cd ~/Desktop/app-demo

app-demo - $ ls

index.html style.css server.js

How do I check whether the current folder is a Git repo or not?


Git commands
How do I check whether the current folder is a Git repo or not?

$ git status
fatal: not a git repository (or any of the parent directories): .git
OR

$ ls -la

(check for .git hidden file, if you see it in the current directory, then it means

that current directory/folder is a git repository)

DO NOT RUN git init command everywhere you see.

Always run inside a project folders or new empty folders.


Git commands
$ git init

After git init, git will track every change that takes place within
that folder.
Git commands
- Now go to Github and create a new repo
Git commands
Git commands

How do we check if our local repo connected to remote repo?

$ git remote -v

This command will tell whether your local repository is connected to the
remote repo or not.

How do we connect local repos to remote repos?


Git commands
$ git remote add origin https://github.com/agaparkg/app-demo.git

$ git push -u origin main (first time only, next consecutive pushes,

just: $ git push)


Git commands
- 2nd way -> Clone the remote repo
Git commands
Open your terminal and go to your desktop
$ cd ~/Desktop
Create new folder
$ mkdir github; cd github (you can run two commands at the same time by putting “;”
in between)
$ git clone https://github.com/agaparkg/app-demo.git
$ cd app-demo
$ git remote -v
origin https://github.com/agaparkg/app-demo.git (fetch)
origin https://github.com/agaparkg/app-demo.git (push)
Additional git commands
There are 3 ways of cloning and contributing to the remote repo:

1. If it is your own github account and repository, you can clone, push and
pull directly to the main branch without any issues
2. If it is someone else’s remote repo, they can add you as a collaborator
to the repo
3. If it is someone else’s remote repo, you can fork the repo and submit
pull request when you would like to merge some code into their repo.
Git commands
1. If it is your own github account and repository, you can clone, push and pull directly to
the main branch without any issues
Git commands
2. If it is someone else’s repository, one way to contribute to the project/repo is to be
added as a collaborator

Example:

- Clone this repo locally in the terminal: https://github.com/agaparkg/app-demo


- Change anything within the repo in the main branch
- Add changes to staging, commit changes, and push the changes to the remote repo
- You will get the following error:

remote: Permission to agaparkg/app-demo.git denied to agaparov-relay.

fatal: unable to access 'https://github.com/agaparkg/test2.git/': The requested URL returned


error: 403
Git commands
Git commands
3. If it is someone else’s repository, another way to contribute to the project/repo is to fork
it.

- Go to someone else Github account


- Try to fork any of their repository
- Once you that, you will get the copy of the someone else’s repo.
- The copy includes all the code, branches, and commits from the original repo.
- Pay attention to the github accounts
Git commands
- Next, clone the repo by opening the terminal on your computer and running the command:

$ git clone https://github.com/agaparkg/app-demo

$ git remote -v (check connected remote repos locally)

$ git checkout -b new_branch (create a new branch out of a main branch)

- Create a new remote for the upstream repo with the command:

$ git remote add upstream https://github.com/agaparkg/app-demo

- Check connected remote repos locally again, now you will see one for origin

and one for upstream

$ git remote -v
Git commands
- Next, make some changes and push them to the remote origin (your forked remote
repo)

$ echo “Add new test.txt file” > test.txt

$ cat test.txt

Add new test.txt file

$ git status

$ git add test.txt (OR git add .)

$ git commit -m “Added a test file”

$ git push -u origin new_branch (OR $ git push --set-upstream origin newb)
Git commands
- Next, check your github account
Git commands
Git commands
Git commands
- Separate into groups/teams
- Create team project repos in Github
- Introduce with HTML/CSS projects
- Projects are due in 2 weeks
- Work in groups (create local
branches, avoid pushing code to main
branch)
- Collaborate with each other
- Distribute tasks between each other
- Designate a team leader
Questions?

You might also like