Git: What You Need To Know

Commands you will use on a daily basis

Kemil Beltre
4 min readMar 13, 2022

Git is a version control system that is widely used in the programming world. It is used for tracking changes in the source code during software development. It was developed in 2005 by Linus Torvalds, the creator of the Linux operating system kernel.

Core concept

  • Control System: This basically means that Git is a content tracker. So Git can be used to store content — it is mostly used to store code due to the other features it provides.
  • Version Control System: The code which is stored in Git keeps changing as more code is added. Also, many developers can add code in parallel.
  • Distributed Version Control System: Git has a remote repository which is stored in a server and a local repository which is stored in the computer of each developer. This means that the code is not just stored in a central server, but the full copy of the code is present in all the developers’ computers.

Git: All That You Need To Know

Tell git who you are

Configure the author name and email address to be used with your commit:

git config --global user.name "Neo"git config --global user.email neo@example.com

Initializing a new local repository

git init

Clone remote repository

git clone <server>

Add files

Add a single file:

git add <filename>

Or add all files changed to staging (index):

git add .

Commit

Commit changes to head (but not yet to the remote repository):

git commit -m “message”

Push

Send changes to the master branch of your remote repository:

git push origin master

Or if you need to push the feature branch to remote:

git push --set-upstream origin <branchname>

Status

List the files you’ve changed and those you still need to add or commit:

git status

Branches

List all the branches in your LOCAL repo, and also tell you what branch you’re currently in:

git branch

Create a new branch and switch to it:

git checkout -b <branchname>

Switch from one branch to another:

git checkout <branchname>

Delete the feature branch:

git branch -d <branchname>

Amending commits

The git commit — amend command is a convenient way to modify the most recent commit.

Update commit message:

git commit --amend -m “New commit message”

Add more files to your previous commit:

git add <filename>
git commit --amend --no-edit

--no-edit : means that the commit message does not change.

Point to note

Use the amend command only in your local repository. Using amend for the remote repository can create a lot of confusion.

Undo changes

Undo commit:

git reset --soft HEAD~1

Only resets the files which are different between the current HEAD and the given commit:

git reset --keep HEAD~1

Replace the changes in your working tree with the last content in the HEAD:

git checkout -- <filename>

Stash

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

Stashing your work:

git stash

Re-applying your stashed changes:

git stash pop

Stashing untracked or ignored files

git stash -u

Managing multiple stashes:

git stash list

Re-applying a specific stash:

git stash pop stash@{#}

Cleaning up a particular stash:

git stash drop stash@{#}

or you can delete all your stashes with:

git stash clear

Update from the remote

Fetch and merge changes on the remote server to your working directory:

git pull

To merge a different branch into your active branch:

git merge <branchname>

View changes or conflicts:


git diff
git diff --base <filename>

Preview changes, before merging:

git diff <sourcebranch> <targetbranch>

Connect to a remote repository

If you haven’t connected your local repository to a remote server, add the server to be able to push to it:

git remote add origin <server>

List of all currently configured remote repositories:

git remote -v

Bonus

Clean up your git project branches

Delete all local branches merged in master and ALSO master:

git branch -d $(git branch --merged=master)

Delete all local branches merged in master but NOT master:

git branch -d $(git branch --merged=master | grep -v master)

You just removed all the useless branches on your local repository!

Removes your local remote branches based on your repository:

git fetch --prune

Some advice

  • Keep your local repository up-to-date with the code in the remote repository.
  • Do NOT force push to remote repository.
  • Do NOT amend commits to the remote repository.
  • Be very careful when you are using reset commands in git. You may have to use reset in some scenarios, but evaluate the situation completely before going ahead with hard reset.

--

--