DEV Community

Cover image for Important Git Commands: Every Programmer Should Know
Aadarsh Nagrath
Aadarsh Nagrath

Posted on • Edited on

Important Git Commands: Every Programmer Should Know

Introduction:

Git, a distributed version control system, has become an indispensable tool for developers worldwide. Whether you're working solo or in a team, understanding Git and its essential commands is crucial for efficient project management and collaboration. In this article, we'll explore the fundamental Git commands categorized for easy reference, covering everything from initializing a repository to sharing and updating projects.

Getting & Creating Projects:

  • git init: Initialize a local Git repository.
  • git clone [repo_url]: Clone a public repository.
  • git clone ssh://git@github.com/[username]/[repository-name].git: Clone a private repository.

Basic Snapshotting:

  • git status: Check the status of your working directory and staging area.
  • git add [file-name]: Add a file to the staging area.
  • git add -A: Add all new and changed files to the staging area.
  • git commit -m "[commit message]": Commit changes with a descriptive message.
  • git rm -r [file-name]: Remove a file or folder from the working directory and staging area.

Branching & Merging:

  • git branch: List local branches (the asterisk denotes the current branch).
  • git branch -a: List all branches (local and remote).
  • git branch [branch name]: Create a new branch.
  • git branch -d [branch name]: Delete a branch.
  • git branch -D [branch name]: Delete a branch forcefully.
  • git push origin --delete [branch name]: Delete a remote branch.
  • git checkout -b [branch name]: Create a new branch and switch to it.
  • git checkout [branch name]: Switch to a branch.
  • git merge [branch name]: Merge a branch into the active branch.

Sharing & Updating Projects:

  • git push origin [branch name]: Push a branch to your remote repository.
  • git push -u origin [branch name]: Push changes to the remote repository and set upstream tracking.
  • git push: Push changes to the remote repository (remembered branch).
  • git pull: Update the local repository to the newest commit from the remote repository.
  • git remote add origin [repo_url]: Add a remote repository.
  • git remote set-url origin [repo_url]: Set a repository's origin branch to SSH.

Inspection & Comparison:

  • git log: View the commit history.
  • git log --summary: View detailed changes in the commit history.
  • git log --oneline: View brief commit history.
  • git diff [source branch] [target branch]: Preview changes before merging branches.

Deleting a Commit -

  • git revert [commit id]: Revert changes introduced by a specific commit. Steps:
  • Identify the commit you want to delete by checking the commit history using git log.
  • Note the commit ID (a unique alphanumeric code associated with each commit).
  • Use git revert [commit id] followed by the commit ID to revert the changes introduced by that commit.
  • Git will create a new commit that undoes the changes made by the specified commit, effectively deleting its effects from the project history.
  • Push the changes to the remote repository to synchronize the changes and update the project history for collaborators.

Also -

If you have not yet pushed the commit anywhere, you can use git rebase -i to remove that commit. First, find out how far back that commit is (approximately). Then do:

git rebase -i HEAD~N
The ~N means rebase the last N commits (N must be a number, for example, HEAD~10). Then, you can edit the file that Git presents to you to delete the offending commit. On saving that file, Git will then rewrite all the following commits as if the one you deleted didn't exist.

Or, you could look at the output of the git log, find the commit id of the commit you want to back up to, and then do this:

git reset --hard <sha1-commit-id>
If you already pushed it, you will need to do a force push to get rid of it...

git push origin HEAD --force
However, if others may have pulled it, then you would be better off starting a new branch. Because when they pull, it will just merge it into their work, and you will get it pushed back up again.

If you already pushed, it may be better to use git revert, to create a "mirror image" commit that will undo the changes. However, both commits will be in the log.

Connect on - Twitter Handle

Comment more commands will update

Top comments (0)