Understanding GIT

 

Getting started

Method of installing GIT will be explained in the beginning.

 

Step 1

Install git by using following command

"sudo apt install git"

 

Step 2

Confirm git installation by checking for a git version

"git --version"

 

Step 3

Configure Git, enter credentials(user name and email) to identify you as the author if your work

"git config --global user.name "your_username""

"git config --global user.email "your_email_address@example.com""



To check the configuration

"git config --global --list"



Clone a repository

"git clone <repository path>"



Convert a local directory to a repository

Go to the required location and insert the command:

"git init"



Add a remote repository

Create a new project in GitLab to hold your files.

Visit this project's homepage, scroll down to Push an existing folder, and copy the command that starts with "git remote add"

On your computer, open the terminal in the directory you've initialized, paste the command you copied, and press "Enter"

"git remote add origin <git@gitlab.com:username/projectpath.git"

 

 

Download the latest changes in the project 

To work on the up-top-date copy of the project (it is important to do this every time you start working on a project), you "pull" to get all the changes made by users since the last time you cloned or pulled the project. Use "master" as <name of branch> to get the main branch code, or the branch name of the branch you are currently working in.

"git pull <REMOTE> <name-of-branch>"

When you clone a repository, "REMOTE" is typically "origin". This is where the repository was cloned from, and it indicates the SSH or HTTPS URL of the repository on the remote server.






View remote repositories

"git remote -v"



Branching

If you want to add code to a project but you are not sure if it will work properly, or you are collaborating on the project with others, and don't want your work to get mixed up, it's a good idea to work on a different branch.



Create Branch

git checkout -b <name-of-branch>



Switch to master branch

The main branch is the master branch, but you can use the same command to switch to a different branch by changing "master" to the branch name.

"git checkout master"



View changes made

"git status"



View difference between local, unstaged changes and repository.

"git diff"

 

 

Add and Commit local changes

Use "git add" to first stage (prepare) a local file/folder for committing. Then use "git commit" to commit(save) the staged files

"git add <file-name OR folder-name>"

"git commit -m "COMMENT TO DESCRIBE THE INTENTION OF COMMIT""

 

 

 

 Add all changes to commit

"git add ."

"git commit -m "COMMENT TO DESCRIBE THE INTENTION OF COMMIT

 

 

Send changes to GitLab

To push all local commits (saved changes) to the remote repository

"git push <remote> <name-of-branch>


To push your local commits to the master branch of the origin remote

"git push origin master"

 

To delete all changes in the branch 

"git checkout . "


Undo most recent commit

"git reset HEAD~1"



Merge a branch with master branch

When you are ready to make all the changes in a branch a permanent addition to the master branch, you "merge" the two together:

"git checkout <name-of-branch>"

"git merge master"

 

 

 

 Tag

 "git checkout master"


"git tag my_lightweight_tag"


"git tag -a v1.0 -m 'Version 1.0'"

"git tag"


"git push origin --tags"




Comments

Popular posts from this blog

cURL (Client Uniform Resource Locator)

JDK Version Differneces

Object Oriented Programming