Most Used Git Commands

UMUT ÇELİK
HAVELSAN
Published in
5 min readNov 3, 2023

Which git commands does a developer need the most?

Firstly;

A version tracking system should be used where the code is developed with the team.

Usually, developers send their codes to the relevant branches without seeing the git commands through the ide, but cmd lovers like me, open pull request by seeing /typing the git commands.

pr: Pull requests let you tell others about changes you’ve pushed to a branch in a repository on GitHub

Let’s get started, Cmd lovers;

In this article, I will talk about which commands we will use when starting a project, and then I will talk about the commands we need most, in order.

git clone: Used to download the project codes to our local computer.
git pull: We downloaded the project to local and we take the last committed codes for the cloned branch to our local computer.
git pull origin master fetches commits from the master branch of the origin remote (into the local origin/master branch), and then it merges origin/master into the branch you currently have checked out.
git pull only works if the branch you have checked out is tracking an upstream branch. For example, if the branch you have checked out tracks origin/master, git pull is equivalent to git pull origin master
git branch -b <BRANCH_NAME>: We develop from the main branch that we downloaded to our local and determine the branch name that we will send back to the main branch. Otherwise, we pull the code from the main branch and always work on the main branch. This is not suitable for teamwork.

<BRANCH_NAME>: I recommend that you make your selection as <FEATURE_NAME> if you are making a new development,

<BUGFIX_NAME> if you find a bug and are developing, and <HOTFIX_NAME> if the error comes from a working environment.

Now everything is ready, we can start development. What did we do? We pulled the code to local, we made sure that it was in its final form by doing git pull, and we started writing code by creating a branch name that we would open a PR according to ourselves.

We started and wrote our code, now we are ready to send it to the main branch. What should we do at this stage?

First, we need to communicate what we have done via message. The git command we need for this is:

git commit — amend -m “New commit message”

We are now ready to send the code.

git push: we send our development to the main branch with this command.

So far happy path actually, we may encounter some errors. What are these?

1. Conflicts error while doing git push
2. error: failed to push some refs to [remote repo]

Solution:

1. It is necessary to merge to the main branch by using the git merge command and using an ide to visually see/select the pages that show conflicts and are not auto merge, that is, the pages that Git cannot automatically merge and resolve.

Merge :)

git merge — no-ff:

2. To resolve this issue, you should first run git pull origin master to update your local branch with the changes from the remote branch.
After resolving any conflicts that may arise during the merge, you should be able to push your changes to the remote branch using git push origin master.

When we overcome these problems, we will have matched our code in the happy scenario.

Now let’s get to know a few more git commands.

git status:The git status command is used to display the state of the repository and staging area.
git fetch :The git fetch command gets all the changes from a remote repository. The fetched metadata resides in the .git directory, while the working directory stays unaltered.
git reset :Used to undo transactions by rewriting the commit history
git show:This command is used to view the metasystems and file performance of a particular process.

git show [commit id]

git log:This command is used to list the version history of the current git branch.

git init:
This command is used to start a new git repository.

git init [repository name]

Note: If you don’t give the repository a name, it defaults to .git.
git rebase: It is a Merge process that is the same thing and aims to merge two branches. If you want to see all the changes, you should merge because merge preserves all the changes as they are. If you want a simpler, understandable and linear commit history, rebase will be more useful for you.

git chery-pick :Cherry-pick is used to take the commit from one branch and show it to another branch.

git cherry-pick <commit-hash>

Cherrys

Joker Git Commands:

git prune :The Git prune command is much like an internal housekeeping utility that cleans up unreachable Git objects.

git prune cleans.

Thank you, dear readers, for reading.

--

--