Table of Contents
- Introduction
- What are we trying to do
- Step 1 Create Repo in Github Website
- Step 2 Copy the Repo Url from Address Bar
- Step 3 Create a directory with similar to repo name
- Step 4 Go inside the directory
- Step 5 Run Git init command to initialize directory
- Step 6 Create branch main
- Step 7 Now add the repo to remote Git URL
- Step 8 Create a file
- Step 9 Check repo changes using git status
- Step 10 Add the files to local staging
- Step 11 Commit the staged files
- Step 12 Push the repo changes to Git
- Next steps
- Check repo changes using git status
- Add the files to git staging
- Commit the staged files
- Push the repo changes to Git
- Conclusion
Introduction
- This article is a Git-101 starter tutorial. I'm trying to provide the steps to create a repo locally and Git commands to initialize and push the repo changes.
- I've used GitHub for demo purpose, and it is similar to all Git based source code management tools
- Also this article inspired by the Git Cheat Sheet section
Create Repositories
What are we trying to do
- A new repository can either be created locally, or an existing repository can be cloned. In case a repository was created locally, user has to initialize and have to push it to GitHub afterwards
- Use Case for this article is,
- A GitHub user having a set of files in a directory (
test_repo
) and needs to be check-in into Git - User wants to know, how to create repo in github website and Git commands associated with initializing the local repo and pushing the changes to github
- A GitHub user having a set of files in a directory (
Step 1 Create Repo in Github Website
- Login into Github
- Choose create repo
- Fill-in the details in required field
Repository name
and optional fieldDescription
- Choose your repo is
public
orprivate
- Then click
Create Repository
Step 2 Copy the Repo Url from Address Bar
- Copy the Git repo url from browser address bar. It will be used later in Step 8
Step 3 Create a directory with similar to repo name
mkdir test_repo
Step 4 Go inside the directory
cd test_repo
Step 5 Run Git init command to initialize directory
- We need to download and install Git CLI tool by following Git Installation](https://git-scm.com/downloads) instruction of specific operating system
- The
git init
command turns an existing directory into a new Git repository inside the folder you are running this command.
git init
Step 6 Create branch main
- After the initiatives of
Inclusive naming convention
, it is recommended to use branch name asmain
, instead ofmaster
- So the below command renames the branch master to main
git branch -m main
Step 7 Now add the repo to remote Git URL
- After using the git init command, link the local repository to an empty GitHub repository using the following command:
git remote add origin https://github.com/chefgs/test_repo.git
Step 8 Create a file
- Create a file named
README.md
echo "## test_repo readme" > README.md
Step 9 Check repo changes using git status
git status
Step 10 Add the files to local staging
- Stages the file in preparation for versioning
git add .
Step 11 Commit the staged files
- Records staged files permanently in version history
git commit -m "first commit"
Step 12 Push the repo changes to Git
-
git push
Uploads all local branch commits to GitHub - The current branch
main
has no upstream branch in GitHub, since we initialized a local directory as repo, so use below command for the first time to push the current branch changes and set the remote branch as upstream,
git push --set-upstream origin main
- We can now see the repo updated with checked-in files
Next steps
- Once changes are pushed for the first time, follow-up changes can be pushed by running below commands
Check repo changes using git status
git status
Add the files to git staging
git add .
Commit the staged files
git commit -m "new commit message"
Push the repo changes to Git
git push origin main
Conclusion
Here is the link to the repo, created as part this article.
In this article we have discussed about,
- Creating a repo in Github
- Initializing, adding files and pushing changes into GitHub
Looking forward to write more such tutorials regarding Git commands as part Git tutorial series
Top comments (0)