Getting started with Git

getting started with git

Create a new repository

Create a directory where you'll store your project and inside that directory run command:

git init

This command will create all necessary files in your project to allow you to upload your progress on GitHub or Bit Bucket

Checkout a repository

Create a working copy of a local repository by running the command:

git clone /path/to/local/repository

When you want to use a remote server like Github or BitBucket your command will become:

git clone

where username is the github account name and repository-name is the name of the remove repository hosted in github.

Git-Specific Commands

Since Git was designed with a big project like Linux in mind, there are a lot of Git commands. However, to use the basics of Git, you’ll only need to know a few terms. They all begin the same way, with the word “git.”

git init

Initializes a new Git repository. Until you run this command inside a repository or directory, it’s just a regular folder. Only after you input this does it accept further Git commands.

git config

Short for “configure,” this is most useful when you’re setting up Git for the first time.

git config --global ""
git config --global "my-github-username"

git config --global push.default simple
git config --global push.default simple will refuse to push if the upstream branch's name is different from the local one

git help

Forgot a command? Type this into the command line to bring up the 21 most common git commands. You can also be more specific and type “git help init” or another term to figure out how to use and configure a specific git command.

git status

Check the status of your repository. See which files are inside it, which changes still need to be committed, and which branch of the repository you’re currently working on.

git status

git add

This does not add new files to your repository. Instead, it brings new files to Git’s attention. After you add files, they’re included in Git’s “snapshots” of the repository.

# stage a single file
git add path/to/filename.ext

# stage all files and directories recursively from a directory
git add paht/to/directory

# stage changes without deleted
git add .

# stage everything including deleted files
git add --all .

git commit

Git’s most important command. After you make any sort of change, you input this in order to take a “snapshot” of the repository. Usually it goes git commit -m “Message here.” The -m indicates that the following section of the command should be read as a message.

git commit -m 'this is my commit description'

git cherry-pick

If you wrote some code in another branch/commit, you can cherry-pick it and add it to your working branch:

# cherry-pick specific commit (you can provide full commit hash as well)
git cherry-pick 12abc3

git branch

Working with multiple collaborators and want to make changes on your own? This command will let you build a new branch, or timeline of commits, of changes and file additions that are completely your own. Your title goes after the command. If you wanted a new branch called “cats,” you’d type git branch cats.

# list local branches
git branch

# list remote branches
git branch -r

# list local and remote branches
git branch -a

# search for a specific branch using grep
git branch -a | grep my-branch

git checkout

Literally allows you to “check out” a repository that you are not currently inside. This is a navigational command that lets you move to the repository you want to check. You can use this command as git checkout master to look at the master branch, or git checkout cats to look at another branch.

# load a specific branch
git checkout develop

# reset file changes
git checkout -- path/to/file.ext

# grab specific commit into your current branch
git checkout [commit hash]

git merge

When you’re done working on a branch, you can merge your changes back to the master branch, which is visible to all collaborators. git merge cats would take all the changes you made to the “cats” branch and add them to the master.

# merge develop branch into current branch
git merge origin develop

# merge branches "fixes" and "enhancements" on top of the current branch, making an octopus merge
git merge fixes enhancements

# merge branch "my-branch" into the current branch, using ours merge strategy:
git merge -s ours my-branch

# merge branch "my-bug-fix" into the current branch, but do not make a new commit automatically:
git merge --no-commit my-bug-fix

git push

If you’re working on your local computer, and want your commits to be visible online on GitHub as well, you “push” the changes up to GitHub with this command.

# usualy the very first commit on your repository and set master branch as an update stream
git push -u origin master

# push on server after you commited changes
git push origin my-current-branch

# or
git push # this command might output some warnings, depending on the settings

git pull

If you’re working on your local computer and want the most up-to-date version of your repository to work with, you “pull” the changes down from GitHub with this command.

# get changes from the same branch if setup stream is set
git pull

# get changes from the same branch
git pull origin my-current-branch-name

# get changes from the same branch with rebase option
git pull --rebase origin my-current-branch-name

git rebase (squash commits)

When you want to cleanup a branch or a PR, that means you'll want to rebase your branch against another branch (usually master or develop). Those are the commands that you'll have to run:

Let's pretend that you are on your current working branch my-branch and want to rebase it against develop branch

# go on main branch (develop in our case)
git checkout develop

# update branch to have the latest changes
git pull --rebase origin develop # "origin develop" is optional

# switch back to your branch
git checkout origin my-branch

# rebase your branch against develop
git rebase -i develop # -i means "interactive"

# next, you'll see a list with your commits and you'll have to select which one to squash, remove, etc

# update your branch (Always with --force option)
git push -f origin my-brnach