General Middleware

GIT Commands with Examples

GIT is a repository management tool that helps multiple developers to code at the same time, along with maintaining a clean code base, with just few simple commands. With different piece of the application, it is important to delegate the development to multiple people, but that poses a risk to unwanted overwrites in the code base. GIT is a one stop solution to this problem. Let’s look at the different commands step by step.

Step1. Setting up the repository in GIT

Sign up with Github , if you don’t have an account already.

Step2. Click New, to create a new repository in Github

Step3. Choose a name, and select either Public or Private, depending on the need.

For this example, our repository name is _demo, and it is declared as Public.

Step4. Let’s now create a directory in our local machine, and put all the initial files, that has to go to the repository

Here is how my local directory looks like. It consists of a set of images.

Step5. Now, grab the remote url for the repository from Github

Step6. Now convert your local directory (created in Step4) into a repository

bash-3.2$ git init
Initialized empty Git repository in /Users/animeshbanerjee/Documents/mware/MyPlayground/_demo/.git/
bash-3.2$ git remote add origin https://github.com/anijumech/_demo.git
bash-3.2$ 

Step7. You local repository is all set and is now configured to push files to the remote repository in Github

Perform git status to check the current status of your local repository. The below output shows that :

  1. You are on the master branch
  2. All the image files in the current directory are currently untracked i.e they are not ready to be committed to local repository yet.
bash-3.2$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	art.jpg
	bootstrap.jpg
	carousel1.png
	carousel2.png
	carousel3.png
	carousel31.png
	cross.png

nothing added to commit but untracked files present (use "git add" to track)
bash-3.2$ 

Step8. Now, we have to update the local repository with all those untracked files, or whichever files we want to.

Let’s follow the simple ACP rule to update files in local repository and finally push to the remote Github

Agit add | git rmThis is to add or remove your local files to your local repository
Cgit commit -m “put your comments”This is to commit the added or removed files to the local repository
Pgit push origin masterThis is to push your commits the master branch of the remote repository

Note: Specify multiple files or directories using regular Unix syntax

Displaying the output from the different commands in the ACP. Note that git status doesn’t do anything, but just shows the status of the local repository. It is good to perform git status after every command to see the changes.

1. git status

bash-3.2$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	art.jpg
	bootstrap.jpg
	carousel1.png
	carousel2.png
	carousel3.png
	carousel31.png
	cross.png

nothing added to commit but untracked files present (use "git add" to track)

2. git add .

bash-3.2$ git add .
bash-3.2$ 

3. git status

bash-3.2$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   art.jpg
	new file:   bootstrap.jpg
	new file:   carousel1.png
	new file:   carousel2.png
	new file:   carousel3.png
	new file:   carousel31.png
	new file:   cross.png

bash-3.2$ 

4. git commit

bash-3.2$ git commit -m "Updating all the image files"
[master (root-commit) ddebf79] Updating all the image files
 Committer: Animesh Banerjee <animeshbanerjee@Animeshs-MacBook-Pro.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 7 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 art.jpg
 create mode 100644 bootstrap.jpg
 create mode 100644 carousel1.png
 create mode 100644 carousel2.png
 create mode 100644 carousel3.png
 create mode 100644 carousel31.png
 create mode 100644 cross.png
bash-3.2$ 

5. git push origin master

bash-3.2$ git push origin master
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 16 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 1.42 MiB | 2.61 MiB/s, done.
Total 9 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/anijumech/_demo.git
 * [new branch]      master -> master
bash-3.2$ 

Step9. Let’s now look at our remote repository

We can see all the files uploaded with the appropriate comment specified during the push.

Common Use Cases

  1. To push changes to a temporary remote branch, follow below commands.
bash-3.2$ git checkout -b mytempbranch
Switched to a new branch 'mytempbranch'
bash-3.2$ ls -lrt
total 2832
-rw-r--r--  1 animeshbanerjee  staff   21628 Jul  3 00:38 bootstrap.jpg
-rw-r--r--  1 animeshbanerjee  staff  988966 Jul  3 11:31 carousel31.png
-rw-r--r--  1 animeshbanerjee  staff  104269 Jul  3 11:59 carousel1.png
-rw-r--r--  1 animeshbanerjee  staff  178983 Jul  3 11:59 carousel2.png
-rw-r--r--  1 animeshbanerjee  staff  141559 Jul  3 11:59 carousel3.png
-rw-r--r--  1 animeshbanerjee  staff    1927 Jul 25 16:45 cross.png

bash-3.2$ git status
On branch mytempbranch
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	deleted:    art.jpg

no changes added to commit (use "git add" and/or "git commit -a")

bash-3.2$ git rm art.jpg
rm 'art.jpg'
bash-3.2$ git status
On branch mytempbranch
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	deleted:    art.jpg


bash-3.2$ git commit -m "art.jpg deleted"
[mytempbranch af182e4] art.jpg deleted
 Committer: Animesh Banerjee <animeshbanerjee@Animeshs-MacBook-Pro.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 art.jpg

bash-3.2$ git push orgin mytempbranch
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 242 bytes | 242.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote: 
remote: Create a pull request for 'mytempbranch' on GitHub by visiting:
remote:      https://github.com/anijumech/_demo/pull/new/mytempbranch
remote: 
To https://github.com/anijumech/_demo.git
 * [new branch]      mytempbranch -> mytempbranch
bash-3.2$ 

Now, you can see the new branch created with the changes in the Github

This is the new branch, mytempbranch, and it doesn’t have the art.jpg
This is the master branch, and it contains the art.jpg file

2. Create a pull request in Github to merge the new branch, mytempbranch, with the master.

Compare and initiate the pull request

Add appropriate comments and create the pull request
Merge the pull request

Finally, you get the changes updated in the master branch. In our case, the master branch doesn’t have the art.jpg file anymore.

Leave a Reply

Your email address will not be published. Required fields are marked *