Git Commands every junior developer should know…

Mrudula
MindOrks
Published in
5 min readJan 2, 2020
Photo by Christopher Gower on Unsplash

I'd you are a software developer you know how important it is to use version control. Git is an open-source version control system which is similar to the version control systems like CVS and Subversion. It is used to keep track of project files.

Git stores information about the project’s progress in a repository. A repository is having commits to the project or a set of references to the commits called heads. Git allows developers to easily collaborate, as they can fetch the newest version of the project file, make changes and commit them.

There are a lot of resources on the internet to learn git but if you are just starting to learn git then this article will help you to understand the basic commands that we use in git.

1. init — initialize a git repository for an existing or new project

The git init command is used for creating a new repository. it can be used to convert the current directory into a git repository

git init <directory name>

2. clone — To copy a git repository from a remote source.

git clone is a Git command-line utility which is used to target an existing repository and create a clone, or copy of the target repository

git clone <repository url>

3. add — marks changes to be included in the next commit

The “addcommand marks changes to be included in the next commit. It adds changes to Git’s “Staging Area”, the contents of which can then be wrapped up in a new revision with the “git commit” command

git add [file-name.txt] - Add a file to the staging areagit add -A - stages all changesgit add .  - stages new files and modifications, without deletionsgit add -u - stages modifications and deletions, without new files

4. commit — To commit your local changes and sets it to a new commit

The git commit command captures a snapshot of the project's currently staged changes.

git commit -a”commit msg” - 

5. status — Check the status of files you have changed in your working directory

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git. The status output does not show you any information regarding the committed project history.

git status

6. log

After you have created several commits, or if you have cloned a repository with an existing commit history, you’ll probably want to look back to see what has happened. The most basic and powerful tool to do this is the git log command.

git loggit log -n <limit>git log --oneline (condense the log message to a single line)git log --stat (logs the messages with the number of lines added and deleted)git log -p (Displays the patch representing the each commit, shows the full difference)git log <file_name> (Displays the commits regarding the particular file)git log --author = “xyz” -p <file_name> (Shows all the changes made by user xyz to the particular file)

7. stash

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

git stash list [<options>]git stash show [<options>] [<stash>]git stash drop [-q|--quiet] [<stash>]git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]git stash branch <branchname> [<stash>]git stash cleargit stash create [<message>]git stash store [-m|--message <message>] [-q|--quiet] <commit>

8. branch

Lists all the branches on the local machine

git branch -List branches (the asterisk denotes the current branch)git branch -aList all branches (local and remote)git branch [branch name]Create a new branchgit branch -d [branch name]Delete a branchgit push origin --delete [branch name]Delete a remote branchgit checkout -b [branch name]Create a new branch and switch to itgit checkout -b [branch name] origin/[branch name]Clone a remote branch and switch to itgit branch -m [old branch name] [new branch name]Rename a local branch

9. checkout

The git checkout command lets you navigate between the branches created by the git branch. Also to reset a file by checking out.

git checkout -Switch to the branch last checked outgit checkout -- [file-name.txt]Discard changes to a file

10. merge

git-merge joins two or more development histories together. It combines all the integrated changes into a single commit, instead of preserving them as individual commits.

git merge [branch name]Merge a branch into the active branchgit merge [source branch] [target branch]Merge a branch into a target branch

11. reset

git-reset resets the current HEAD to the specified state.

git reset <file_name>git reset (resets the staging area to the most recent commit  but leave the working directory unchanged)git reset --hard (Reset the staging area and the working directory to match the most recent commit)git reset <commit> (Move the current branch tip backward to <commit>, reset the staging area to match, but leave the working directory alone)

12. push — to push the local changes to the remote

The git push command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo.

git push origin [branch name] Push a branch to your remote repositorygit push -u origin [branch name] Push changes to remote repository (and remember the branch)git push Push changes to remote repository (remembered branch)git push origin --delete [branch name] Delete a remote branch

13. pull

The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content. Merging remote upstream changes into your local repository is a common task in Git-based collaboration workflows.

git pullUpdate local repository to the newest commitgit pull origin [branch name]Pull changes from remote repository

14. rm

git rm is used to remove a file from a Git repository. It is a convenience method that combines the effect of the default shell rm command with git add.

git rm [file name]

15. show

This command displays the metadata and content changes of the specified commit.

git show [commit id]

GitHub Isn’t Just for Developers

Although git is mainly designed for developers, you can use it to host a variety of files. To store JSON stubs or any other files, one can use gist. Also If you have a team that is constantly updating a word document, you could use GitHub as your version control system.

--

--

Mrudula
MindOrks

#developer #writingcode #android #java #kotlin