Home Salesforce Useful Git Commands

Useful Git Commands

by Dhanik Lal Sahni

Version Control is important for Team-Based Development for every organization.  We can use TFS or Git for versioning control.

What is Git?

Git is distrbuted version control system. Distributed version control systems give developers flexibility and freedom by providing every collaborator with a copy of the entire repository at any time, including all branches (lines of work) and commits (saved points in history) on a developer’s local machine.

What is Github?

Github is collaboration platform built on top of Git. GitHub serves as a community space where we can share our work, see our team’s work, complete code reviews, and connect integrations that help us build, test, and deploy our code. GitHub is home to the largest collection of open source projects in the world.

We can utilize git commands for GitHub and TFS code as well.

Git Commands :

Local Environment Setup before using Git:

We should set some configuration before using git commands like user information.

To set user name for local git repository use below commands

git config --global user.name "Dhanik Sahni"

To set user email for local git repository use below commands

git config --global user.email "dhanik.sahni@yahoo.com"

Git Network Command:

Git has four network commands git clone, git fetch, git pull, and git push which work for local and remote repository.

git clone

Git clone is a git command line tool which is used to target an existing repository and create a clone, or copy of the target repository
1. Cloning to a specific folder

git clone <repo> <directory>
git clone https://github.com/dhaniksahni/rotateGlobeInLightning.git c:\code

2. Cloning a specific branch

git clone branch <branch> <repo>
git clone -branch new_feature https://github.com/dhaniksahni/rotateGlobeInLightning.git
here new_feature is branch

git fetch

The git fetch command downloads commits, files, and refs from a remote repository into local repository. It will download the remote content but not update local repo’s working state, leaving current work intact.

git fetch <remote>
https://github.com/dhaniksahni/rotateGlobeInLightning.git
git fetch <remote> <branch>
https://github.com/dhaniksahni/rotateGlobeInLightning.git new_feature

git pull

The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content.

git pull <remote>  -- Fetch the specified remote’s copy of the current branch and immediately merge it into the local copy. 
git pull https://github.com/dhaniksahni/rotateGlobeInLightning.git

git push

The git push command is used to upload local repository content to a remote repository. Pushing is transferring commits from local repository to a remote repository.

git push <remote> <branch>
git push https://github.com/dhaniksahni/rotateGlobeInLightning.git new_feature
//new_feature is branch

Other Important git commands

git branch

This will list out all local branches.

git branch --List of local branches

git branch myfeaturebranch --this will create new branch in local repository

git checkout

The git checkout command is used to switch between branches in a repository. Example : We have multiple branches for our repository and we want to take code for any specific branch. we can checkout for that specific branch.

git checkout master  --master is branch
git fetch

git add

This command will add file to local repository.

git add README.md --Will add README.md in repository

git commit

The “commit” command is used to save our changes to the local repository. It will not push code automatically to the remote server. Using the “git commit” command only saves a new commit object in the local Git repository.

git commit -m “Commit to resolve merge conflict” --This will commit and comment will be added

git log

The git log command enables us to display a list of all of the commits on current branch. By default, the git log command presents a lot of information all at once.

git log -10 --will only show the 10 most recent commits.

git diff

git diff will compare between any two commits, branches, or tags in the repository.

git diff 4e3dc9b 0cd75d4 --4e3dc9b and 0cd75d4 are commit #

git revert

git revert will undo changes. It can be used on commits at any point in the repository’s history without affecting other work. This will revert from remote repository as well.

git revert <commit-id>  -- Commit-id will be checked using git log

git reset

This will help to rewind committed code to local repository. It will not revert code if it is already pushed to remote. Use git revert in that case.

git reset --hard f414f31  --f414f31 is commit id

Reference:

https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-clone
https://www.atlassian.com/git/tutorials/setting-up-a-repository

You may also like

1 comment

Dhanik Lal Sahni March 30, 2020 - 12:31 am

Thank You Abhishek.

Reply

Leave a Comment