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.
2. create/ initialize a git repository
git init
. This command will initialize your folder as a git repository.
3. 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.
4. 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.
5. 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.
6. 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
7. 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 usinggit checkout -b develop
To delete the branch git branch -d <branch_name>
.
If you are creating a branch locally, it will not be available for others unless you push it. Use git push origin to push the branch to your remote repository.
8. 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
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
And it will be marked as merged.
before merging changes, you can also preview them by usinggit diff <source_branch> <target_branch>
9. 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.