· Alvaro Barber · Tutorials  · 4 min read

Git Branches

Use Git and Git branches at work

Use Git and Git branches at work

Here you have the commands that you need to configure your workspace and start working with other developers.

Basic Commands

Using the cmd, go to the location or folder in your local PC where you want to import the files and
clone the repo to your local folder with (example):

git clone https://github.com/abarbercaselles/SQL-Snowflake.git

From now on, if no authentication is involved , you are able to work on this repo with the cmd and with cmd terminal in Visual Studio or other tool.
As a test, apply or modify some changes the files that you cloned in order to know which files were modified.

git status

If you want to add the changes to the staged area.\

git add .

Add only one file to the staged area

git add file1.txt

Now you moved your changes to the staged area. To unstage use:

git restore --staged file1.txt

However, if you want to continue, commit your changes with

git commit -m "I am committing with change in file1.txt"

Be sure to check the cheatsheet for more combinations , but this is basic commit with a message.
If you are ready to push your commit from your local repository in your PC to the repo in git

git push

What if someone made some changes in the git repository and you want that the code in your local PC gets updated?

git pull

Branches

If there is no other branch, you will be in the master branch.
Be sure to check if this is true, executing:
Go to master branch

git checkout master

Check number of branches and which one are you currently using.

git branch

Dont forget to pull the last changes from the repo

git pull

Once in the master branch, you want to create a branch from master as it has the latest changes.
For this:
Create a dev branch and move to dev branch

git checkout -b dev

Create only a branch but continue in your master branch

git branch dev

Push the branch

git push

If you want to go back to master, execute once again git checkout master. It can happen that git does not let you because you have some changes pending to save and commit. You either save and commit or execute the follwing:
To save the changes temporarily

git stash 

Delete a branch with:

git branch --delete dev

A good advice once you create a new branch or later in your development is to check the difference between files and modifications. For this execute:

git diff --name-only target-branch source-branch

Merge Branches

Let’s say that dev branch needs to be merged to master branch.

git checkout master
git merge dev

Finally, it there is a need to fix a merge conflict with another developer, make sure to check in the link below the basic Basic merge conflict section: https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging

Undo your Changes

It is for sure that you will make some mistakes.
Here what you need to know and which commands to use depend at what stage you make the mistake and how you want to repair it. Undo Changes

Summary

1. git clone https://github.com/abarbercaselles/SQL-Snowflake.git
2. git status
3.1 git add .  - will add all the files to the staged area
3.2 git add file1.txt  - will add only one file to the staged area
4. git restore --staged file1.txt
5. git commit -m "I am committing with change in file1.txt"
6. git push
7. git pull
-----------------------------------------------------------------------------
1. git checkout master - Go to master branch
2. git branch - Check number of branches and which one are you currently using. 
3. git pull - Dont forget to pull the last changes from the repo 
4.1 git checkout -b dev - Create a dev branch and move to dev branch
4.2 git branch dev - Create only a branch but continue in your master branch
5. git push - to push the branch.
6. git stash - To save the changes temporarily 
7. git branch --delete dev
8. git diff --name-only target-branch source-branch
9. git merge dev
Back to Blog