Let's understand Git with its basic commands.
Hello geeks, ππ»ββοΈ
This article will list the Git commands you need in the daily development lifecycle while working with any version control systems.
Version Control Systems like GitHub, Gitlab, BitBucket etc., use Git commands to manage code repositories.
Before reading this article, you must know
- What is Git, Git how does it work
- Version control systems (VCS)
- Repositories
Let's understand Git commands one by one.
- Setup
- Download Git from here, according to your OS.
- create/ initialize a git repository
git init
, this command will initialize your folder as a git repository.
- check out the existing repository
git clone /path/to/repository
, this command will create a copy of your remote repository in your local system.
If your repository is hosted privately on VCS, it will ask you for your username and personal access token. (Asking for passwords is deprecated.)
You can check your VCS doc to find out where to get the personal access token .
- Local vs remote repositories
your local repository consists of three "trees" maintained by git. Git first one is your Working Directory
, which holds the actual files. The second one is the Index
which acts as a staging area, and finally, the HEAD
which points to the last commit you've made.
Your commits will be stored locally until and unless you push it.
- add files and commit code changes
After making changes in your local repo, you need to stage those changes. And for that, the command is git add <filename>
.
Similarly, you can use git add .
, add all the files that have changes.
after that, you need to fire git commit -m βcommit messageβ
, which will commit your changes to HEAD
. Yet, your changes remain to go on the remote repo.
- Push changes to remote repositories
Your changes are now in the HEAD
of your local working copy. To send those changes to your remote repository, execute git push origin <branch\_name>
.
If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with git remote add origin <remote_url>
. Now you can push your changes to the selected remote server
- Branches
Branches are used to develop isolated features(independent from each other) and work with other developers on the same repository.
So, work on different branches and merge them into a hosted branch once the work is done.
Create a new branch named "develop" and switch to it using
git checkout -b develop
.
to delete branch git branch -d feature_x
.
If you are creating a branch locally, it will not be available for others unless you push it. Use git push origin <branch>
to push the branch to your remote repository.
- Pull and merge
To fetch the latest changes on your remote repo, git fetch
.
To update your current branch with the latest commits, use git pull
.
To get another branch's code into your branch git merge <branch>
.
If you and your colleague have worked on the same line in the same file, there will be merge conflicts in your file. You need to take either your changes or your colleague's changes, or you can take both. Then you need to again stage the file with git add <filename>
. And it will be marked as merged.
Before merging changes, you can also preview them by using
git diff <source_branch> <target_branch>
- Tagging
It's recommended to create tags for software releases. You can create a new tag named 1.0.0
by executing git tag 1.0.0 <commit_id
.
That's all.
These are all the basic commands required to start using Git with VCS.
Bye. ππΌ
Keep Coding, Keep committing, and Keep pushing π»
About meπ©π»βπ»
I am a Backend Developer at DhiWise.
You can follow me on LinkedIn and Twitter for more tech updates.
Top comments (0)