Git Series — 3. Complete Basic Flow
In previous article of Git Series, we discussed Git Basics including creating a git repository, tracking files, staging and unstaging files, committing changes, adding remotes, undoing commits, pushing and pulling changes, tags and aliases. All of the material might sound complicated for some users and they may be interested in a simplified flow for using git for their normal and basic usage. So in this article we will go through a basic and simplified flow of git from creating an empty git repository to pushing and pulling code.
Pre-Requisites
I am assuming that you have installed git on you machine and have completed the first time setup. I will use Git Bash for windows in this article. You can use and command line tool as per your OS. You will also need a GitHub account.
Let’s Start
Example 1
- Create an empty Folder named git-demo-1
- Open your command line tool and navigate into git-demo-1 folder.
- Type
git init
in your CLI
- This will create an empty
.git
(hidden) folder. - Type following two commands one by one
git status
git remote -v
- You will se following results which means that there are no changes in the folder tracked yet and no remotes are set up for this folder.
- No add a txt file named
demo.txt
in the folder and typeLine 1
in this file and save the file.
- In your CLI type following commands
git add .
git commit -m "First Line Added"
- Now that you have committed your changes, we need to push them to GitHub. For this, we need to create a repository on GitHub and connect this GitHub repository with our local repository of git-demo-1 folder.
- For This, you need to first login to your GitHub and create a repository.
- Copy the link of this GitHub repository.
- In your CLI type
git remote add origin LINK_OF_GITHUB_REPOSITORY
- You can type
git remote -v
to confirm that remote has been added. Adding remote will be required only once.
- Now that your local repository is connected with GitHub, you can type
git push — set-upstream origin master
to push your local files to GitHub. set-upstream will be required only when pushing for first time.
- You can refresh your GitHub repository page and see your demo.txt file on GitHub.
- Now lets add another file in our local folder and push it. But before you make any change in your folder, every time enter
git pull
command in CLI first. (This command is not required in this case but we will see it use later). - Create a new file demo2.txt and write something in it and save it.
- Now all you need to run is following three commands in this sequence.
git add .
git commit -m "Demo 2 File Added"
git push
- From now on, every time you make a change in your folder, you will need to run only these three commands.
- Now lets see when
git pull
command will be required. Go to your GitHub repository and make some changes in demo.txt file and commit them.
- Now in your CLI, type
git pull
. This will add the changes made in demo.txt file on GitHub into our local demo.txt. You can open local demo.txt file to confirm. Running this command everytime before changing somethin in local folder will help you avoid merge conflicts. Merge conflicts often occur when multiple people are working on the same project.
So we have learned how to create an empty git folder, push changes first time and pushing changes after first time.
In Next Article we will take a dive into Git Branches.