Skip to main content

Git Commonly used commands

Updated 7 min readMarkdown
Git Commonly used commands

After setup / First Install

Using the setup option for various platforms we can install git on our system. We would be able to create local repo and track our files after this.

config --global is used for configuring user information across all the repositories

Setup & Init

Here, we go to GitHub login to our account fork the repo of other user, using <>Codebutton we copy the url. After that on our local repo we initialize the empty repo and clone the remote repo to our local repo. Git would be able to track or untrack files after this.

Stage and Snapshot

There are three type of storage area in git

  1. Working Dir (local area): In this area, files on our local file system are created, edited or deleted.

  2. Staging area: In this area, changes before making a commit are visible. All the tracked file are in the staging area.

  3. Repository (remote area): In this area, after we use git commit and git push files get accessible on github.com and other users can contribute if repo access is public. All the metadata of git file and history of changes comes in this area.

There are some commands which we need to keep handy in order to get benefit from git.

Git commanduse of the command
git statusshows files which are being tracked and which are not
git add [file]adds file to track the git changes
git reset [file]remove the file from traking changes (staging)
git diffchanges between file when not tracked(unstage)
git diff --stagedchanges between files staged but not committed
git commit -m "<message>"when commiting a message is necessary

Git status shows the untracked and tracked changes

After performing git reset <filename> the file moves to untracked list from tracked list

git diff shows changes in untracked files while git diff --staged shows changes in tracked file.

git commit -m <message> generates hash id and adds the change to index. Here git lo is alias for git log --oneline HEAD points to latest commit id in master branch.

Branch and Merge

From master branch we can make a its sub-branch and give it to the other person to work on any feature. According to branching & merging commands we can integrate the branches. Some common commands are

Git commandUse Case
git branchlist the branches * appears on current branch
git branch <branch-name>a new branch is created with provided branch name
git checkoutswitches to another branch and check it out into working dir
git merge <branch>merges the branch into current
git logshows hash id, date, commit message and author

Inspect and Compare

One of the feature of git is to track changes and perform useful operations related to these changes. Some of the commands related to this is shown below

Git CommandUse Case
git log --onelineshows the log in single line
git log branchA..branchBshows commit on branchA not on branchB
git log --follow <file>shows the commit that changed file, rename included
git diff branchB...branchAshow the diff of what is branchA not in branchB
git show [SHA]show object in Git in human-readable format

git log are shown in single line with commit id and branch, HEAD and message

Author, Date, Hashid (commit id) , also master and dev branch are displayed seperately

There is a file version01.txt and using git log --follow the all the commits made to that particular file are being shown.

Tracking Path Changes

The process of assigning hash-id (commit id) and keep track of file modifcation is called versioning. There are certain commands that help perform versioning in easier way.

Git commandUse Case
git rm <file>deletes the file from the project and stage (track) removal for commit
git mv <existing-path> <new-path>to track file moved within the project
git log --stat -Mto list all commits that have moved path

git rm <filename> deletes file and git status which tracks git repo shows the file that has been deleted

git stash undo all the uncommited changes , file is recovered git mv moves fiel from one location to another. git status again shows that file has been moved.

Ignoring Patterns

Sometimes we don't want our private data or large files to be kept on remote repository, for that git provides .gitignore file which helps to avoid storing specific file or files with certain pattern using wildcards. Some of the commonly used commands that ignore patterns are listed below.

Git commandUse Case
vim .gitignore
logs/
*.notes
pattern*/These patterns are inside .gitignore file, whenever these changes are commited git ignore these changes.
git config --global core.excludesfile <filename>This command is used to define location of file that contains list of all the files/wildcards that has to be ignored

First of all we created a .gitignore file and added a list of wildcards/file that git version control system need not track.

Then we told the git about location of .gitignore file using git config --global core.excludesfile <filename> After that we added feat1.txt and dev_file and on checking status we noticed that file with .txt extension do not get added while the other one was added to staging area.

Share and update

The git has some commands that are helpful for synchronisation between remote and local repository. These commands help in open source contribution, sharing code, or making changes in other person code.

Git commandUse Case
git remote add [alias] [url]add git url as alias
git fetchfetches down all branches from git remote
git merge [alias]/[branch]merge remote branch into current branch
git push [alias] [branch]transmit local branch commit to the remote repo
git pullfetch and merge any commit from tracking remote branch

The other developer created a branch remote_branch and pushed to git remote repo. Using git fetch our developer fetched the branch on local repo.

Using git merge origin/remote_branch our developer was able to merge remote branch with current branch.

git fetch has bought all the branches across the repo (remote/local) of multiple developers working on the project

it would push changes from dev branch to origin alias of remote repo as declared in previous command.

master branch would pull changes from origin/master remote repository

Rewrite History

Git has unique feature of retrieving deleted code /deleted feature that we wanted after removal. Because Git mantains a commit id which helps it to reset changes made.

Git Command

Use Case

git rebase <branch>

apply any commit of current branch ahead of <branch> specified

git reset --hard <commit>

clears staging area, rewrite working tree from specified commit

git log shows that test branch is ahead of dev branch by two commits.

now, we checkout to dev branch and apply git rebase to test from dev branch. As evident from git log (alias: git lo) both branches point to same head.

we also see git reset in action. It has cleared particular commit using commit id and bought it to untracked state. --hard option deletes file if present after particular commit id mentioned.

Temporary Commits

Git has a feature to stash save staged (tracked changes). It has certain command to facilitate this feature.

Git CommandUse Case
git stashsave staged or tracked changes
git stash listlist of tracked or staged changes
git stash popinserts topmost change on the list to current repo
git stash dropdiscards the changes from top of stash list

git stash in action. First we see untracked files with git status then we git stash to save our changes. After that we see the list of changes made using git stash list

git stash pop in action. After we git stash from dev branch we checked out to test branch where we used git stash pop to pop out topmost change in our stash list.

git stash drop in action. We see 3 items in stash list and we used stash drop then only 2 itmes were remaining in our stash list.

References

https://education.github.com/git-cheat-sheet-education.pdf

https://www.trainwithshubham.com/

About the author

  • On HashNode I would be sharing my understanding of the topic. My goal is to create helpful content for the community in making tech easy to learn. My audience can find practical and hands on about any topic related to IT infra including, DevOps and cloud.

No comments yet. Be the first to comment.