git basic commands

Created: Jan. 1, 2010, 4 a.m.
Update: April 20, 2024, 4:42 a.m.
git and tricks

$ = Bash

Source

http://thelucid.com/2008/12/02/git-setting-up-a-remote-repository-and-doing-an-initial-push/

Set git user account

git config --global user.name "Your full name"
git config --global user.email "user@mail.com"

Send a project to a new respository / New repository

On server
ssh user@server.com
mkdir /home/user/dev/my_project
cd /home/user/dev/my_project
git init --bare
git update-server-info # If planning to serve via HTTP
exit
On local machine
cd my_project
git init 
git add . 
git commit -m "My initial commit message" 
git remote add origin user@server.com:/home/user/dev/my_project 
git push -u origin master

How to add file and folder.

git add .  # add all files
git add folder
git add path/file

Clone project from server

git clone git@example.com:my_project.git

remote. Add new server and ovewrite all files

https://git-scm.com/book/pt-br/v1/Git-Essencial-Trabalhando-com-Remotos
Add new server. Rename and/or delete a remote
$ git remote rename origin old-stable
$ git remote rm old-stable
$ git remote -v
$ git remote add <name> ssh://user@server.com:22/path
update from new server
$ git remote update origin
$ git fetch origin master -vf
checkout branch from new server
$ git checkout branch
$ git push --mirror https://github.com/my_username/my_repo.git
Undo last commit, keep altered files.
   $ git reset HEAD~

Pull for last commit, go to last commit.
   $ git reset --hard origin/master

Rollback to a old commit

git checkout <commit>

Remove a file of repository without delete of the local host.

git rm --cached path-to-file
get pull master
git pull origin master

To create a new branch at localhost

In Git 1.7.0 and later, you can checkout a new branch:
git checkout -b <branch>
Edit files, add and commit. Then push with the -u option:
git push -u origin <branch>

list branchs

git branch
change to a branch
git checkout <branch>
git clone -b <branch> <remote_repo>
git clone -b my-branch git@github.com:user/myproject.git
git clone -b my-branch https://git@github.com/username/myproject.git

git commit --amend é uma forma conveniente de modificar o commit mais recente. Ele permite que você combine alterações preparadas com o commit anterior, em vez de criar um novo commit.

git add main.py
git commit --amend --no-edit
or
git commit --amend -m "uma mensagem de commit atualizada"

copy a specific file from another branch, "copy" from master to frontend.  Overwrite from another branch.

git checkout frontend
git checkout master path/to/file

Git merge branch

git checkout frontend
git merge master

conflict files, abort merge.

git merge --abort

overwrite conflict file

git checkout master path/to/file

Show diff code between branchs

git diff branch0 branch1 -- full/path/to/file

Show diff of commit

git show commit-id

Make a copy of modified: deleted: renamed:  files

set filter
$ FLT='(modified:|deleted:|renamed:)'
set prefix
$ PFX=$(date +%d%m%Y)
command
$ echo -e $PFX; for X in `git status | egrep $FLT | cut -d: -f2`; do cp $X $X.$PFX -v; done


Tiago de Souza Moraes - teago.futuria.com.br - 2024 // CSS by UIKIT CSS