DEV Community

May Do
May Do

Posted on

Basic Git Commands

Git is the most popular version control system that is designed to manage changes in source codes during development. It is a very powerful tool to use to preserve progress especially when working with teams. However, Git has a steep learning curve. Learning to use Git will take some time so to help get started, here are some Git commands you may want to remember.

git status

git status: shows the state of the working directory and staging area. Use git status to see an overview of all the files that you've made changes in. It should show the files that are currently staged and unstaged, and files not tracked by git.

git add <filename>

git add: add changes in working directory to staging area. Use git add when you want to save the changes in the file that you specify. This will not save the file, but it will prepare the file to be saved. You can can multiple files by attaching the names of each file like git add <filename> <filename> <filename> or use git add . to add all files. If you change your mind about adding a file, you can use git reset <filename>.

git commit

git commit: records the changes added in staging area to repository. To avoid the prompted text editor, attach '-m' to create the commit message like this: git commit -m 'this is my message'. Git commit will save those changes that you added with git add. If you want to undo last commit, use git revert <commit hash> you can access the hash using git log. Git revert will create a new commit with the changes on the specified commit.

git push <remote> <branch>

git push: update the repository with latest changes committed. Git push will transfer the commits that you've made on the branch that you specify to a remote repository. After pushing, there should be either a pull request or an updated repository with the committed changes.

git pull <remote> <branch>

git pull: grabs and merges the contents from a remote repository to the local branch. Git pull is a combination of git fetch and git merge. Git fetch will download and the contents from the remote repository and git merge will try to stitch the remote and local contents together. Merging will prompt a merge conflict if there is any competing lines in a file. An alternative to git merge is rebase. git rebase will add you commits on top of the fetched contents instead of stitching it together.

Other commands
If you ever want to reword the last commit message, use git commit --amend or git commit --amend --no-edit if you just want to add a small change without changing the commit message.

If you change your mind and want to get rid of all your local changes at once, use git checkout .

If you want to change the name of the branch that your working on, use git branch -m <old name> <new name>

If you want to see all the commits that you've made, use git log.

For other commands not covered here, using git --help from your terminal or visit git-scm to checkout more commands and see what they could do. There are so many commands in Git to learn that it could be quite intimidating, but hopefully these will help you get started on mastering this tool.

Top comments (1)

Collapse
 
webdeasy profile image
webdeasy.de

Great summary! Maybe you can add a FAQ to the article and name and explain more git functions. :)