Hands On With Git
##There are several ways to start working with Git:
Create a new repo locally
- Create the directory for the repo you want to make.
cd
into it.- run
git init
. - This will make the directory into a brand new empty local repo. This does not do anything with github its purely local.
Clone an existing repository from Github
- For example: Go here (opens in a new tab)
- The follow the steps in this screen shot:
- Once you have the URL in your clipboard,in your termimal navigate to where you want your repos to live
- run
git clone git@github.com:jaescalo/git-training-docs.git
- this will clone the repo from github to your local!
- if you type
ls
you should now see a directory for the cloned repo. - you can
cd
into it - if you type
ls
you should see the same files displayed in github. - if you type
git status
it should tell you that you are on the main branch. - it will set up a default remote called origin. Origin is simply the default name given. It can be a bit deceptive depending on if you clone first from a main repo or a fork. As matter of principle I try to not clone from a fork for that reason.
- Now you have a copy that you can work with!
Create a new empty repo from github
- Go to github.com
- Follow the steps in the screenshot. OR simply go to https://github.com/new (opens in a new tab)
- The follow the steps in
- if the repo was initialized with files you can clone it.
- If the repo is empty you can add it as a remote and push to it.
git remote add <repo name> repo_ssh_url
git push origin
Working with a repo
Clone
- For the sake of this class: clone
https://github.com/jaescalo/git-training-docs
. See instructions above :). - cd into the repo.
- run
git remote update
to get all the changes from your remote repo.
In this case there are none but i recommend running this command whenever you create a new branch, and periodically running it to get the latest changes so you can drop your commits on top of the latest shared code. - Run
git log
to now see the change reflected in your commit history - run
git remote -v
to show all the remote repositories that you might push your changes to.
Branch
- create a new branch to work from:
git checkout -b my-git-experiments origin/main
. There is a LOT here.-b my-git-experiments origin/main
says give me a new branch based off main in my remote origin.
Edit
- open
pages.index.mdx
- make an edit to the file
- run
git status
to see the file change and the location of the file.
Stage and Commit
- stage the change for a commit by doing
git add <filename-including-path>
. Note you can stage multiple files at once for use in a single commit. - commit the change and record it with a clear commit message. In this command the -m argument allows you to specify a message.
git commit -m "Made a simple change to index.mdx so that we can show how commits work."
- Run
git log
to now see the change reflected in your commit history
Push
- Try to push the changes to origin. It will fail.
git push origin
ERROR: Permission to jaescalo/git-training-docs.git denied to aweingarten.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
- It's common for people not to grant push access to public repos. In private repos you may have the ability to push.
What the heck is a github fork?
A GitHub fork is a personal copy of someone else's repository that resides in your GitHub account. Forking a repository allows you to freely experiment with changes without affecting the original project. It is a common practice in open-source development, where developers fork a repository to contribute to a project by making changes and then submitting those changes back to the original repository via a pull request.
How to Fork a Repository on GitHub
- Navigate to the Repository: Go to the GitHub page of the repository you want to fork.
- Click the Fork Button: Click the "Fork" button at the top-right corner of the page.
- Select Your Account: Choose your GitHub account or organization where you want to create the fork.
Clone the Forked Repository: Once the fork is created, you can clone it to your local machine using the following command:
git clone https://github.com/your-username/forked-repo.git
. However, in this case we already have cloned fromjaescalo/git-training-docs
. If we cloned again we'd simply have 2 disjointed folders/repos.
How to Work with the Fork
- Instead we can add this as a new remote in our local repo. e.g
git remote add <fork-name> <your fork address>
. When I am adding a new fork I like to use my github username as the remote name. So in my case I would rungit remote add aweingarten git@github.com:aweingarten/git-training-docs.git
- To verify that its working run
git remote update
that will fetch all the updates from the remote repo. - Run
git remote -v
you should see a fetch and remote record for both origin and your username! - Now you can do
git push <your remote name>
- If you have set everything up right you should see some output like below:
git push aweingarten
Enumerating objects: 23, done.
Counting objects: 100% (23/23), done.
Delta compression using up to 12 threads
Compressing objects: 100% (18/18), done.
Writing objects: 100% (18/18), 1.01 MiB | 28.07 MiB/s, done.
Total 18 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), completed with 2 local objects.
remote:
remote: Create a pull request for 'git-curriculum-v1' on GitHub by visiting:
remote: https://github.com/aweingarten/git-training-docs/pull/new/git-curriculum-v1
remote:
To github.com:aweingarten/git-training-docs.git
* [new branch] git-curriculum-v1 -> git-curriculum-v1
its telling us that we have pushed our local branch to the remote repository/fork. They have even been kind enough to give us a URL so we can submit a request to merge the code into the main repo.
Creating your first Pull Request.
- Visit the url in your browser and follow the steps in the screen shot to make your first pull request:
For any questions about submitting a pull-request see here (opens in a new tab) For more detailed information about forks gohere (opens in a new tab)
Conclusion
Congrats you have successfully:
- Configured Git
- Cloned a repo
- Forked the repo
- Added a remote
- Created a local branch
- Made an edit to a file
- Make a commit to your local branch
- Pushed that change to github
- Submitted a pull request.
In the next class we will talk about code reviews, and dealing with fixing mistakes and even rewriting history. If you like Back to the Future
you will love the next class!