Handful git commands

I’m going to list out few helpful git commands here which might be very handy specially when you’re a noob like me with a thurst to learn a lot 😛

Few golden rules while you’re working with git.

#1. Always pull from remote repository before you push your files to remote.
#2. Use git status to see status of your local repo after/before pull/push.
#3. Generally we work on development branch rather than master branch. TL/PM should merge the files from development to master branch after reviewing them.
#3. 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.

Registration

Register your identity in your local system using:

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 it

cd /path/to/my/directory
git init

Git Add and commit

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

git add <file_name>

Add all modifiled files to git using

git add . 
Or 
git add -A

Commit files using

git commit -m “commit message”

Note: Check the status using git status.

Git pull

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

Git push

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 back to previus commits, use command git log to see log history and get commit id from it and afterwards reset it using

git reset <commit_id> 

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

Branching

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

Use following commands to show list of branches

git branch 
or 
git branch -a

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

git fetch && checkout <branch_name>

Merging branches

git checkout <base_branch>
git merge <branch_to_merge>

Stashing/Drafting your changes

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

git stash

Use following command if you don’t want to keep changes or delete them:

git stash drop 

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

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

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s