Commands, Git

Basic Git Commands

Basic Git Commands

GIT is the most widely used open-source VCS (version control system) that allows you to track changes made to files.

Companies and programmers usually use GIT to collaborate on developing software and applications.

The working directory is where you add, delete, and edit the files. So, the changes are staged in the staging area.

After you commit your changes, the snapshot of the changes will be saved into the git directory.

Git is a distributed version control system (DVCS). “Distributed” means that all developers within a team have a complete version of the project.

So version control system is simply software that lets you effectively manage application versions.

Having a distributed architecture, Git is an example of a DVCS (hence Distributed Version Control System).

Rather than have only one single place for the full version history of the software as is common in once-popular version control systems like CVS or Subversion (also known as SVN), in Git, every developer’s working copy of the code is also a repository that can contain the full history of all changes.

In addition to being distributed, Git has been designed with performance, security and flexibility in mind.

Thanks to Git, you’ll be able to do the following :

  • Keep track of all files in a project
  • Record any changes to project files
  • Restore previous versions of files
  • Compare and analyze code
  • Merge code from different computers and different team members

Git commands:

Before you start using a repository you have to register with git.

To tell Git who you are run the following two commands:

$ git config --global "John Doe"
$ git config --global "[email protected]"

The git config command :

If you want to change your git user name and email run the same command again.

You can review the user name : git config
You can review the email : git config

The git config --list command :
Another way to show your Git username is with this git config command:

$ git config --list

Starting a New Local Repository with Git : The “init” command stands for initialize. Once you run “git init”, Git will initialize a hidden directory called “.git” in the project’s root directory.

$ git init

Check the git status : To know the git status run the below command.

$ git status

Create a local copy of a remote repository :

$ git clone ssh://[email protected]/[username]/[repository-name].git

Add a file to the staging area :

$ git add [file-name.txt]

Add all new and changed files to the staging area :

$ git add -A

Commit changes :

$ git commit -m [commit message]

Remove a file (or folder) :
$ git rm -r [file-name.txt]

List branches (the asterisk denotes the current branch) :

$ git branch

List all branches (local and remote) :

$ git branch -a

Create a new branch :

$ git branch [branch name]

Delete a branch :

$ git branch -d [branch name]

Delete a remote branch :

$ git push origin --delete [branch name]

Create a new branch and switch to it :

$ git checkout -b [branch name]

Clone a remote branch and switch to it :
$ git checkout -b [branch name] origin/[branch name]

Switch to a branch :

$ git checkout [branch name]

Switch to the branch last checked out :

$ git checkout -

Discard changes to a file :

$ git checkout -- [file-name.txt]

Merge a branch into a target branch :

$ git merge [source branch] [target branch]

Stash changes in a dirty working directory :

$ git stash

Remove all stashed entries :

$ git stash clear

Push a branch to your remote repository :

$ git push origin [branch name]

Push changes to remote repository (and remember the branch) :

$ git push -u origin [branch name]

Push changes to remote repository (remembered branch) :

$ git push

Delete a remote branch :

$ git push origin --delete [branch name]

Update local repository to the newest commit :

$ git pull

Pull changes from remote repository :

$ git pull origin [branch name]

Add a remote repository :

$ git remote add origin ssh://[email protected]/[username]/[repository-name].git

Set a repository’s origin branch to SSH :

$ git remote set-url origin ssh://[email protected]/[username]/[repository-name].git

View changes :

$ git log

View changes (detailed) :

$ git log --summary

Preview changes before merging :

$ git diff [source branch] [target branch]

Get the Repository Name :

$ git remote show origin

Practice those commands and make the most of your developing skills!

🙂 Happy Coding. Keep Liking & Sharing 💻

So Home
Tagged , , , ,