Useful git commands

Useful git commands

I've compiled a list of helpful Git commands that I found useful as a beginner. These commands can come in handy while you're learning Git and trying to improve your workflow.

Few golden rules while you’re working with git.

  1. Always pull from the remote repository before you push your files to the remote.

  2. Use git status to see the status of your local repo after/before pull/push.

  3. Generally, we work on the development branch rather than the master branch. TL/PM should merge the files from the development to the master branch after reviewing them. You can create a branch if you’re working on a new module from scratch.

  4. You can draft (called stash) your changes in your local repo using git stash commands if you don’t want to push the changes now.

Configure git user in your system

Register your identity in your local system using the below commands

git config --global user.name "user_name"
git config --global user.email "user_email"

Git initialization

If you have created a new directory and want to initiate git in the newly created directory, run below commands

cd /path/to/my/directory
git init

Branching in Git

When you initiate git in a new directory using git init, automatically master branch will get created. Though if you want to create a new branch you can use git checkout -b to create a new branch and switch to the newly created branch.

Use the following commands to show a list of branches

git branch 
or 
git branch -a

If there is a branch with the name branch_name on the remote repo but not in your local repo. Use this command to update your local repo and switch to a new branch:

git fetch && checkout <branch_name>Git pull command

Run the below command to pull code differences from a branch name and merge them in your code

git pull origin <branch_name>
Ex: git pull origin development

Git Add and commit

If you’ve modified some files in your local repository, you need to add them to git before pushing them to the remote repo. Add a file to git using

git add <file_name>

Add all modified files to git using

git add . 
Or 
git add -A

Commit files using

git commit -m "commit message"

Check the status using

git status

Git push command

Run the below command to push your branch to the remote repository

git push origin <branch_name>
Ex: git push origin development

Fixing git conflicts:

Check logs

If you see there are pull/push/merge conflicts and want to revert to previous commits, use the command git log to see log history and get the commit id from it and afterward reset it using

git reset <commit_id>

It will update your repo with the code to the version when commit_id was committed.

Merging branches

If you want to merge code from another branch to your branch, you can use the following commands

git checkout <base_branch>
git merge <branch_to_merge>

Above command will merge the code from base_branch to branch_to_merge .

Stashing/Drafting your changes

Use the following command to stash/draft your changes in the local repo.

git stash

Use the following command if you don’t want to keep stashed changes and you want to delete them:

git stash drop

If you’ve modified any local file, then added that file to gitignore, still you’ll see that file will be present in the index (when running the command: git status). You can clear your screen using the below command

git update-index --assume-unchanged <file_name>

I’ll keep extending this list based on your suggestions and my experiences.