Tags: , ,

Git

1   Create a new repository

# Enter the project directory
cd project

# Create the ignore list
cat<<EOT>.gitignore
*.aux
*.bbl
*.blg
*.log
*.toc
*.pdf
EOT

# Create an empty repository
git init

# Stage project files
git add .gitignore *

# Commit
git commit -m 'Initial import'

2   Clone an existing repository

git clone user@server:path/to/project

3   Clone an existing repository as pull-only

git clone user@server:path/to/project
git remote set-url --push origin no_push

4   Add a remote

git remote add remote_name remote_url

5   Pull

git pull remote_name branch_name

6   Pull individual files

Is it possible to pull just one file in Git?

git fetch {remote}
git checkout FETCH_HEAD -- {file}

7   Undo

7.1   Local commit

git reset --soft HEAD^     # use --soft if you want to keep your changes
git reset --hard HEAD^     # use --hard if you don't care about keeping the changes you made

7.2   Public commit

git reset --hard HEAD^
git push -f origin HEAD

8   Tag

git tag                                 # list tags
git tag -l pattern                      # list tags matching pattern
git tag -a v0.1 -m "annotated tag v0.1" # create an annotated tag
git show v0.1                           # show tag information

git tag v0.1-lw                         # create a lightweight tag

9   Show the origin URL

git config --get remote.origin.url

git remote show origin

10   Rename a branch

Rename a local and remote branch in git

# Rename your local branch.
# If you are on the branch you want to rename:
git branch -m new-name

# If you are on a different branch:
git branch -m old-name new-name

# Delete the old-name remote branch and push the new-name local branch.
git push origin :old-name new-name

# Reset the upstream branch for the new-name local branch.
# Switch to the branch and then:
git push origin -u new-name

11   Checkout a branch from one remote and push it to another

# Checkout a branch from one remote (unique branch name)
git checkout branch-name

# Push it to another remote
git push -u another-remote

12   Cache credentials

Git credential cache, why type password again and again

# timeout after 28,800 seconds (8 hours)
git config --global credential.helper 'cache --timeout 28800'

# forget
git credential-cache exit

# Windows
git config --global credential.helper wincred

13   References