Posted in Information Technology

Git Cheat Sheet

Overview

Git is the go-to version control tool for most software developers because it allows them to efficiently manage their source code and track file changes while working with a large team. In fact, Git has so many uses that memorizing its various commands can be a daunting task, which is why we’ve created this git cheat sheet.

 

Setup Git:

After Git is installed, whether from apt-get or from the source, you need to copy your username and email in the gitconfig file. You can access this file at ~/.gitconfig.

Opening it following a fresh Git install would reveal a completely blank page:

sudo vim ~/.gitconfig

You can use the follow commands to add in the required information. Replace ‘user’ with your username and ‘user@example.com’ with your email.

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

And you are done with setting up. Now let’s get started with Git.

Repository:

Create a new directory, open it and run this command:

git init
Playing around git 1

This will create a new git repository. Your local repository consists of three “trees” maintained by git.

First one is your Working Directory which holds the actual files. Second one is the Index which acts as a staging area and finally the HEAD which points to the last commit you’ve made.checkout your repository using git clone /path/to/repository.

Suggested read  Must Have Essential Linux Applications

Checkout your repository (repository you just created or an existing repository on a server) using git clone /path/to/repository.

Add files and commit:

You can propose changes using:

git add <filename>

This will add a new file for the commit. If you want to add every new file, then just do:

git add --all

Your files are added check your status using

git status
Playing around git 2

As you can see, there are changes but they are not committed. Now you need to commit these changes, use:

git commit -m "Commit message"
playing around git 3

You can also do (preferred):

git commit -a

And then write your commit message. Now the file is committed to the HEAD, but not in your remote repository yet.

Push your changes

Your changes are in the HEAD of your local working copy. If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it first with:

git remote add origin <serveraddress>

Now you are able to push your changes to the selected remote server.To send those changes to your remote repository, run:

git push -u origin master

Branching:

Branches are used to develop features which are isolated from each other. The master branch is the “default” branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion.

Create a new branch named “mybranch” and switch to it using:

git checkout -b mybranch
Playing around Git (4)

You can switch back to master by running:

git checkout master

If you want to delete the branch use:

git branch -d mybranch
Playing around Git (5)

A branch is not available to others unless you push the branch to your remote repository, so what are you thinking about just push it:

git push origin <branchname>

Update and  Merge

To update your local repository to the newest commit, run:

git pull

In your working directory to fetch and merge remote changes.To merge another branch into your active branch (e.g. master), use :

git merge <branch>

In both cases, git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible for merging those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged with

git add <filename>

Before merging changes, you can also preview them by using

git diff <sourcebranch> <targetbranch>

Git log:

You can see the repository history using.

git log

To see a log where each commit is one line you can use:

git log --pretty=oneline

Or maybe you want to see an ASCII art tree of all the branches, decorated with the names of tags and branches:

git log --graph --oneline --decorate --all

If you want to see only which files have changed:

git log --name-status

And for any help during the entire process, you can use git --help

 

Reference:

https://itsfoss.com/basic-git-commands-cheat-sheet/

https://git-scm.com/docs

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s