How Git Works — 7 Basic Steps for Beginners
Git is a distributed version control system created by Linus Torvalds in 2005. It also has an excellent support for branching, merging, rewriting repository history and more. Git is considered to be the modern standard for software development.
In this guide you are going to learn the basics of git workflows like
- Create a repository (project).
- Clone (copy) your repository to your local machine
- Add a file to your local repository and commit (save) the changes.
- Push the changes to your main branch.
- Create a branch (version), make commit, push changes to the new branch.
- Open a pull request (propose changes to master).
- Merge your branch to master branch.
Prerequisites
Install Git on your system or server. Learn how to install Git on Ubuntu.
Step 1: Create new Repository
To create a new repository go to your development platform like GitHub or BitBucket or GitLab and follow the guide to create new repository.
Here is guide on how to create a new repository in GitHub.
Initialize Git
Now you need to configure Git with your name and email so that Git associates your identity with every changes you make.
git config --global user.name "Your Name"
git config --global user.email "youremail@mail.com"
Step 2: Clone your repository
Create a new folder in your local system and navigate to your project folder and you can clone the repository that you created in the previous step.
Copy the SSH url from your repository.
Now you can use the clone command to pull the repository.
git clone ssh_url .
You need to specify the “.” at the end of the command so that the files will be cloned directly into the local folder.
Step 3: Add new File and Commit
As you have the repository cloned to your local system, it is time to start the development process.
Now you can create a new file using the touch
command.
touch filename.txt
This command will create a new file with the provided name inside your current directory.
Now you can check the status of your repository using the git status command.
You will see something similar to the above screenshot which indicates the new file that is created is not tracked.
Now you can use the add command to tell git to track the file.
git add filename.txt
Now if you check the status again you will see the new file added.
Now use the commit command to save the changes.
git commit -m "Initial Commit"
You will receive an output similar to the one below.
Step 4: Push changes to branch
Now you can push your changes to your remote repository using the push command.
git push -u origin branch_name
Now if you take a look at your project in GitHub or BitBucket you will see the new file gets added.
Step 5: Create new branch
Create the branch on your local machine and switch in this branch:
git checkout -b new_branch_name
Now you need to push the changes to remote.
git push origin new_branch_name
Step 6: Create Pull Request
Go to GitHub repository, you will see a notification to perform a pull request or you can go to the Pull Request tab and initiate a New Pull Request
Once the pull request is created you can accept and merge it or you can revise the code.
Step 7: Merge Branch
Now you can accept and merge your branch with the main branch.
Delete a Branch
To delete a branch you can delete the branch in local and apply the changes to the remote.
git branch -d branch_name
git push origin :branch_name
Conclusion
Now you have learned how to perform basic operations in git like create repo, clone, new branch, pull request, delete branch. etc
Thanks for your time. If you face any problem or any feedback, please leave a comment below.