Common Git Commands

Common Git Commands

Git is an important part of daily programming (especially if you’re working with a team) and is widely used in the software industry.

1. Set Up Git Credentials like: Username and Email

The username is required to link commits with your name. It is not the same as your GitHub account username, which you use to login into your GitHub profile. You can set or update your username by using the git config command. The new name will automatically reflect in any future commits that you push to GitHub from the command line. If you want to hide your real name, you can use any arbitrary text as your Git username.

git config --global user.name "SkillsHats"

You can also use the git config command to update the email address that you want to associate with your Git commits. The new email address will automatically reflect in any future commits that you push to GitHub from the command line.

git config --global user.email "hello@skillshats.com"

Note: --global argument will setup crendentials globally for all the git projects, but if you want to configure different username and email address for each git project, then remove the --global argument.

Set up Git Credentials for project specific.

git config user.email "hello@skillshats.com"

2. Cache Your Login Credentials

If you want to avoid re-typing the username and password every time you perform a commit.

git config --global credential.helper cache

3. Initialize a Repository

You can create an empty Git repository or reinitialize an existing one by using the init parameter. It will create a hidden directory knows as .git upon initialization.

git init

4. Clone the repository

Git clone is used for downloading existing source code from a remote repository (like Github, Gitlab).

git clone <https://name-of-the-repository-link>

Example:

git clone https://github.com/SkillsHats/<repository_name>.git

5. Add Individual File or All Files To Staging Area

To add a single file to the staging area by using the add parameter and the name of the file.

git add python.py

To add all files and directories to the staging area by providing the wildcard . instead of the file name.

git add .

6. Check a Repository Status

You can view the status of the current repository by using the status keyword, which includes the staged, unstaged, and untracked files.

git status

Git Status command show the information like:

  • Whether the current branch is up to date
  • Whether there is anything to commit, push or pull
  • Whether there are files staged, unstaged or untracked
  • Whether there are files created, modified or deleted

7. Commit Changes with a *Single Line Message

You can add a single line message while making a commit in your repository by using the commit parameter, and -m flag.

git commit -m "Single comment about the changes in the file(s)"

8. View Commit History With Changes

You can view the changes made in your repository by using the log parameter. It will show a list of the newest commits in sequential order. You can also check the detailed changes of each file by adding the -p flag.

git log -p

9. View a particular git commit using git show command.

To see the detailed changes of a specific commit by using the show parameter and providing the ID or hash of the commit. This hash is unique for each commit made in your repository.

git show 0d8da876c99bca2d42cbea36b1deaa5008f4f3ab

You can also provide a short hash to get the same result. Mostly first 4-5 character are enough.

git show 0d8da8

10. View Changes Before Committing

You can view the list of changes made to your repository by using the diff parameter. It will only show the unstaged changes by default.

git diff

If you want to view the staged changes, you can add the --staged flag.

git -diff --staged

You can also provide a filename as a parameter to only view the changes of a specific file.

git diff python.py

11. Remove Tracked Files From The Current Working Tree

You can remove files from the current working tree by using the rm parameter. This action will also remove the files from the index.

git rm dirname/python.py

You can also provide file globs (e.g., *.html) to remove all matching files.

