Git commands to know as a Software Engineer
Git plays a very important role in the life of a Software Engineer. It helps developers collaborate with teams to track the changes working on a project. There are many git commands available, in which some commands are very important to know.
Let’s talk about the useful Git commands you should know to better manage your workflow.
Helper command
- It displays all the necessary information about git commands
git help
Initializing the repository
- To initialize a new repository with the given name on your local machine
git init <repo-name>
Cloning a project
- It clones an already existing remote repository on the provided GitHub link, onto your local machine
git clone <repository-url>
- It initializes a git repo, and also stores all the files from the project on a given location on your machine.
Checking the branch
- It lists all the branches in your repository and also checks which branch currently we are in
git branch
- To create a branch
git branch <branch-name>
Adding files
- Add all files to the git at a time
git add .
- To add files individually
git add <filename>
Checking the current status
- Shows what has already been added to the staging area and what files have been changed that need to be added to the staging area.
git status
Get the difference between the branches
- To see the differences you made inside your branch, compared to the parent branch
git diff
- To see changes between two specific commits, by using unique commit IDs
$ git diff commit1 commit2
Committing files
- To save your changes in the local repository.
- <mesage> in quotes will be a brief description of what was changed with each commit.
git commit -m <message>
Knowing previous commits
- To know about the previous commits or view the full change history
git log
Fetching
- It recovers the latest commits from the remote repository but does not add them to a local branch on your workspace.
- With git fetch, you will get the latest updates of the repository, changes in the code, and the new branches that were created.
git fetch
Switch to another branch
- It allows you to explore between the branches made by the git branch.
- To switch to a desired branch, by providing its name
git checkout <branch-name>
- To create a new branch and switch to it
git checkout -b <branch-name>
Pull
- It will fetch all the changes from the remote repository and merge any remote changes in the current local branch.
- If you are working on the same codebase with other people, this command will allow you to pull the latest version from the remote repository and update your local version so you can work with the latest updates as their changes enter the codebase.
git pull origin <branch-name>
Push
- To transfer the local changes, to the remote repository.
- Before using push, you have to use the commit and add command in order to prepare the changes for pushing.
- When you do it for the first time,
git push -u origin master
- To push your code to GitHub after your initial push,
git push
Merge two branches
- It merges or combines branches’ commits.
- The last thing to do, once you have done working on the branch, is to merge your branch with the main branch
- First, be on the branch you want your branch to be merged into.
git checkout branch_b
git merge branch_a
- Here, branch_a is merged into branch_b(main branch).
- If conflicts occur, resolve them manually, by choosing the changes you want to retain or remove.
Copy changes from another branch
- There are some scenarios where we have to add changes to more than one branch.
- For example, if there are two versions and we support both of them, we should commit changes to both branches.
- Let’s say we have two branches - branch-A and branch-B. Instead of committing in both branches manually, we can use the git rebase command.
git checkout branch-A
git rebase branch-B
- Here, branch-A will look like it was branched from branch-B.
Deleting branches
- To delete a branch locally
git branch -d <branch-name>
- To delete a branch remotely
git push origin --delete <branch-name>
Getting to know these commands and how to use them is very important in the IT world. Just get hands-on with the commands and you will find how useful Git is. I also suggest you use any software to manage git. GitHub Desktop is available for free to handle your GitHub repositories.
I hope you found this article useful. Happy reading !!!