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.
Sign up with Github , if you don’t have an account already.
For this example, our repository name is _demo, and it is declared as Public.
Here is how my local directory looks like. It consists of a set of images.
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$
Perform git status to check the current status of your local repository. The below output shows that :
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$
Let’s follow the simple ACP rule to update files in local repository and finally push to the remote Github
A | git add | git rm | This is to add or remove your local files to your local repository |
C | git commit -m “put your comments” | This is to commit the added or removed files to the local repository |
P | git push origin master | This 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.
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$ git add .
bash-3.2$
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$
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$
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$
We can see all the files uploaded with the appropriate comment specified during the push.
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
2. Create a pull request in Github to merge the new branch, mytempbranch, with the master.
Finally, you get the changes updated in the master branch. In our case, the master branch doesn’t have the art.jpg file anymore.