Basic git tutorial for beginners

Mumtaz
5 min readOct 26, 2022

This post lists the git commands that a developer uses almost daily

Terminology

origin — This is a shorthand for the remote repository. If it’s a cloned repo then that original repository’s URL is called the origin. It always refers to the remote host. This is a standard and conventional name but it can be renamed to anything else.

It is possible to have multiple origins, therefore it becomes important to specify which remote you intend to pull the code from or push the code to. For e.g. you can have an origin where code changes are regularly pushed and another one for production releases.

staging area — This is like a rough draft space, it’s where you add file(s) that you want to save in your next commit. You can take files out of the staging area too, then these files will not be part of the commit.

master — The default branch name in Git is master when you create a new repo.

get the name of the remote

git remote -v → mostly it is going to be origin

rename “origin”

git remote rename origin <new_origin_name>

create a new repository

git init

This creates a default branch named master

clone a remote repository

git clone <git remote URL>

e.g

git clone https://www.example.git

create a new local branch

git checkout -b <branch_name>

switch to an existing branch on local

git checkout <branch_name>

list files to add, commit to staging area

git status

preview changes before merging

git diff

git diff <file>

git diff <source_branch> <target_branch>

list all the branches in the repo

git branch

add modified file to staging area

git add <file>

To add all the modified files in current directory use:

git add .

or, to add everything at once

git add -A

delete files from remote and filesystem

One way is to right click file → delete then execute git add <file>

or

git rm <file>

remove files from staging area

git restore --staged . → This preserves the changes that you made but takes the file out of staging area

discard your local changes

git restore <file> → discard changes in the <file>

git restore . → discard all files

git checkout <branch_name> <file> → discard your local changes and add changes from another branch

e.g. if I have conflicts in a file and I just want to discard my changes and have what is available in master branch

git checkout master <file>

to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it, do this:

git fetch origin
git reset --hard master

merge changes from another branch to your current working branch

git merge <another_branch>

This will merge local changes of <another_branch> with your current branch. If there are changes on remote but are absent in you local <another_branch> those changes won’t be merged into your current branch. To fetch and merge the remote changes use:

git pull origin <another_branch>

resolve conflicts

conflicts occur when same line is modified in 2 branches. Conflicts can occur while merging another branch into current branch, or while rebasing.

resolve merge conflicts:
git checkout <branch_name> → current branch
git merge master → branch we are trying to merge into current branch

Conflicts can be fixed manually by editing the conflicted file or by discarding all changes of a particular branch and keeping changes of only 1 branch

git checkout --ours <file> → to select changes done in <branch_name>

git checkout --theirs <file> → to select changes done in the master branch

then continue the merge:
git add <file>
git merge --continue

resolve rebase conflicts:
Here --ours and --theirs reverse the roles

git checkout <branch_name> → current branch
git rebase master → rebase from master

git checkout --ours <file> → to select changes done in <master>

git checkout --theirs <file> → to select changes done in the <branch_name>

then continue the rebase:
git add <file>
git rebase --continue

squash commits into one commit

git reset --soft master → if current branch is cut out from master

git commit -m "commit_msg"

commit all staged files

git commit -m "commit message"

commit all staged/unstaged files

git commit -a

push local changes on your branch to remote

git push origin <branch_name>

your branch is now on remote and is accessible over the internet not just on your system.

get latest updates, branches, tags from the remote

This won’t pull latest changes to your current working branch, but get new branches, tags that got pushed to remote

git fetch

update your local with remote changes

git pull origin

This updates the current branch that you are currently on with latest changes pushed by others working on the same repo.

This is same as the combination of git fetch and git merge

delete a branch from local

git branch -d <branch_name>

The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet.

To delete a branch you should be on a different branch from the branch getting deleted.

delete a branch from remote

git push origin :<branch_name>

or

git push origin --delete <branch_name>

rename a branch on local

To rename a branch switch to the branch that needs a rename and type

git branch -m <new_branch_name>

If you want to rename a branch that you are not currently on, then:

git branch -m <old_branch> <new_branch>

rename a branch on remote

Firstly, rename the branch on local then execute:

git push origin -u <new_branch_name>-u is short hand for setting the upstream

Note: Every time you fetch updates from remote or push your changes to remote you have to mention the origin. To skip mentioning the origin all the time, you need to set the upstream:

git branch --set-upstream-to origin

then you can just execute git pull or git push and it will push/pull latest code from the origin.

see git history

git log → it’s a record of commits.

tagging

You can use tagging to mark a significant change, such as a release:

git tag <tag_name> <commit_id>

push tags to remote

git push --tags origin

pick selective commits from any branch

git cherry-pick <commit_sha> → choose a commit from one branch and apply it onto current branch

visualisation of git branching
git branching

--

--