git, java framework

A Simple guide to start Git Project for Developers


I have gone through lot of search on How to start a Git project for newbies but couldn’t find proper document so thought of writing this article .

First step is to clone the project to your local repository .

Here is the sample project link , https://github.com/test/sample-project .Since my machine is Windows, I am using Git App for my development and here is the link to download the tool. https://git-scm.com/downloads

Go to the specific git project website ,in the top right corner, you can see the button for clone with two options , Clone with SSH and Clone with HTTPS.Copy the Http Link and keep it ready . If you are windows then login to Git Tool with bash option .For Linux users refer the page here for login .Once you login successfully run the below command ,

Git Commands for Beginners

1. Git clone the project

git clone https://github.com/test/new-framework.git. If the login failed then re login with below command, git clone https://<username>@https://github.com/test/new-framework.git.

2. Create a Branch

By default your project will be in master branch .Create a new branch and make that as your local branch .
git branch <branch-name>
git checkout -b <branch-name>

Use the below command for doing both operation at once
git checkout -b <branch-name>
To delete a branch
git branch -d <branch name >
To delete a branch with force delete
git branch -D <branch name >

Sometime the local repository is not up to date then get the latest with below command
git fetch

3.Add a file

After creating new file in local branch ,move the file to staging or default change list by below command.
git add <filename >

4.Verify the file in staging

git status .
This will shows newly added file in staging

git diff –staged
This command helps to see the changes you made on staging .This is one of the good practice before you commit code.

5.Commit

Commit the changes by below command .
git commit -m “my first commit”

6.Push the changes

Once commit is successful then push your changes to remote project by below command,

git push origin <branch name>

Now your change will be available in Remote Git repository . Now go to Remote project and select Merge Request .Here select your branch as source and target as Master branch .Once its done your change will be ready for merge.

After the approval, file will be moved to master project .If you have any review comments make the changes in file and follow the steps 3,4,5,6.New changes will be reflected in same merge request after the refresh in remote project.

Any login failed during the push use the below command,after the command it will ask for username and password.

git push -u origin <your branch>

7 Save the changes in local

To see the diff between local change and your previous commit
git diff HEAD <file name>
To keep/save the changes in local branch use the command
git stash
To get the changes back then use the command
git stash pop or git stash apply

8 To discard the local change

git checkout <filename>
git reset –hard <filename>

To go back to HEAD that will discard all your local changes
git reset –hard HEAD

9 To discard the previous commit

To discard the previous commit and move to previous commit
git log – It will tell you where is your commit now
git reset –hard <previous commit>
Check again, whether it moved to previous commit
git log
In case you have local changes then use git stash and pop
before you make the reset.

9 Keep your local branch up to date

If your local branch is outdated and want to sync up with latest then use the below commands,

To checkout the master
git checkout master

To get the latest change from master branch
git pull

Merge with master branch
git checkout <your branch>
git merge master

Now master changes available in your branch .
If you find any conflict, resolve with either accept the change or over write the change
Push this changes to master/remote with below command
git push origin <your branch>

If the local file is not with latest then its need to be updated with remote file .

git merge --ff-only <latest remote commit-hash> (or)

Incase your changes are behind with remote branch ,may be you have updated file in the git repo in the UI not in local.
In that case,you have to pull the changes or rebase with below command

git pull --rebase origin <your branch name >

Sometime your merge will be failed because of conflict with local change .In that situation first resolve and fix it .

Git config

Edit the git config using the below command and set the required fields

git config --global --edit
git config -e (local)
Check the remote url 
git remote -v
git remote set-url origin <NEW_GIT_URL_HERE>
To pull master with force 
git fetch origin master
git reset --hard FETCH_HEAD

Hope this article will helpful for you .Bye for now .If you have any comments please let me know .

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.