DEV Community

Cover image for A set of handy git commands for beginners to make your life easier!
𝐁𝐚𝐛𝐢 ✨
𝐁𝐚𝐛𝐢 ✨

Posted on

A set of handy git commands for beginners to make your life easier!

Starting out with version control is exciting however keeping track of all the commands can be difficult.

This is a simplified cheat sheet with short explanations to guide you on which command does what and if the changes made are locally, remotely or both.

1. First-Time Git Setup

Once git is installed on your system you have to customize your git environment. This can be done using the git config tool

i. To setup your Git username

git config --global user.name <username>
Enter fullscreen mode Exit fullscreen mode

Example

git config --global user.name janedoe
Enter fullscreen mode Exit fullscreen mode

ii. To setup your Git username

git config --global user.email <emailAddress>
Enter fullscreen mode Exit fullscreen mode

Example

git config --global user.email janedoe@gmail.com
Enter fullscreen mode Exit fullscreen mode

iii. To check Git configuration

git config -l
Enter fullscreen mode Exit fullscreen mode



🡅 back to TOC

2. Creating A Repo 🖊

These are probably the first command every git enthusiast learns.

i. To create a local repo

git init
Enter fullscreen mode Exit fullscreen mode

You should run this command in the folder you want to become the root(base) folder for your repo

ii. To clone an existing repo

git clone <repo-clone-link>
Enter fullscreen mode Exit fullscreen mode

Example:

git clone https://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode

Ensure that the link is copied from the green clone button NOT the browser tab

clone button open button

iii. Linking to a remote repo

git remote add origin <link-to-github-remote-repo>
Enter fullscreen mode Exit fullscreen mode

Example:

git clone https://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode




🡅 back to TOC

3. Managing Local Changes (including undos) 🛠

i. Checking files you've worked on without committing (unstaged files)

git status
Enter fullscreen mode Exit fullscreen mode

ii. To stage files

  • To stage particular files, you'll have to specify the filenames (including the extensions)
git add <filename>
Enter fullscreen mode Exit fullscreen mode

Example:

git add index.html
git add index.html styles.css app.js
Enter fullscreen mode Exit fullscreen mode

To check which files to add run git status

  • To stage all files at once
git add .
Enter fullscreen mode Exit fullscreen mode

Note: Do no forget the decimal point

iii. To unstage (unadd or uncommit 😄) files

These can be used for commits that have not been pushed

To unstage a single file

git reset HEAD <filename>
Enter fullscreen mode Exit fullscreen mode

Example:

git reset HEAD index.html
Enter fullscreen mode Exit fullscreen mode

To unstage all stage files

git reset HEAD
Enter fullscreen mode Exit fullscreen mode

To unstage the last file you staged

git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode

Note: Be sure that you don't have any additional changes to the files

iv. To commit staged files

Once you've stage a file you can then commit it

git commit -m "<commit-message>"
Enter fullscreen mode Exit fullscreen mode

Example:

git commit -m "initial commit"
Enter fullscreen mode Exit fullscreen mode

Note: Do not forget the commit message

To commit means to capture a snapshot of the project's currently staged changes and "commit it to git memory"

v. To stage and commit all changes at once

git commit --am "<commit-message>"
Enter fullscreen mode Exit fullscreen mode

Example:

git commit -a -m "styles and design done"
Enter fullscreen mode Exit fullscreen mode

vi. To correct your last commit message

git commit --amend -m "<commit-message>"
Enter fullscreen mode Exit fullscreen mode

Example:

git commit --amend -m "Corrected: first commit"
Enter fullscreen mode Exit fullscreen mode

Note: Don't amend published(pushed) commits!

vii. To rollback commits

These are for commits that have been pushed

To rollback the latest commit

git revert HEAD
Enter fullscreen mode Exit fullscreen mode

To rollback to a specific commit

git revert <commitID>
Enter fullscreen mode Exit fullscreen mode




🡅 back to TOC

4. Managing Branches

i. To list all existing local branches

git branch 
Enter fullscreen mode Exit fullscreen mode

Example:

git branch

ii. To list all branches (remote and local)

