If you are used to work with GUI tool and want to learn the basics of working using the terminal, keep on reading.
- en: Create a new project to start.
mkdir project-name # create folder project
cd project-name # enter folder project
- 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)
- 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
- Settings > Developer settings > New personal access token
- Save your private token*
ghp_1BX2Kw9snvtQq6ttc9vX...uh
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
- 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
# 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
# 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
# status
git status
# ... add and commit
git push origin new-branch-name
# 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
- 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
- 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
- en: After merge with master
git brash -d new_branch_name
- 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
- en: Send your files to origin fork repository
git push --set-upstream origin master
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
git checkout @{-1} # back -1 branch history
git checkout -
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
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
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.
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
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
$ git push -u origin main
# or
$ git push -u origin --all
# or
$ git config --global push.default current
git error fatal: Invalid branch name: 'HEAD'
$ git config --global init.defaultBranch main
$ cat ./.git/HEAD
$ rm -rf ./.git/HEAD # if necessary
$ git init
$ git checkout main --force