Git Product home page Git Product logo

natancabral / git-start Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 137 KB

Basic Git commands. git init initializes a brand new Git repository and begins tracking an existing directory. git clone creates a local copy of a project that already exists remotely. git add stages a change. git commit saves the snapshot to the project history and completes the change-tracking process.

git github projects

git-start's Introduction

Learn to use Git Terminal by example

If you are used to work with GUI tool and want to learn the basics of working using the terminal, keep on reading.

Start Project

  • en: Create a new project to start.
mkdir project-name # create folder project
cd project-name # enter folder project

Initialize Git

  • en: You need install GIT first. Terminal: "sudo apt install git".
git init # create files git

GitHub

You need login on GitHub to create a new repository (+ > new repository). Give your repository a name--ideally the same name as your local project. The next screen you see will be important, so don't close it. Inside click on Code > Clone (url)

Back Terminal

  • en: Create a new file.
echo '# Title' > README.md
  • en: Now link your local project with remote project (repository):
git remote add origin https://github.com/<github_username>/<repository_name>.git
git remote -v # show origin 

Token

  • Settings > Developer settings > New personal access token
  • Save your private token*
ghp_1BX2Kw9snvtQq6ttc9vX...uh

Global Config

Configure Global UserName

git config --global user.email “[email protected]”
git config --global user.name “gitusername“
git config --list # list data
  • en: To edit file
nano ~/.gitconfig # edit file
  • en: Save username for 8h.
git config --global credential.helper ‘cache –timeout=28800’
  • en: Save username permanently.
git config --global credential.helper cache

Basic Functions

Basic Functions

Push Origin Master

  • en: Change file or create a new file and send to repositoty:
git add .
git commit -m "words"
git push origin master # or git push

Change Branch Name

# go to branch
git checkout branch-name
# a new
git checkout -m new-branch
# rename your current branch
# -m --force
git branch -m new-branch-name
# list all branchs
git status -a
# change remote branch too
git branch -m :old-branch-name new-branch-name
# finaly remote upstream (-set-upstream-to define future push and sync branchs)
# pt-BR: finalmente define upstream remoto (-set-upstream-to define onde os próximos push 
# devem ser enviados, mantendo a branch local e remota em sincronia)
git push origin -u new-branch-name
git remote -v

Ref

Branch

Create Branch

# option 1
git branch new-branch-name
git checkout new-branch-name
# option 2
git checkout -b new-branch-name 
# no necessary -b if branch exists

Ref

Staus / Push

# status
git status
# ... add and commit
git push origin new-branch-name

Merge Branches

# change to master or master branch
git checkout master
# merge master <- new-branch-name
git merge new-branch-name
  • en: Now send changed files to repository master
git add .
git commit -m "merge commit"
git push origin master
# or
git push

Find Branches

  • en: On GitHub open repository and find above files a link named BRANCHES. All branches storages on this page
# to list brachs localy
git branch
# list too remote branchs
git branch -a

Log

  • en: Show pushes
git log
git log --oneline # short
git log --all --decorate --oneline --graph # nice | highlight
# to remember flags: "A DOG"
# A --all
# D --decorate
# O --oneline
# G --graph

Delete Branch

  • en: After merge with master
git brash -d new_branch_name

Alias | Shotcuts Commands

  • en: Create alias
# to create shotcut "dog"
# command "log --all --decorate --oneline --graph"
git config --global alias.dog "log --all --decorate --oneline --graph"
  • en: Remove alias
git config --global --unset alias.dog

Fork Original

  • en: Send your files to origin fork repository
git push --set-upstream origin master

Previous Commits and Branchs

pt: Qual a diferença entre RESET e REVERT?

  • RESET - Aponta para uma COMMIT e apaga tudo que foi criado ou alterado após essa commit específica. Perdendo todo seu histórico.
  • 1,2,[3],4,5 -> 1,2,3
  • REVERT - Reverte uma COMMIT específica, as alterações dessa commit escolhida será revertida mas o histórico posterior mantido.
  • 1,2,[3],4,5 -> 1,2,4,5

1 of 4 ) Previous Branch @{-1}

Link Link

git checkout @{-1} # back -1 branch history
git checkout -

2 of 4 ) Previous Commit RESET | REVERT

Link

RESET

git log --oneline # show commits
# copy head commit, example: c14809fa or c14809fafb08b9e96ff2879999ba8c807d10fb07
# Won't have to type the entire sha, just a little bit will work
git reset --sorf c14809fa 
git reset --mixed c14809fa 
git reset --hard c14809fa
git reset --hard HEAD~1 # previous 1 back
git reset --hard HEAD~2 # previous 2 back
git commit

REVERT

This will revert the last commit:

git revert HEAD

Or

git log --oneline # show commits
git revert c14809fa
git revert c14809fa 0d1d7fc32  # 2 commits
git revert c14809fa..0d1d7fc32 # range commits
# flags
git revert c14809fa --no-edit # no open file revert
git revert c14809fa --no-commit # no commit this revert
# final
git commit

Revert error: go back

git revert --abort

3 of 4 ) Hard delete unpublished commits

If, on the other hand, you want to really get rid of everything you've done since then, there are two possibilities. One, if you haven't published any of these commits, simply reset:

Bad idea

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32
git commit

**Better idea**

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.

4 of 4 ) Undo published commits with new commits

On the other hand, if you've published the work, you probably don't want to reset the branch, since that's effectively rewriting history. In that case, you could indeed revert the commits. With Git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. This way you don't rewrite any history.

# This will create three separate revert commits:
git revert a867b4af 25eee4ca 0766c053

# It also takes ranges. This will revert the last two commits:
git revert HEAD~2..HEAD

#Similarly, you can revert a range of commits using commit hashes (non inclusive of first hash):
git revert 0d1d7fc..a867b4a

# Reverting a merge commit
git revert -m 1 <merge_commit_sha>

# To get just one, you could use `rebase -i` to squash them afterwards
# Or, you could do it manually (be sure to do this at top level of the repo)
# get your index and work tree into the desired state, without changing HEAD:
git checkout 0d1d7fc32 .

# Then commit. Be sure and write a good message describing what you just did
git commit

Error

Error/Problem

fatal: The current branch master has no upstream branch. To push the current branch and set the remote as upstream, use

git push --set-upstream origin master

Soluction

stackoverflow stackoverflow

$ git push -u origin main
# or 
$ git push -u origin --all
# or 
$ git config --global push.default current

Error/Problem

git error fatal: Invalid branch name: 'HEAD'

Solution

$ git config --global init.defaultBranch main
$ cat ./.git/HEAD
$ rm -rf ./.git/HEAD # if necessary
$ git init
$ git checkout main --force

git-start's People

Contributors

natancabral avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.