Skip to content

Instantly share code, notes, and snippets.

@LuisQBlanco
Last active May 19, 2020 19:30
Show Gist options
  • Save LuisQBlanco/66c56067b0a0efcb917acaa6535f8bd0 to your computer and use it in GitHub Desktop.
Save LuisQBlanco/66c56067b0a0efcb917acaa6535f8bd0 to your computer and use it in GitHub Desktop.
Some git comon git commands

GIT Commands

Git Branch

View Branch

To view the branches in a git repository

    git branch

To view both remote -trancking branches and local branches, run the command:

    git branch -a

To see more information use -v, -vv or --verbose

    git branch -a -vv

Checkout a Branch

To checkout an existing branch

    git checkout <branch-name>

Note that this command only creates the new branch. You’ll need to run git checkout NEW-BRANCH-NAME to switch to it.

There’s a shortcut to create and checkout a new branch at once. You can pass the -b option (for branch) with git checkout . The following commands do the same thing:

    # Two-step method
    git branch NEW-BRANCH-NAME
    git checkout NEW-BRANCH-NAME

    # Shortcut
    git checkout -b NEW-BRANCH-NAME

When you create a new branch, it will include all commits from the parent branch. The parent branch is the branch you’re on when you create the new branch.

Rename a Branch

To rename a branch

    git branch -m OLD-BRANCH-NAME NEW-BRANCH-NAME

Alternative

    git branch --move OLD-BRANCH-NAME NEW-BRANCH-NAME

Delete a Branch

Git won’t let you delete a branch that you’re currently on. You first need to checkout a different branch, then run the command:

    git branch -d BRANCH-TO-DELETE

    # Alternative:

    git branch --delete BRANCH-TO-DELETE

The branch that you switch to makes a difference. Git will throw an error if the changes in the branch you’re trying to delete are not fully merged into the current branch. You can override this and force Git to delete the branch with the -D option (note the capital letter) or using the --force option with -d or --delete

    git branch -D BRANCH-TO-DELETE

    # Alternatives

    git branch -d --force BRANCH-TO-DELETE
    git branch --delete --force BRANCH-TO-DELETE

Compare Branches

You can compare branches with the git diff command:

    git diff FIRST-BRANCH..SECOND-BRANCH

You’ll see colored output for the changes between branches. For all lines that have changed, the SECOND-BRANCH version will be a green line starting with a “+”, and the FIRST-BRANCH version will be a red line starting with a “-”. If you don’t want Git to display two lines for each change, you can use the --color-words option. Instead, Git will show one line with deleted text in red, and added text in green.

If you want to see a list of all the branches that are completely merged into your current branch (in other words, your current branch includes all the changes of the other branches that are listed), run the command

    git branch --merged .

Update a Branch from Remote

To update a local branch from remote:

    git stash (optional, to save local changes which differs from the remote repository if any) 

If you weren’t already on the branch you want to work on:

    git checkout my_local_branch 

Finally pull from the remote branch

    git pull

Track a Remote Branch

If you already have a branch and you want to track a remote branch, then you use set-upstream-to command:

    git branch --set-upstream-to origin/BRANCH

Or you can use the -u flag (upstream) when you make your first push:

    git push -u origin BRANCH

Help with Git Branch If you forget how to use an option, or want to explore other functionality around the git branch command, you can run any of these commands:

    git help branch
    git branch --help
    man git-branch

Link

Start New Project form GitHub

GIT Commands

Created new project in GitHub

  • Create new repository

    • Add Repository Name
    • Description
    • Select if will be Public or Private
    • Add the file Readme when you select Initialize this repository with a README
    • Add the file .gitignore for python project
    • Add the license MIT
    • push Create repository
    • Copy remote reposity URL
  • Go to projects folder in your local machine and run

    git clone <remote repository URL>

Start New Project form folder

GIT Commands

New repositiry from local folder

  • Create a directory to contain the project
  • Go into the new directory.
  • Run
      git init
    
  • Create some file and write some code. like readme.md
  • Run
    git add .

Or

    git add --all
  • Commit the files that were created in your local repository.
    git commit -m 'First commit'
  • Create a new repository in the GitHub website, copy the remote reposity URL, go to the terinal and run
    git remote add origin <remote repository URL>
  • Sets the new remote repository in your local machine
    git remote -v
  • Push the changes in your local repository to GitHub
    git push origin master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment