Monday, May 31, 2021

Git Commands

 Basic Git commands:


Once Git is installed in our system, we need to configure git in order to work efficiently. Git commands are important to learn as all of the git features are supported through command line, which otherwise GUIs might not be able to support.

Configure

1st thing after installing Git is to set username & email.

$git config --global user.name "arib_anwar22"
$git config --global user.email "hiareeb@gmail.com"

The global command will set this username and password throughout the system and this is stored in the git config file. We can also change the username/email for specific directory, for that use the git config and remove the --global to set specifics.

Once the username & email has been set we can see the settings,
$git config --list

Create Repository

To create a repository, go the the path of the project,$git init This creates a git repository

We can also clone an existing repository instead of starting from scratch,(even in this case we will have a git repository in an existing directory. To clone a git repository we need to have the link to the specific repository and then execute the following line, $git clone <link>
If we want to clone the repository into the directory named something else of our choice we can give the name as an additional argument.$ git clone https://freakster22.github.com/JavaTrial Project1 This will change the directory to be named as Project1.

Recording Changes to Repository

Since we have our own repo now, we might have started working on it, on different files all together, to know the status of the files, we have the git status command.$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
git status gives the status of:

  • tracked as well as untracked files.
  • Informs the branch the user is on
  • Shows the files that were modified(tracked or newly added(untracked).

We also have the option to view a simplified, short status to check the files status,$ git status -s
$ git status --short

Tracking changes instead of files

If the status is too vagued for us, and we just want to know what exactly was changed, not just which files were changed, we can use git diff command,$ git diffThis command is to know, 

>>What we have changed but not yet staged.
>>What we have changed that is yet to be committed.$ git diff --staged This will tell us, what we have staged that is yet to be commmitted.
It is important to note that git diff by itself doesn't show us all the changes made since our last commit, it only shows the changes that are still unstaged. If we have staged all the changes, git diff will give no output

Staging Modified files

In order to begin tracking a new file, we use the command git add.This command stages the file, to stage all the files,$ git add *$ git add file.txt This will stage specific file.

Committing the changes

Finally, once we are ready and sure with all the changes we can commit these changes so that they don't get lost, for this we use git commit comand, simply running this command will take us to the text editor to add a commit message. Alternatively, we can skip this and give the message in the single command$ git commit -m "Added subheading"
One thing to remember is git commit will only commit the files that were in the staging staging area, that is, the files that were added using git add command. To skip adding files and directly committing(not-recommended) we can use -a option
$ git commit -a -m ' 'Add new benchmarks'
The git commit saves the changes to your local and we can't yet find it from a different location

Removing Files

To remove a file from Git we have to remove it from staging area, and then commit. git rm is the command for the rescue.

To unstage a staged file: Lets say we have changed two files and we want to commit them as two seperate changes, but accidently we type git add * and stage them both, on typing git status, we get the following msg advice, ->(use "git reset HEAD <file>..." to unstage)
The command is a bit strange, but it works.

Unmodifying Files

Suppose we realize we want to revert the files, and don't want the changes unmodify it — revert it back to what it looked like when you last committed(or initially cloned, or however you got it into your working directory). We get the hint from git status while showing the unstaged area (use "git checkout -- <file>..." to discard changes in working directoryLet us try that command;$ git checkout -- CONTRIBUTING.md $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: README.md -> README It’s important to understand that git checkout -- <file> is a dangerous command. Any local changes you made to that file are gone — Git just replaced that file with the last staged or committed version.

That's all for this post, in the next post we will dive deeper and get to know advanced commands as well as how to push to Github

No comments:

Post a Comment