git branch -a
Enter fullscreen mode Exit fullscreen mode

iii. To create a new branch on the current head

You can think of the HEAD as the "current branch". When you switch branches with git checkout (switch branches), the HEAD revision changes to point to the tip of the new branch

git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

Example:

git branch develop
Enter fullscreen mode Exit fullscreen mode

iv. To switch to an existing branch an update current working directory

git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

Example:

git checkout develop
Enter fullscreen mode Exit fullscreen mode

v. git checkout -b

Creates new branch from current branch(the branch you are currently working on) and checkout out to it.

This move can be done even when you have unstage files. The untracked files will now be in the new branch

vi. To delete a branch

To delete a merged branch locally. Do not be on the branch to be deleted.

git branch -d <branch>
Enter fullscreen mode Exit fullscreen mode

To delete a branch locally whether merged or not. Do not be on the branch to be deleted.

git branch -D <branch>
Enter fullscreen mode Exit fullscreen mode

Note: These delete the local branch only


To delete a remote branch from terminal.

git push origin --delete <branch>
Enter fullscreen mode Exit fullscreen mode

Note: This deletes the remote branch only

vi. To rename a branch

To rename a branch while on another branch

git branch -m <oldName> <newName>
Enter fullscreen mode Exit fullscreen mode

To rename your current working branch

git branch -m <newName>
Enter fullscreen mode Exit fullscreen mode

If you want upstream to reflect your changes, that is the branch upstream to have the same naming as your local branch

git push origin -u <newName>
Enter fullscreen mode Exit fullscreen mode

vii. To set local branch to track remote branch

 git branch --set-upstream-to=origin/<remoteBranchName> <localBranchName>
Enter fullscreen mode Exit fullscreen mode

vii. Pushing a branch for the first time

If you clone a project, created a branch, worked on it and want to push it to upstream for the very first time

git push --set-upstream origin <branchName>
Enter fullscreen mode Exit fullscreen mode

After this is done, just do git push whenever you wish to push that branch to the one upstream



🡅 back to TOC

5. Pushing And Pulling

i. To push local branch for the first time

This is to push a branch that was created locally for the very first time. That is, it does not exist remotely.

 git push --set-upstream origin <branchName>
Enter fullscreen mode Exit fullscreen mode

OR

For short

git push -u origin <branchname>
Enter fullscreen mode Exit fullscreen mode

ii. To push

To push a branch which already exists and has already been set to an upstream

git push
Enter fullscreen mode Exit fullscreen mode

Note: The local branch must be set to track to remote branch. Use this command to set the branch to upstream.

iii. To push to a specific origin

git push origin <remoteBranchName>
Enter fullscreen mode Exit fullscreen mode

iv. To pull

To pull from the remote branch and directly merge/integrate into HEAD recursively

git pull
Enter fullscreen mode Exit fullscreen mode

Use this command to set the branch to upstream.

v. To pull from a specific origin

To pull from the specific remote branch and directly merge/integrate into HEAD

git pull origin <branchName>
Enter fullscreen mode Exit fullscreen mode



🡅 back to TOC

6. To fetch

i. Synchronizing to the central repository

To synchronize your local repository to that of the central repository's main branch

git fetch origin
Enter fullscreen mode Exit fullscreen mode

Any branch that was behind the central repository's main branch will be downloaded.

ii. Fetching a remote branch

To fetch and update a branch exists locally and remotely or to fetch a remote branch locally. Checkout to that branch

git fetch <remote> <branch>
Enter fullscreen mode Exit fullscreen mode



🡅 back to TOC

7. Merging

Merging is simply combining (or merging) branches
As a beginner with git, when working in a team with others, I merge my branches on Github (after creating pull requests) so I could compare changes and easily resolve any conflicts. Then pull the work down to my local branch

i. Merging two local branches

On the terminal, to merge a branch into another, you have to be in the branch you want to merge into. Then run the command

git merge <branch-to-be-merged>
Enter fullscreen mode Exit fullscreen mode

ii. Merging a remote branch into a local branch

First fetch the branch, then merge using the merge command above

Top comments (0)