git rm dirname/*.html

12. Rename Files

You can rename a file or directory by using the mv parameter. This parameter requires a <source> and a <destination>. The source must exist and can be a file, or directory, and the destination must be an existing directory.

git mv dir1/python.py dir2

Executing the above command will move the source(s) into the target directory. The index will get updated, but you have to commit the changes.

13. Revert Unstaged and Staged Changes

You can restore the unstaged working tree files by using the checkout parameter.
You need to provide a file path to update it. If the file path is not set, then git checkout will update the HEAD to set the specified branch as the current branch.

git checkout python.py

To restore a staged working tree file, you can use the reset parameter

git reset HEAD python.py

If you want to unstage all staged files, then do not provide the file path.

git reset HEAD

14. Amend The Most Recent Commit

To make changes to the most recent commit by using the commit parameter with the --amend flag.
For instance, you just committed some files, and you remember that you have made a mistake in your commit message. In such a scenario, you can execute that command to edit the previous commit’s message without modifying its snapshot.

git commit --amend -m "Updated message for the previous commit"

It is also possible to make changes to the previously committed files.
For instance, you have updated some files in multiple folders that you want to commit in a single snapshot, but then you forgot to add one folder to commit. Fixing this sort of error is just a matter of staging the other files or folders, and committing with the --amend and --no-edit flags.

git add file1
git commit

# Here you forgot to add file2 to commit, you can execute the following command to amend the other files and folders.

git add file2
git commit --amend --no-edit

The --no-edit flag will allow you to make the correction to your commit without altering its commit message.

15. Rollback Last Commit

To roll back the last commit by using the revert parameter. This will create a new commit, an inverse of the previous commit, and add it to the current branch history.

git revert HEAD

Revert v/s Reset

The git revert command only undoes a single commit. It does not move back to the previous state of a project by removing all succeeding commits, which is done when git reset is used.
The git revert is a much better and safer way to undo the changes.

16. Rollback a Particular Commit

To roll back to a particular commit by using revert parameter and the commit ID. It will create a new commit, a copy of the provided commit ID, and add it to the current branch history.

git revert 0d8da8

17. Create and Switch To a New Branch

To create a new branch by using the branch parameter and the name of the branch.

git branch new_branch_name

But git won’t switch to it automatically. If you want git to auto-switch to the new branch, you have to pass the -b flag and the checkout parameter.

git checkout -b new_branch_name

18. List All Branches

To view the list of all branches by using the branch parameter. It will display all branches and mark the current branch with an asterisk (*) sign and highlight it.

git branch

To list all remote branches by using the -a flag.

git branch -a

19. Delete a Branch

To delete a branch by using the branch parameter, -d flag and the name of the branch. If you’ve completed working on a branch and have merged it into the main branch, you can delete the branch without losing any history. However, if the branch hasn’t been merged, the delete command will output an error message.

git branch -d existing_branch_name

To forcibly delete a branch, need to use the capital -D flag. This will delete the branch despite its current status and without any warning.

git branch -D existing_branch_name

The above commands will only delete a local copy of the branch. The branch may exist in the remote repository. If need to delete a remote branch, then origin --delete parameter with push command.

git push origin --delete existing_branch_name

20. Merge Two Branches

To merge two branches by using the merge parameter and the name of the branch. This will combine the specified branch into the main branch.

git merge existing_branch_name

If you need a merge commit, you can execute git merge with the --no-ff flag.

git merge --no-ff existing_branch_name

21. Show Commit Log as Graph for Current or All Branches

To view the commit log as a graph for the current branch by using the log parameter and --graph --oneline --decorate flags.
The --graph option will draw an ASCII graph, which represents the branch structure of the commit history. When it used in association with the --oneline and --decorate flags, it makes it easier to identify which commit belongs to which branch.

git log --graph --oneline --decorate

To see the commit log for all branches, you can use the --all flag.

git log --all --graph --oneline --decorate

22. Abort a Conflicting Merge

To abort a conflicting merge by using the merge parameter and the --abort flag. It allows you to exit from the merge process and return to the state after which the merge began.

git merge --abort

Also use the reset parameter to during a merge conflict to reset the conflicted files to a stable state.

git reset

23. Add a Remote Repository

To add a remote repository by using the remote add parameter, <shortname> and the <url> of the remote repository.

git remote add skillshats https://github.com/SkillsHats.git

24. View Remote URLs

To view the remote URLs by using the remote parameter and -v flag.

git remote -v

25. Get Additional Information About a Remote Repository

To get detailed information about a remote repository by using the remote show parameter and the name of the remote like origin.

git remote show origin

The above command will output a list of branches that are associated with the remote and also the endpoints that are connected to fetch and push files.

26. Push Changes To a Remote Repository

To push changes to a remote repository by using the push parameter, name of the repository, and the name of the branch.

git push origin branch_name

Example:

git push origin main

27. Pull Changes From a Remote Repository

To pull changes from a remote repository by using the pull parameter. This will fetch the specified remote’s copy of the current branch and instantly merge it into the local copy.

git pull

To view the details of the files that have been downloaded, by using the --verbose flag.

git pull --verbose

To pull changes from a specifi branch by using the pull parameter, you can also provide the branch name.

git pull origin branch_name

Example:

git pull origin main

28. Merge Remote Repository With Local Repository

To merge a remote repository with your local repository by using the merge parameter and the name of the remote.

git merge origin

29. Push a New Branch To Remote Repository

To push a new branch to a remote repository by using the push parameter, -u flag, the name of the remote, and the name of the branch.

git push -u origin <new_branch>

OR

git push --set-upstream <remote> <new_branch>

30. Remove a Remote Branch

To remove a remote branch by using the push parameter, --delete flag, the name of the remote, and the name of the branch.

git push --delete origin existing_branch

31. Use Rebase

Rebasing is a process to combine or move a sequence of commits to a new base commit. The rebase parameter and the name of the branch.

git rebase branch_name

The command will change the base of your branch from one commit to another, which will make it appear as if you have created your branch from a different commit.

Explore More Git Posts

  • Git
  • 4 min read
GitFlow: A Branching Model for Large Projects

Learn how GitFlow can help manage large projects with multiple developers through clear and consistent process of developing and releasing software.

Read More
  • Git
  • 2 min read
Git vs Github: Understanding the Differences

Confused about the difference between Git and Github? Key differences and explains how they are used in version control and code collaboration.

Read More