6 Useful Git commands
Ralf Becker edited this page 2023-11-22 17:20:39 +02:00

WikiDevelopmentUseful Git Commands

Useful Git Commands

Status

Getting current status of repository which would show modified/merged/stashed files or commits that need to be pushed.

git status


Branch

In order to get list of branches or create one.

List of branches: git branch -a

Create branch: git branch <your branch name>

Delete branch: git branch -b <branch name>

switch to another branch: git checkout <branch name>
e.g.: git checkout 17.1


Merge

Sometimes we need to merge our branch with master.

git checkout <your branch>
git rebase master

or when we want to merge only a single commit into our branch from another branch (e.g. backporting):

git cherry-pick <commit hash>

--no-commit: this will help if you don't want your cherry-pick be committed right after.

Merge master into current branch, making it like copying all files from master, but in an updatable fashion:

git merge --no-squash -s recursive -Xtheirs master

Revert

For reverting uncommitted changes on files you may run the following:

git checkout <filename>

and for reverting commits which are not pushed you may use:

git reset --hard <commit hash>

or if you want to go back to latest previous commit:

git reset --hard HEAD^

IMPORTANT: reset with --hard parameter reset your commits and you may no longer have access to your changes.

Copy a file including history from one repo to an other

git log --pretty=email --patch-with-stat --reverse --full-index --binary -- path/to/file_or_folder > /tmp/commits.log

Fix the relative path to new repo

sed -i 's|path/to/file|new/path/to/file|g' /tmp/commits.log

Reapply it to new repo

cd /path/to/new/repo
git am < /tmp/commits.log