Lab-2-Git Commands Lists
Lab-2-Git Commands Lists
2. git add
Usage (i): git add [file(s) name]
This will add the specified file(s) into the Git repository, the staging area, where they
are already being tracked by Git and now ready to be committed.
3. git commit
Usage: git commit -m “message”
This command records or snapshots files permanently in the version history. All the
files, which are there in the directory right now, are being saved in the Git file system.
4. git status
Usage: git status
This command will show the modified status of an existing file and the file addition
status of a new file, if any, that has to be committed.
5. git remote
Usage: git remote add origin “[URL]”
Once everything is ready on our local system, we can start pushing our code to the
remote (central) repository of the project. For that, follow the below steps:
Step 1:
(1) Login to the GitHub account if the account already exists (If not, sign up on
github.com)
(2) Click on New
Step 3: Click on the Copy icon on the right side of the URL box of the Github
repository to copy the link and paste it as shown below:
git remote add origin “URL”
Now, we are ready to operate the remote commands in our repository that we have
just created.
6. git push
Usage: git push origin [branch name]
Suppose, we have made some changes in the file and want to push the changes to
our remote repository on a particular branch. By using the command ‘git push,’ the
local repository’s files can be synced with the remote repository on Github.
7. git clone
Usage: git clone [URL]
Suppose, we want to work on a file that is on a remote Github repository as another
developer. How can we do that? We can work on this file by clicking on Clone
or Download and copying the link and pasting it on the terminal with the git clone
command. This will import the files of a project from the remote repository to our
local system.
(1) The below screenshot shows from where to copy the link:
To create a local folder, we have to use the following command:
mkdir [directory- name]
cd [directory- name]
git clone [URL]
Now, paste the copied link along with the git clone command as shown below:
Note: Here, we don’t have to use the git remote add origin command because we
have already cloned the remote repository in the local directory. Now, if we push any
new file, it knows where it has to go.
8. git branch
Usage (i): git branch [name-of-the-branch]
When multiple developers are collaborating on a project or repository, branches
become essential for managing different workspaces. Using this command, we can
create a new branch (for example, ‘branch1’). This allows developers to work
independently on their respective branches, making changes and commits without
affecting the main branch or other branches.
Usage (ii): git branch -D [name-of-the-branch]
Likewise, to delete a branch, we utilize the “git branch -D” command. This enables
us to remove a specific branch (e.g., ‘name-of-the-branch’) that is no longer needed,
cleaning up the repository and reducing clutter.
9. git checkout
Usage (i): git checkout [name-of-the-new-branch]
This command allows us to switch to an existing branch within our repository. It
facilitates navigating to the desired branch, enabling us to add new files, make
changes, and commit those files within that specific branch.
In situations where we want to keep our repository up to date but are concerned that
updating our files might lead to issues, a specific technique comes to the rescue. To
integrate the commits into our master branch, we use the merge feature. This feature
actively retrieves all the branches from the repository and then proceeds to
download all the required commits and files from another repository. It ensures that
our repository remains current while mitigating the risk of potentially breaking our
ongoing work.
17. git reset
Usage: git reset –hard [SOME-COMMIT]
We use this command to return the entire working tree to the last committed state.
This will discard commits in a private branch or throw away the uncommitted
changes!
Here, we have executed a ‘hard reset’ using the –hard option. Git displays the
output indicating that the HEAD is pointing to the latest commit. Now, when we
check the state of the repo with git status, Git will indicate that there are no pending
changes (if any prior addition of a new file or modification of an existing file is done
before using the ‘git reset –hard’ command). Our modifications to an existing file, if
not committed, and the addition of a new file, if not staged, will be destroyed. It is
critical to take note that this data loss cannot be undone.
If we do git reset –hard [SOME-COMMIT], then Git will:
Make our current branch (typically master) back to point <SOME-COMMIT>
Make the files in our working tree and the index (“staging area”) the same as the
versions committed at <SOME-COMMIT>
18. git pull
Usage: git pull origin master