Useful git commands

Here's the list of some of the useful commands of Git.

  • Git clone : Download existing source code from a remote repository. It makes an identical copy of the latest version of a project and saves it locally.
git clone <https://name-of-the-repository-link> 
  • Git log: To print a history of commits. Tip: if you use only git log, then press q or z, to exit out of that command in case there are a lot of commits to go through.
   git log --oneline 
  • Git remote: To check the remote URL that has been added. It shows where your code goes, and which git remote alias you should use in order to push code.
git remote -v
  • Amending commit : This command is useful in case you want to add some more changes that you may have accidently left in your previous commit message. Rather than making another commit, you an just amend your changes.
git commit--amend -m "commit message"
  • Git status : This command shows what files have been changed, and whether it is ready to commit or not.
git status
  • Git stash : Sometimes you urgently want to switch to another branch. In your current branch, you have some edited files but you don't want to commit those yet, so you can just stash them so you can start where you left.
git stash
git stash list - to check lists of stash
git stash apply <stashId>- to get all stashed file
  • create new branch : If you want to create a new branch you can create using this commands. Flag -b indicates new branch. Without -b flag, it will switch to the branch name you provided if branch name exists.
git branch <Your branch name>
or
git checkout -b <your branch name>
            When you run above command, you are in a specific named branch.
  •     You can check if you are connected to your account using SSH or not using this command
                                                        ssh -T git@github.com
            Output should be likeHi (your username)! You've successfully authenticated, but Github does not provide shell accesss.                                                                                 
  • To copy your local ssh key from command line
                                     pbcopy < ~/.ssh/<public_key_name>.pub 
    after that you should be able to paste copied public key to your online github account. 

  • To restore deleted file that is not yet committed 
                                    git checkout HEAD file/path/filename 
  • Rename a branch name
                                    git branch -m old-branch new-branch  (If not changing the current  branch )
                                    git branch -m new-branch-name (If changing current branch name) 
  •  Delete a branch (locally)
                                    git branch -delete old-branch
                                    git branch -d old-branch

These are some of the useful commands that one should know. You can also download pdf of most commonly used git commands here

If you think you have a recommended command that is very useful to you, feel free to comment. Ill keep updating this list as I come across commands.

          Post a Comment

          0 Comments