Basic Git commands

Check if Git has already been installed, Git is usually preinstalled on Mac and Linux.

git –version

Type the following command and then press enter: Tell Git who you are First, you need to tell Git who you are:

git config –global “[email protected]”
git config –global “Your Name”

Some basic Git operations
When we worked on GitHub, the basic work cycle like the fork,  edit, commit, pull request,  merge. The same cycle, with a few differences, is what we will work through on the command line.

Getting the current branch

$ git status
# On branch master
nothing to commit (working directory clean)

Create new branch from remote branch name

git checkout -b newBranch origin/remote-branchName

Merge code from remote branch called dev to local user alex

git checkout dev
git pull
git checkout alex
git merge dev

Merge code from local alex to remote branch

git checkout dev
git merge alex
git push
git checkout alex

git branch commands’ various options. -a shows all local and remote branches, while -r shows only remote branches.

$git branch -a

Show all remote branch

$ git branch -r
$ git branch -v -r

There’s also another way to do figure out what branches are on your remote by actually using the remotely related commands, git remote and git ls-remote. The former displays plenty of information about the remote in general and how it relates to your own repository, while the latter simply lists all references to branches and tags that it knows about.

$ git remote show origin
$ git ls-remote –heads origin

Conflict Merge error
While merging files from the master to the local Alex branch, sometimes we may cause a conflict merge error. We can solve the conflicted merge error in a number of ways. If we want to merge conflict file to remote master file then we can use the git command.

git checkout master hitest.php
git add .
git stash

Reset last committed or undo: The 1st command will Undo local and second will force delete from remote

git reset –hard HEAD^1
git push -f

Check Last two week commits

git log –since=2.weeks

Undo the git merge that has not committed yet: first check git log. You can use

git reset –hard HEAD~1
Spread the love

Leave a Comment

Your email address will not be published.

Scroll to Top